From ce4f2315835d7db3a5e168939268eb6d69d3fe45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 25 May 2020 23:56:18 +0200 Subject: [PATCH] starting the renderMainandSubScreen fct --- sources/PPU/Background.cpp | 10 +++++--- sources/PPU/Background.hpp | 7 +++--- sources/PPU/PPU.cpp | 51 +++++++++++++++++++++++++++++++++++--- sources/PPU/PPU.hpp | 18 ++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 48922b1..6660f54 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -9,7 +9,8 @@ namespace ComSquare::PPU { Background::Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority): - _priority(priority) + priority(priority), + bgNumber(bgNumber) { _cgram = _ppu.cgram; _vram = _ppu.vram; @@ -27,8 +28,8 @@ namespace ComSquare::PPU { uint16_t vramAddress = this->_TileMapStartAddress; Vector2 offset(0, 0); - this->_backgroundSize.x = this->_tileMaps.x * this->_characterSize.x * 32; - this->_backgroundSize.y = this->_tileMaps.y * this->_characterSize.y * 32; + this->backgroundSize.x = this->_tileMaps.x * this->_characterSize.x * 32; + this->backgroundSize.y = this->_tileMaps.y * this->_characterSize.y * 32; for (int i = 0; i < 4; i++) { if (!(i == 1 && this->_tileMaps.x == 1) && !(i > 1 && this->_tileMaps.y == 1)) { @@ -59,7 +60,8 @@ namespace ComSquare::PPU palette = getPalette(tileData.palette); reference = getTilePixelReference(graphicAddress, index); color = getRealColor(palette[reference]); - this->_buffer[pos.x][pos.y] = color; + if (tileData.tilePriority == this->priority) + this->buffer[pos.x][pos.y] = color; index++; pos.x++; if (index == (8 / this->_bpp) - 1) { diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index fc663f0..d86bcef 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -22,7 +22,6 @@ namespace ComSquare::PPU int _bpp; bool _directColor; bool _highRes; - bool _priority; uint16_t _TileMapStartAddress; uint16_t _tileSetAddress; @@ -39,8 +38,10 @@ namespace ComSquare::PPU //! @brief draw a tilemap 32x32 starting at baseAddress void drawBasicTileMap(uint16_t baseAddress, Vector2 offset); public: - Vector2 _backgroundSize; - std::array, 1024> _buffer; + bool priority; + int bgNumber; + Vector2 backgroundSize; + std::array, 1024> buffer; Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority); //! @brief Render a background on the screen void renderBackground(void); diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 1b7cb0d..3c5d9eb 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -253,9 +253,9 @@ namespace ComSquare::PPU (void)cycles; this->_backgrounds[0].renderBackground(); - for (int i = 0; i < this->_backgrounds[0]._backgroundSize.y; i++) { - for (int j = 0; j < this->_backgrounds[0]._backgroundSize.x; j++) { - this->_renderer.putPixel(j, i, this->_backgrounds[0]._buffer[i][j]); + for (int i = 0; i < this->_backgrounds[0].backgroundSize.y; i++) { + for (int j = 0; j < this->_backgrounds[0].backgroundSize.x; j++) { + this->_renderer.putPixel(j, i, this->_backgrounds[0].buffer[i][j]); } } this->_renderer.drawScreen(); @@ -483,4 +483,49 @@ namespace ComSquare::PPU backgroundSize.x = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 2 : 1; return backgroundSize; } + + void PPU::renderMainAndSubScreen(void) + { + for (auto & _background : this->_backgrounds) + _background.renderBackground(); + + // the buffer is overwrite if necessery by a new bg so the background priority is from back to front + switch (this->_registers._bgmode.bgMode) { + case 0: + 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]); + //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]); + //sprites priority 3 + break; + } + } + + template + void PPU::add_buffer(std::array, SIZE> &buffer, Background &bg) + { + for (int i = 0; i < bg.backgroundSize.y; i++) { + for (int j = 0; j < bg.backgroundSize.x; j++) { + if (bg.buffer[i][j]) + buffer[i][j] = bg.buffer[i][j]; + } + } + } + + void PPU::addToMainSubScreen(Background &bg) + { + int i = bg.bgNumber + bg.priority; + + if (this->_registers._t[0].raw & (1U << bg.bgNumber - 1U)) + this->add_buffer(this->_mainScreen, this->_backgrounds[i]); + if (this->_registers._t[1].raw & (1U << bg.bgNumber - 1U)) + this->add_buffer(this->_subScreen, this->_backgrounds[i]); + } } \ No newline at end of file diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 8ad927b..ea4e174 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -21,6 +21,17 @@ namespace ComSquare::PPU { class Background; + enum bgName { + bg1NoPriority = 0, + bg1Priority, + bg2NoPriority, + bg2Priority, + bg3NoPriority, + bg3Priority, + bg4NoPriority, + bg4Priority + }; + enum ppuRegisters { //! @brief INIDISP Register (F-blank and Brightness) inidisp = 0x00, @@ -605,6 +616,13 @@ namespace ComSquare::PPU uint16_t getTileSetAddress(int bgNumber); //! @brief Give the number of tilemaps to be rendered Vector2 getBackgroundSize(int bgNumber); + //! @brief Render the Main and sub screen correctly + void renderMainAndSubScreen(void); + //! @brief Add a bg buffer to another buffer + template + void PPU::add_buffer(std::array, SIZE> &buffer, Background &bg); + //! @brief Add a bg to the sub and/or main screen + void addToMainSubScreen(Background &bg); }; } #endif //COMSQUARE_PPU_HPP