diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index bc2131e..07285e6 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -57,15 +57,8 @@ namespace ComSquare::PPU } } - void Background::_drawBgTile(uint16_t data, Vector2 pos) + void Background::_drawTileFromMemoryToTileBuffer(const Utils::TileData &tileData) { - union Utils::TileMapData tileData; - - tileData.raw = data; - - if (tileData.tilePriority != this->_priority) - return; - uint16_t graphicAddress; Vector2i tileOffset = {0, 0}; // X horizontal @@ -84,6 +77,17 @@ namespace ComSquare::PPU tileOffset.x = 0; tileOffset.y++; } + } + + void Background::_drawTile(uint16_t data, Vector2 pos) + { + union Utils::TileData tileData; + + tileData.raw = data; + + //if (tileData.tilePriority != this->_priority) + // return; + this->_drawTileFromMemoryToTileBuffer(tileData); // todo check why i need to invert vertical and horizontal flips if (tileData.verticalFlip) @@ -107,8 +111,8 @@ namespace ComSquare::PPU // TODO function to read 2 bytes (LSB order or bits reversed) tileMapValue = this->_vram->read(vramAddress); tileMapValue += this->_vram->read(vramAddress + 1) << 8U; - _drawBgTile(tileMapValue, {(pos.x * this->_characterNbPixels.x) + offset.x, - (pos.y * this->_characterNbPixels.y) + offset.y}); + this->_drawTile(tileMapValue, {(pos.x * this->_characterNbPixels.x) + offset.x, + (pos.y * this->_characterNbPixels.y) + offset.y}); vramAddress += 2; if (pos.x % 31 == 0 && pos.x) { pos.y++; @@ -162,4 +166,9 @@ namespace ComSquare::PPU { return this->_priority; } + + bool Background::isPriorityPixel(int y, int x) const + { + return this->tilesPriority[y / this->_characterNbPixels.y][x / this->_characterNbPixels.x]; + } } \ No newline at end of file diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index 11e7246..175efe2 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -11,6 +11,7 @@ #include "TileRenderer.hpp" #include "../Ram/Ram.hpp" #include "PPU.hpp" +#include "PPUUtils.hpp" namespace ComSquare::PPU { @@ -33,7 +34,7 @@ namespace ComSquare::PPU //! @brief The tilemap configuration nb of tileMap vertically and horizontally //! @note members are set to true if the tilemap is expended in their direction Vector2 _tileMapMirroring; - //! @brief The number of pixels of a character (x: width, y:height) + //! @brief The number of pixels of a character (x: height, y:width) Vector2 _characterNbPixels; //! @brief The number of bits per pixels to currently look for each pixel int _bpp; @@ -59,7 +60,9 @@ namespace ComSquare::PPU //! @brief The access to cgram std::shared_ptr _cgram; //! @brief Draw a tile on the screen at x y pos - void _drawBgTile(uint16_t data, Vector2 pos); + void _drawTile(uint16_t data, Vector2 pos); + //! @brief Draw the tile to the tile Buffer + void _drawTileFromMemoryToTileBuffer(const union Utils::TileData &tileData); //! @brief draw a tileMap 32x32 starting at baseAddress //! @param baseAddress The starting address of the tileMap //! @param offset The rendering offeset in pixels @@ -70,6 +73,10 @@ namespace ComSquare::PPU //! @brief The output buffer (pixels are written on it) std::array, 1024> buffer; + std::array, 64> tilesPriority; + + bool isPriorityPixel(int x, int y) const; + //! @brief Render a background on his internal buffer void renderBackground(); //! @brief Set the tileMap start address diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index a0e58c8..89c6325 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -36,7 +36,7 @@ namespace ComSquare::PPU { this->_registers._isLowByte = true; - //Utils::Debug::populateEnvironment(*this, 0); + Utils::Debug::populateEnvironment(*this, 1); } uint8_t PPU::read(uint24_t addr) diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp index 106f350..4faa8c2 100644 --- a/sources/PPU/PPUUtils.hpp +++ b/sources/PPU/PPUUtils.hpp @@ -22,7 +22,7 @@ namespace ComSquare::PPU::Utils //! @brief Transform SNES color code BGR to uint32_t RGB uint32_t getRealColor(uint16_t color); //! @brief Used to parse easily VRAM Tile information - union TileMapData { + union TileData { struct { //! @brief Tile X offset uint16_t posX: 4;