From 6964290dcfc755521d3b50821817b0d6d6a1d4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 12 Jul 2021 00:43:08 +0200 Subject: [PATCH] transforming the addBuffer function into a templated function to save time with level etc --- sources/PPU/Background.hpp | 7 +++---- sources/PPU/PPU.cpp | 28 +++++++++++----------------- sources/PPU/PPU.hpp | 11 ++++++++++- tests/PPU/testTileRenderer.cpp | 1 + 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index f71d992..6ec57e7 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -110,12 +110,11 @@ namespace ComSquare::PPU //! @brief Add a bg buffer to another buffer - template + template static void mergeBackgroundBuffer(std::array, DEST_SIZE_X> &bufferDest, std::array, DEST_SIZE_X> &pixelDestinationLevelMap, - const Background &backgroundSrc, - int levelLow, - int levelHigh) + const Background &backgroundSrc + ) { int i = 0; int j = 0; diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index a488e23..93a662f 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -6,6 +6,7 @@ #include #include "PPU.hpp" #include "Exceptions/InvalidAddress.hpp" +#include "PPU/Background.hpp" #include "Models/Vector2.hpp" namespace ComSquare::PPU::Utils::Debug { @@ -570,27 +571,30 @@ namespace ComSquare::PPU // the starting palette index isn't implemented switch (this->_registers._bgmode.bgMode) { case 0: - this->addToMainSubScreen(this->_backgrounds[BgName::bg4NoPriority], {0, 15}); - this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority], {10, 16}); + this->addToMainSubScreen<0, 15>(this->_backgrounds[BgName::bg4NoPriority]); + this->addToMainSubScreen<10, 16>(this->_backgrounds[BgName::bg3NoPriority]); //sprites priority 0 // this->addToMainSubScreen(this->_backgrounds[BgName::bg4Priority]); // this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); //sprites priority 1 - this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority], {20, 35}); - this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority], {30, 36}); + this->addToMainSubScreen<20, 35>(this->_backgrounds[BgName::bg2NoPriority]); + this->addToMainSubScreen<30, 36>(this->_backgrounds[BgName::bg1NoPriority]); //sprites priority 2 // this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); // this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); //sprites priority 3 break; case 1: - this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority], {0, this->_registers._bgmode.mode1Bg3PriorityBit ? 30 : 5}); + if (!this->_registers._bgmode.mode1Bg3PriorityBit) + this->addToMainSubScreen<0, 5>(this->_backgrounds[BgName::bg3NoPriority]); + else + this->addToMainSubScreen<0, 30>(this->_backgrounds[BgName::bg3NoPriority]); //sprites priority 0 // if (!this->_registers._bgmode.mode1Bg3PriorityBit) // this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); //sprites priority 1 - this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority], {10, 25}); - this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority], {20, 26}); + this->addToMainSubScreen<10, 25>(this->_backgrounds[BgName::bg2NoPriority]); + this->addToMainSubScreen<20, 26>(this->_backgrounds[BgName::bg1NoPriority]); //sprites priority 2 // this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); // this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); @@ -654,16 +658,6 @@ namespace ComSquare::PPU } } - void PPU::addToMainSubScreen(Background &bg, const Vector2 &level) - { - if (this->_registers._t[0].raw & (1U << (bg.getBgNumber() - 1U))) { - Background::mergeBackgroundBuffer(this->_mainScreen, this->_mainScreenLevelMap, bg, level.x, level.y); - } - if (this->_registers._t[1].raw & (1U << (bg.getBgNumber() - 1U))) { - Background::mergeBackgroundBuffer(this->_subScreen, this->_subScreenLevelMap, bg, level.x, level.y); - } - } - int PPU::getBgMode() const { return this->_registers._bgmode.bgMode; diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index fbbdcc3..1639cd1 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -624,7 +624,16 @@ namespace ComSquare::PPU //! @brief Render the Main and sub screen correctly void renderMainAndSubScreen(); //! @brief Add a bg to the sub and/or main screen - void addToMainSubScreen(Background &bg, const Vector2 &level); + template + void addToMainSubScreen(Background &bg) + { + if (this->_registers._t[0].raw & (1U << (bg.getBgNumber() - 1U))) { + Background::mergeBackgroundBuffer(this->_mainScreen, this->_mainScreenLevelMap, bg); + } + if (this->_registers._t[1].raw & (1U << (bg.getBgNumber() - 1U))) { + Background::mergeBackgroundBuffer(this->_subScreen, this->_subScreenLevelMap, bg); + } + } //! @brief Get the current background Mode [[nodiscard]] int getBgMode() const; //! @brief update the Vram buffer diff --git a/tests/PPU/testTileRenderer.cpp b/tests/PPU/testTileRenderer.cpp index b102f5c..85df464 100644 --- a/tests/PPU/testTileRenderer.cpp +++ b/tests/PPU/testTileRenderer.cpp @@ -646,6 +646,7 @@ TEST_CASE("render 8bpp", "[PPU][TileRenderer]") ComSquare::Ram::Ram vram(100, static_cast(0), "vramTest"); ComSquare::Ram::Ram cgram(512, static_cast(0), "cgramTest"); ComSquare::PPU::TileRenderer tileRenderer(vram, cgram); + std::srand(std::time(nullptr)); tileRenderer.setBpp(8); tileRenderer.setPaletteIndex(0);