From 1553903eff19e59f06a95ec049569b766c70d4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 25 May 2020 17:14:11 +0200 Subject: [PATCH] making the constructor of background class much more easier to use --- sources/PPU/Background.cpp | 39 ++++++++++++---------- sources/PPU/Background.hpp | 15 +++++---- sources/PPU/PPU.cpp | 67 ++++++++++++++++++++++++++++++++++++++ sources/PPU/PPU.hpp | 12 ++++++- 4 files changed, 108 insertions(+), 25 deletions(-) diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index d66301e..723a3b4 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -4,25 +4,28 @@ #include "Background.hpp" #include "PPUUtils.hpp" +#include "PPU.hpp" namespace ComSquare::PPU { - Background::Background(int bpp, Vector2 backgroundSize, Vector2 characterSize, bool directColor, bool highRes, bool priority, uint16_t vramAddress, uint16_t graphicVramAddress): - _backgroundSize(backgroundSize), - _characterSize(characterSize), - _bpp(bpp), - _directColor(directColor), - _highRes(highRes), - _priority(priority), - _vramAddress(vramAddress), - _graphicVramAddress(graphicVramAddress) + Background::Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority): + _priority(priority) { + _cgram = _ppu.cgram; + _vram = _ppu.vram; + _bpp = _ppu.getBPP(bgNumber); + _characterSize = _ppu.getCharacterSize(bgNumber); + _TileMapStartAddress = _ppu.getTileMapStartAddress(bgNumber); + _tileSetAddress = _ppu.getTileSetAddress(bgNumber); + _backgroundSize = _ppu.getBackgroundSize(bgNumber); + _directColor = false; + _highRes = false; } - void Background::renderBackground(void) + std::array, 1024> Background::renderBackground(void) { - uint16_t vramAddress = this->_vramAddress; + uint16_t vramAddress = this->_TileMapStartAddress; Vector2 offset(0, 0); for (int i = 0; i < 4; i++) { @@ -48,7 +51,7 @@ namespace ComSquare::PPU uint32_t color = 0; tileData.raw = data; - graphicAddress = this->_graphicVramAddress; + graphicAddress = this->_tileSetAddress + (tileData.posX * 16 * this->_bpp * 8) + (tileData.posY * this->_bpp * 8); for (int i = 0; i < this->_characterSize.y; i++) { for (int j = 0; j < this->_characterSize.x; j++) { palette = getPalette(tileData.palette); @@ -74,8 +77,8 @@ namespace ComSquare::PPU uint16_t addr = nbPalette * 0x10; for (int i = 0; i < 0xF; i++) { - palette[i] = this->cgram->read_internal(addr); - palette[i] += this->cgram->read_internal(addr + 1) << 8U; + palette[i] = this->_cgram->read_internal(addr); + palette[i] += this->_cgram->read_internal(addr + 1) << 8U; } return palette; } @@ -100,7 +103,7 @@ namespace ComSquare::PPU uint8_t Background::getTilePixelReference(uint16_t addr, int nb) { - uint8_t reference = this->vram->read_internal(addr); + uint8_t reference = this->_vram->read_internal(addr); switch (this->_bpp) { case 8: @@ -122,8 +125,8 @@ namespace ComSquare::PPU uint16_t vramAddress = baseAddress; while (vramAddress < 0x800 + baseAddress) { - tileMapValue = this->vram->read_internal(vramAddress); - tileMapValue += this->vram->read_internal(vramAddress + 1) << 8U; + tileMapValue = this->_vram->read_internal(vramAddress); + tileMapValue += this->_vram->read_internal(vramAddress + 1) << 8U; vramAddress += 2; drawBgTile(tileMapValue, {(pos.x * this->_characterSize.x) + offset.x, (pos.y * this->_characterSize.y) + offset.y}); if (pos.x % 31 == 0 && pos.x) { @@ -134,4 +137,6 @@ namespace ComSquare::PPU pos.x++; } } + + } \ No newline at end of file diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index a042ac4..b379f51 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -10,6 +10,7 @@ #include #include "../Models/Vector2.hpp" #include "../Ram/Ram.hpp" +#include "PPU.hpp" namespace ComSquare::PPU { @@ -21,12 +22,12 @@ namespace ComSquare::PPU bool _directColor; bool _highRes; bool _priority; - uint16_t _vramAddress; - uint16_t _graphicVramAddress; + uint16_t _TileMapStartAddress; + uint16_t _tileSetAddress; std::array, 1024> _buffer; - std::shared_ptr vram; - std::shared_ptr cgram; + std::shared_ptr _vram; + std::shared_ptr _cgram; //! @brief Draw a tile on the screen at x y pos void drawBgTile(uint16_t data, Vector2 pos); //! @brief Get a palette from the number of the palette (0 - 7) @@ -38,11 +39,11 @@ namespace ComSquare::PPU //! @brief draw a tilemap 32x32 starting at baseAddress void drawBasicTileMap(uint16_t baseAddress, Vector2 offset); public: - Background(int bpp, Vector2 backgroundSize, Vector2 characterSize, bool directColor, bool highRes, bool priority, uint16_t vramAddress, uint16_t graphicVramAddress); + Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority); //! @brief Render a background on the screen - void renderBackground(void); + std::array, 1024> renderBackground(void); }; } -#endif //COMSQUARE_BACKGROUND_HPP +#endif //COMSQUARE_BACKGROUND_HPP \ No newline at end of file diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 4fd6a88..cc4bdca 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -422,4 +422,71 @@ namespace ComSquare::PPU { return this->cgram->read_internal(addr); } + + int PPU::getBPP(int bgNumber) + { + switch (this->_registers._bgmode.bgMode) { + case 0: + return (2); + case 1: + if (bgNumber < 3) + return (4); + return (2); + case 2: + return (4); + case 3: + if (bgNumber == 1) + return (8); + return (4); + case 4: + if (bgNumber == 1) + return (8); + return (2); + case 5: + if (bgNumber == 1) + return (4); + return (2); + case 6: + return (4); + case 7: + if (bgNumber == 1) + return (8); + return (7); + default: + return (-1); + } + } + + Vector2 PPU::getCharacterSize(int bgNumber) + { + Vector2 characterSize(8, 8); + + //this wont work for modes 5 and 6 and will be reworked + if (this->_registers._bgmode.raw & (1U << (4 + bgNumber))) + characterSize = {16, 16}; + return characterSize; + } + + uint16_t PPU::getTileMapStartAddress(int bgNumber) + { + return this->_registers._bgsc[bgNumber - 1].tilemapAddress << 1U; + } + + uint16_t PPU::getTileSetAddress(int bgNumber) + { + uint16_t baseAddress = this->_registers._bgnba[bgNumber > 2].raw; + + baseAddress = (bgNumber % 2) ? baseAddress & 0xFU : (baseAddress & 0xFU) >> 4U; + baseAddress = baseAddress << 12U; + return baseAddress; + } + + Vector2 PPU::getBackgroundSize(int bgNumber) + { + Vector2 backgroundSize(0,0); + + backgroundSize.y = (this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring) ? 2 : 1; + backgroundSize.x = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 2 : 1; + return backgroundSize; + } } \ No newline at end of file diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 38aceaf..7cbe6d1 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -583,7 +583,7 @@ namespace ComSquare::PPU std::string getValueName(uint24_t addr); //! @brief Return true if the CPU is overloaded with debugging features. virtual bool isDebugger(); - //! @brief Allow others components to read the CGRAM (Debuggers) + //! @brief Allow others components to read the CGRAM uint16_t cgramRead(uint16_t addr); //! @brief Render a background on the screen void renderBackground(int bgNumber, Vector2 characterSize, int bpp, bool priority); @@ -599,6 +599,16 @@ namespace ComSquare::PPU uint8_t getTilePixelReference(uint16_t addr, int bpp, int nb); //! @brief draw a tilemap 32x32 starting at baseAddress void drawBasicTileMap(uint16_t baseAddress, int bgNumber, int bpp, Vector2 characterSize, Vector2 offset); + //! @brief get the bpp depending of the bgNumber and the Bgmode + int getBPP(int bgNumber); + //! @brief Give the correct character size depending of the bgMode + Vector2 getCharacterSize(int bgNumber); + //! @brief Give the address where the tilemap starts + uint16_t getTileMapStartAddress(int bgNumber); + //! @brief Give the address to find the correct tileset for a given x and y + uint16_t getTileSetAddress(int bgNumber); + //! @brief Give the number of tilemaps to be rendered + Vector2 getBackgroundSize(int bgNumber); }; } #endif //COMSQUARE_PPU_HPP