From 3e73370a9a55a058a1cad927c86f10c434d0fd7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 7 Jul 2021 00:40:50 +0200 Subject: [PATCH] try to make priority work but not yet functionnal --- sources/PPU/Background.hpp | 31 +++++++++++++++++++++++++++++ sources/PPU/PPU.cpp | 40 ++++++++++++++++++++++++-------------- sources/PPU/PPU.hpp | 4 +++- sources/PPU/PPUUtils.hpp | 1 - 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index ca62f38..c7ad7f6 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -107,6 +107,37 @@ namespace ComSquare::PPU //! @return the current Background priority bool getPriority() const; + + //! @brief Add a bg buffer to another buffer + template + static void mergeBackgroundBuffer(std::array, DEST_SIZE_X> &bufferDest, + std::array, DEST_SIZE_X> &pixelDestinationLevelMap, + const Background &backgroundSrc, + int levelLow, + int levelHigh) + { + int i = 0; + int j = 0; + int pixelLevel; + std::for_each(backgroundSrc.buffer.begin(), backgroundSrc.buffer.end(), [&](auto &sourceRow) { + std::for_each(sourceRow.begin(), sourceRow.end(), [&](auto &pixel) { + if (pixel <= 0xFF) { + j++; + return; + } + pixelLevel = backgroundSrc.isPriorityPixel(i, j) ? levelHigh : levelLow; + + if (pixelLevel > pixelDestinationLevelMap[i][j]) { + bufferDest[i][j] = pixel; + pixelDestinationLevelMap[i][j] = pixelLevel; + } + j++; + }); + j = 0; + i++; + }); + } + //! @brief ctor Background(ComSquare::PPU::PPU &_ppu, int backGroundNumber, bool hasPriority); //! @brief Default copy ctor diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 46c86da..6cc0f65 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -317,8 +317,10 @@ namespace ComSquare::PPU //add_buffer(this->_screen, this->_backgrounds[2].buffer); for (unsigned long i = 0; i < this->_screen.size(); i++) { for (unsigned long j = 0; j < this->_screen[i].size(); j++) { - this->_renderer.putPixel(i, j, this->_screen[i][j]); + this->_renderer.putPixel(i + 200, j, this->_screen[i][j]); } + if (i > 500) + break; } this->_renderer.drawScreen(); for (auto &i : this->_mainScreen) @@ -563,31 +565,35 @@ namespace ComSquare::PPU uint32_t color = Utils::getRealColor(colorPalette); for (auto &row : this->_subScreen) row.fill(color); + for (auto &row : this->_mainScreenLevelMap) + row.fill(0); + for (auto &row : this->_subScreenLevelMap) + row.fill(0); // the buffer is overwrite if necessary by a new bg so the background priority is from back to front // the starting palette index isn't implemented switch (this->_registers._bgmode.bgMode) { case 0: - this->addToMainSubScreen(this->_backgrounds[BgName::bg4NoPriority]); - this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority]); + this->addToMainSubScreen(this->_backgrounds[BgName::bg4NoPriority], {0, 15}); + this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority], {10, 16}); //sprites priority 0 // 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]); + this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority], {20, 35}); + this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority], {30, 36}); //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]); + this->addToMainSubScreen(this->_backgrounds[BgName::bg3NoPriority], {0, this->_registers._bgmode.mode1Bg3PriorityBit ? 30 : 5}); //sprites priority 0 // 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]); + this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority], {10, 25}); + this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority], {20, 26}); //sprites priority 2 // this->addToMainSubScreen(this->_backgrounds[BgName::bg2Priority]); // this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); @@ -595,7 +601,7 @@ namespace ComSquare::PPU // if (this->_registers._bgmode.mode1Bg3PriorityBit) // this->addToMainSubScreen(this->_backgrounds[BgName::bg3Priority]); break; - case 2: + /* case 2: this->addToMainSubScreen(this->_backgrounds[BgName::bg2NoPriority]); //sprites priority 0 this->addToMainSubScreen(this->_backgrounds[BgName::bg1NoPriority]); @@ -642,7 +648,7 @@ namespace ComSquare::PPU //sprites priority 2 this->addToMainSubScreen(this->_backgrounds[BgName::bg1Priority]); //sprites priority - break; + break;*/ case 7: // Not implemented throw std::runtime_error("not implemented"); @@ -651,12 +657,16 @@ namespace ComSquare::PPU } } - void PPU::addToMainSubScreen(Background &bg) + void PPU::addToMainSubScreen(Background &bg, const Vector2 &level) { - if (this->_registers._t[0].raw & (1U << (bg.getBgNumber() - 1U))) - Utils::addBuffer(this->_mainScreen, bg.buffer); - if (this->_registers._t[1].raw & (1U << (bg.getBgNumber() - 1U))) - Utils::addBuffer(this->_subScreen, bg.buffer); + if (this->_registers._t[0].raw & (1U << (bg.getBgNumber() - 1U))) { + Background::mergeBackgroundBuffer(this->_mainScreen, this->_mainScreenLevelMap, bg, level.x, level.y); + } + // Utils::addBuffer(this->_mainScreen, bg.buffer); + if (this->_registers._t[1].raw & (1U << (bg.getBgNumber() - 1U))) { + //Utils::addBuffer(this->_subScreen, bg.buffer); + Background::mergeBackgroundBuffer(this->_subScreen, this->_subScreenLevelMap, bg, level.x, level.y); + } } int PPU::getBgMode() const diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index f0b4ebf..02ea9a9 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -567,7 +567,9 @@ namespace ComSquare::PPU Background _backgrounds[4]; //! @brief Main Screen buffer std::array, 1024> _mainScreen; + std::array, 1024> _mainScreenLevelMap; //! @brief Sub Screen buffer + std::array, 1024> _subScreenLevelMap; std::array, 1024> _subScreen; //! @brief Final Screen buffer std::array, 1024> _screen; @@ -625,7 +627,7 @@ 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); + void addToMainSubScreen(Background &bg, const Vector2 &level); //! @brief Get the current background Mode int getBgMode() const; //! @brief update the Vram buffer diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp index 4faa8c2..207062e 100644 --- a/sources/PPU/PPUUtils.hpp +++ b/sources/PPU/PPUUtils.hpp @@ -94,6 +94,5 @@ namespace ComSquare::PPU::Utils i++; }); } - } #endif //COMSQUARE_PPU_UTILS_HPP \ No newline at end of file