From 6691a40c42eddd0fae8ada21ad5cf4979329db06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sat, 3 Jul 2021 17:47:30 +0200 Subject: [PATCH] real fix for tilemap drawing --- sources/PPU/Background.cpp | 22 +++++++++++++--------- sources/PPU/Background.hpp | 6 +++--- sources/PPU/PPU.cpp | 16 ++++++++-------- sources/PPU/PPU.hpp | 4 ++-- sources/PPU/PpuDebug.cpp | 8 ++++---- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 8a19044..443c755 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -14,7 +14,7 @@ namespace ComSquare::PPU { Background::Background(ComSquare::PPU::PPU &ppu, int backGroundNumber, bool hasPriority): _ppu(ppu), - _tileMapsConfig(ppu.getBackgroundSize(backGroundNumber)), + _tileMapsConfig(ppu.getBackgroundMirroring(backGroundNumber)), _characterNbPixels(ppu.getCharacterSize(backGroundNumber)), _bpp(ppu.getBPP(backGroundNumber)), _directColor(false), @@ -36,20 +36,24 @@ namespace ComSquare::PPU { uint16_t vramAddress = this->_tileMapStartAddress; Vector2 offset = this->_ppu.getBgScroll(this->_bgNumber); - this->backgroundSize.x = this->_tileMapsConfig.x * this->_characterNbPixels.x * NbCharacterWidth; - this->backgroundSize.y = this->_tileMapsConfig.y * this->_characterNbPixels.y * NbCharacterHeight; + this->backgroundSize.x = + static_cast(this->_tileMapsConfig.x) * this->_characterNbPixels.x * NbCharacterWidth; + this->backgroundSize.y = + static_cast(this->_tileMapsConfig.y) * this->_characterNbPixels.y * NbCharacterHeight; - for (int i = 0; i < 4; i++) { - if (((i == 0 || i == 1) && this->_tileMapsConfig.x == 1) - || ((i == 2 || i == 3) && this->_tileMapsConfig.y == 1)) { - _drawBasicTileMap(vramAddress, offset); - } + this->_drawBasicTileMap(vramAddress, offset); + for (int i = 1; i < 4; i++) { vramAddress += TileMapByteSize; offset.x += NbCharacterWidth * this->_characterNbPixels.x; if (i == 2) { offset.x = 0; offset.y += NbCharacterHeight * this->_characterNbPixels.y; } + if (i > 1 && !this->_tileMapsConfig.y) + break; + if ((i == 1 || i == 3) && !this->_tileMapsConfig.x) + continue; + this->_drawBasicTileMap(vramAddress, offset); } } @@ -142,7 +146,7 @@ namespace ComSquare::PPU this->_tileRenderer.setBpp(this->_bpp); } - void Background::setTilemaps(Vector2 tileMaps) + void Background::setTilemaps(Vector2 tileMaps) { this->_tileMapsConfig = tileMaps; } diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index 518b622..b8b7642 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -31,8 +31,8 @@ namespace ComSquare::PPU //! @brief the ppu used to get registers values (ex: bg scroll) ComSquare::PPU::PPU &_ppu; //! @brief The tilemap configuration nb of tileMap vertically and horizontally - //! @note members are set to 1 if the tilemap is expended in their direction - Vector2 _tileMapsConfig; + //! @note members are set to true if the tilemap is expended in their direction + Vector2 _tileMapsConfig; //! @brief The number of pixels of a character (x: width, y:height) Vector2 _characterNbPixels; //! @brief The number of bits per pixels to currently look for each pixel @@ -85,7 +85,7 @@ namespace ComSquare::PPU void setBpp(int bpp); //! @brief setter for private variable _tileMaps //! @param tileMaps The tileMaps to set - void setTilemaps(Vector2 tileMaps); + void setTilemaps(Vector2 tileMaps); //! @brief Get the BackGround Number //! @return the current Background number diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 284e2b9..b6fe012 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -130,11 +130,11 @@ namespace ComSquare::PPU this->_backgrounds[addr - PpuRegisters::bg1sc + 1].setTileMapStartAddress( this->getTileMapStartAddress(addr - PpuRegisters::bg1sc + 1)); this->_backgrounds[addr - PpuRegisters::bg1sc].setTilemaps( - {this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapHorizontalMirroring, - this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapVerticalMirroring}); + {static_cast(this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapHorizontalMirroring), + static_cast(this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapVerticalMirroring)}); this->_backgrounds[addr - PpuRegisters::bg1sc + 1].setTilemaps( - {this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapHorizontalMirroring, - this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapVerticalMirroring}); + {static_cast(this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapHorizontalMirroring), + static_cast(this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapVerticalMirroring)}); break; case PpuRegisters::bg12nba: case PpuRegisters::bg34nba: @@ -540,12 +540,12 @@ namespace ComSquare::PPU return baseAddress; } - Vector2 PPU::getBackgroundSize(int bgNumber) const + Vector2 PPU::getBackgroundMirroring(int bgNumber) const { - Vector2 backgroundSize(0,0); + Vector2 backgroundSize(false, false); - backgroundSize.y = (this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring) ? 2 : 1; - backgroundSize.x = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 2 : 1; + backgroundSize.y = this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring; + backgroundSize.x = this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring; return backgroundSize; } diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 46eefdd..a96dfa7 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -619,8 +619,8 @@ namespace ComSquare::PPU uint16_t getTileMapStartAddress(int bgNumber) const; //! @brief Give the address to find the correct tileset for a given x and y uint16_t getTilesetAddress(int bgNumber) const; - //! @brief Give the number of tilemaps to be rendered - Vector2 getBackgroundSize(int bgNumber) const; + //! @brief Tells if the tilemap is expanded for the x and y directions + Vector2 getBackgroundMirroring(int bgNumber) const; //! @brief Render the Main and sub screen correctly void renderMainAndSubScreen(); //! @brief Add a bg buffer to another buffer diff --git a/sources/PPU/PpuDebug.cpp b/sources/PPU/PpuDebug.cpp index aa213cf..f4f3301 100644 --- a/sources/PPU/PpuDebug.cpp +++ b/sources/PPU/PpuDebug.cpp @@ -3795,13 +3795,13 @@ namespace ComSquare::PPU::Utils::Debug ppu._registers._bgsc[1].tilemapHorizontalMirroring = 1; ppu._registers._bgsc[2].tilemapAddress = 0x5C00U >> 10U; ppu._backgrounds[0].setTileMapStartAddress(ppu.getTileMapStartAddress(1)); - ppu._backgrounds[0].setTilemaps(ppu.getBackgroundSize(1)); + ppu._backgrounds[0].setTilemaps(ppu.getBackgroundMirroring(1)); ppu._backgrounds[1].setTileMapStartAddress(ppu.getTileMapStartAddress(1)); - ppu._backgrounds[1].setTilemaps(ppu.getBackgroundSize(1)); + ppu._backgrounds[1].setTilemaps(ppu.getBackgroundMirroring(1)); ppu._backgrounds[2].setTileMapStartAddress(ppu.getTileMapStartAddress(2)); - ppu._backgrounds[2].setTilemaps(ppu.getBackgroundSize(2)); + ppu._backgrounds[2].setTilemaps(ppu.getBackgroundMirroring(2)); ppu._backgrounds[3].setTileMapStartAddress(ppu.getTileMapStartAddress(2)); - ppu._backgrounds[3].setTilemaps(ppu.getBackgroundSize(2)); + ppu._backgrounds[3].setTilemaps(ppu.getBackgroundMirroring(2)); ppu._backgrounds[4].setTileMapStartAddress(ppu.getTileMapStartAddress(3)); ppu._backgrounds[5].setTileMapStartAddress(ppu.getTileMapStartAddress(3));