From 9f0f03ebedcc3c227939e1a3ed62b083e7f10426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 6 Jul 2021 23:52:24 +0200 Subject: [PATCH] removing half of backgrounds (priority not functional) --- sources/PPU/Background.cpp | 25 +++++++++++++++---------- sources/PPU/Background.hpp | 9 ++++++--- sources/PPU/PPU.cpp | 34 ++++++++++++++++------------------ sources/PPU/PPU.hpp | 8 ++++---- sources/PPU/PpuDebug.cpp | 8 ++++---- 5 files changed, 45 insertions(+), 39 deletions(-) diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 07285e6..dc10de3 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -35,7 +35,8 @@ namespace ComSquare::PPU void Background::renderBackground() { uint16_t vramAddress = this->_tileMapStartAddress; - Vector2 offset = this->_ppu.getBgScroll(this->_bgNumber); + //Vector2 offset = this->_ppu.getBgScroll(this->_bgNumber); + Vector2i offset = {0, 0}; this->backgroundSize.x = (static_cast(this->_tileMapMirroring.x) + 1) * this->_characterNbPixels.x * NbCharacterWidth; this->backgroundSize.y = @@ -44,10 +45,10 @@ namespace ComSquare::PPU this->_drawBasicTileMap(vramAddress, offset); for (int i = 1; i < 4; i++) { vramAddress += TileMapByteSize; - offset.x += NbCharacterWidth * this->_characterNbPixels.x; + offset.x++; if (i == 2) { offset.x = 0; - offset.y += NbCharacterHeight * this->_characterNbPixels.y; + offset.y++; } if (i > 1 && !this->_tileMapMirroring.y) break; @@ -79,7 +80,7 @@ namespace ComSquare::PPU } } - void Background::_drawTile(uint16_t data, Vector2 pos) + void Background::_drawTile(uint16_t data, Vector2 indexOffset) { union Utils::TileData tileData; @@ -87,6 +88,7 @@ namespace ComSquare::PPU //if (tileData.tilePriority != this->_priority) // return; + this->tilesPriority[indexOffset.x][indexOffset.y] = tileData.tilePriority; this->_drawTileFromMemoryToTileBuffer(tileData); // todo check why i need to invert vertical and horizontal flips @@ -94,10 +96,12 @@ namespace ComSquare::PPU Utils::HFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y}); if (tileData.horizontalFlip) Utils::VFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y}); + + Vector2 pixelPosition{indexOffset.x * this->_characterNbPixels.x, indexOffset.y * this->_characterNbPixels.y}; std::for_each(this->_tileBuffer.begin(), this->_tileBuffer.begin() + this->_characterNbPixels.y, - [this, &pos](const auto &row) { + [this, &pixelPosition](const auto &row) { std::move(row.begin(), row.begin() + this->_characterNbPixels.x, - this->buffer[pos.y++].begin() + pos.x); + this->buffer[pixelPosition.y++].begin() + pixelPosition.x); }); } @@ -111,15 +115,16 @@ 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; - this->_drawTile(tileMapValue, {(pos.x * this->_characterNbPixels.x) + offset.x, - (pos.y * this->_characterNbPixels.y) + offset.y}); + this->_drawTile(tileMapValue, {(offset.x * NbCharacterWidth) + pos.x, + (offset.y * NbCharacterHeight) + pos.y}); vramAddress += 2; if (pos.x % 31 == 0 && pos.x) { pos.y++; pos.x = 0; } - else + else { pos.x++; + } } } @@ -147,7 +152,7 @@ namespace ComSquare::PPU this->_tileRenderer.setBpp(this->_bpp); } - void Background::setTilemaps(Vector2 tileMaps) + void Background::setTileMapMirroring(Vector2 tileMaps) { this->_tileMapMirroring = tileMaps; } diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index 175efe2..ca62f38 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -60,12 +60,15 @@ namespace ComSquare::PPU //! @brief The access to cgram std::shared_ptr _cgram; //! @brief Draw a tile on the screen at x y pos - void _drawTile(uint16_t data, Vector2 pos); + //! @param data The VRAM value to be interpreted as a Utils::TileData + //! @param indexOffset The index offset of the Tile (ranging from 0 to 63) + void _drawTile(uint16_t data, Vector2 indexOffset); //! @brief Draw the tile to the tile Buffer + //! @param tileData The tile data to use to render the tile 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 + //! @param offset The offset of the tile map (ranging from 0 to 1) void _drawBasicTileMap(uint16_t baseAddress, Vector2 offset); public: //! @brief The size of the background (x, y) @@ -92,7 +95,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 setTileMapMirroring(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 89c6325..46c86da 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -23,13 +23,13 @@ namespace ComSquare::PPU _renderer(renderer), _backgrounds{ Background(*this, 1, false), - Background(*this, 1, true), + //Background(*this, 1, true), Background(*this, 2, false), - Background(*this, 2, true), + //Background(*this, 2, true), Background(*this, 3, false), - Background(*this, 3, true), + //Background(*this, 3, true), Background(*this, 4, false), - Background(*this, 4, true) + //Background(*this, 4, true) }, _mainScreen({{{0}}}), _subScreen({{{0}}}) @@ -129,10 +129,10 @@ namespace ComSquare::PPU this->getTileMapStartAddress(addr - PpuRegisters::bg1sc + 1)); this->_backgrounds[addr - PpuRegisters::bg1sc + 1].setTileMapStartAddress( this->getTileMapStartAddress(addr - PpuRegisters::bg1sc + 1)); - this->_backgrounds[addr - PpuRegisters::bg1sc].setTilemaps( + this->_backgrounds[addr - PpuRegisters::bg1sc].setTileMapMirroring( {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->_backgrounds[addr - PpuRegisters::bg1sc + 1].setTileMapMirroring( {static_cast(this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapHorizontalMirroring), static_cast(this->_registers._bgsc[addr - PpuRegisters::bg1sc].tilemapVerticalMirroring)}); break; @@ -553,9 +553,7 @@ namespace ComSquare::PPU { uint16_t colorPalette; // should only render backgrounds needed (depending of th bgMode) - int i = 0; for (auto &_background : this->_backgrounds) { - i++; _background.renderBackground(); } // TODO make a function getDefaultBgColor @@ -572,30 +570,30 @@ namespace ComSquare::PPU this->addToMainSubScreen(this->_backgrounds[BgName::bg4NoPriority]); this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority]); //sprites priority 0 - this->addToMainSubScreen(this->_backgrounds[BgName::bg4Priority]); - this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); + // this->addToMainSubScreen(this->_backgrounds[BgName::bg4Priority]); + // this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); //sprites priority 1 this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority]); this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority]); //sprites priority 2 - this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); - this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); + // this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); + // this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); //sprites priority 3 break; case 1: this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority]); //sprites priority 0 - if (!this->_registers._bgmode.mode1Bg3PriorityBit) - this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); + // if (!this->_registers._bgmode.mode1Bg3PriorityBit) + // this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); //sprites priority 1 this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority]); this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority]); //sprites priority 2 - this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); - this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); + // this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); + // this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); //sprites priority 3 - if (this->_registers._bgmode.mode1Bg3PriorityBit) - this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); + // if (this->_registers._bgmode.mode1Bg3PriorityBit) + // this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); break; case 2: this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority]); diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 175eb04..f0b4ebf 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -34,11 +34,11 @@ namespace ComSquare::PPU enum BgName { bg1NoPriority = 0, bg1Priority, - bg2NoPriority, + bg2NoPriority = 1, bg2Priority, - bg3NoPriority, + bg3NoPriority = 2, bg3Priority, - bg4NoPriority, + bg4NoPriority = 3, bg4Priority }; @@ -564,7 +564,7 @@ namespace ComSquare::PPU Registers _registers{}; Renderer::IRenderer &_renderer; //! @brief Backgrounds buffers - Background _backgrounds[8]; + Background _backgrounds[4]; //! @brief Main Screen buffer std::array, 1024> _mainScreen; //! @brief Sub Screen buffer diff --git a/sources/PPU/PpuDebug.cpp b/sources/PPU/PpuDebug.cpp index f4f3301..2657e27 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.getBackgroundMirroring(1)); + ppu._backgrounds[0].setTileMapMirroring(ppu.getBackgroundMirroring(1)); ppu._backgrounds[1].setTileMapStartAddress(ppu.getTileMapStartAddress(1)); - ppu._backgrounds[1].setTilemaps(ppu.getBackgroundMirroring(1)); + ppu._backgrounds[1].setTileMapMirroring(ppu.getBackgroundMirroring(1)); ppu._backgrounds[2].setTileMapStartAddress(ppu.getTileMapStartAddress(2)); - ppu._backgrounds[2].setTilemaps(ppu.getBackgroundMirroring(2)); + ppu._backgrounds[2].setTileMapMirroring(ppu.getBackgroundMirroring(2)); ppu._backgrounds[3].setTileMapStartAddress(ppu.getTileMapStartAddress(2)); - ppu._backgrounds[3].setTilemaps(ppu.getBackgroundMirroring(2)); + ppu._backgrounds[3].setTileMapMirroring(ppu.getBackgroundMirroring(2)); ppu._backgrounds[4].setTileMapStartAddress(ppu.getTileMapStartAddress(3)); ppu._backgrounds[5].setTileMapStartAddress(ppu.getTileMapStartAddress(3));