diff --git a/sources/Debugger/TileViewer/RAMTileRenderer.cpp b/sources/Debugger/TileViewer/RAMTileRenderer.cpp index 96c9a2f..18c79ea 100644 --- a/sources/Debugger/TileViewer/RAMTileRenderer.cpp +++ b/sources/Debugger/TileViewer/RAMTileRenderer.cpp @@ -33,7 +33,7 @@ namespace ComSquare::Debugger int bufY = 0; int nbTilesDrawn = 0; int resetX = bufX; - for (auto &i : buffer) + for (auto &i : this->buffer) i.fill(0); uint24_t limit = fmin(this->_ram->getSize(), this->_renderSize) + this->_ramOffset; diff --git a/sources/Debugger/TileViewer/TileViewer.cpp b/sources/Debugger/TileViewer/TileViewer.cpp index 2507892..38c08e6 100644 --- a/sources/Debugger/TileViewer/TileViewer.cpp +++ b/sources/Debugger/TileViewer/TileViewer.cpp @@ -142,8 +142,7 @@ namespace ComSquare::Debugger case 1: return this->setBpp(4); case 2: return this->setBpp(8); default: - break; + throw std::runtime_error("Invalid Index"); } - // TODO error handling } } \ No newline at end of file diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 8e20124..dc90389 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -54,7 +54,7 @@ namespace ComSquare::PPU void Background::_drawBgTile(uint16_t data, Vector2 pos) { - union TileMapData tileData; + union Utils::TileMapData tileData; tileData.raw = data; @@ -73,7 +73,7 @@ namespace ComSquare::PPU ((tileData.posY + tileOffset.y) * NbTilePerRow * this->_bpp * Tile::BaseByteSize) + ((tileData.posX + tileOffset.x) * this->_bpp * Tile::BaseByteSize); this->_tileRenderer.render(graphicAddress); - merge2DArray(this->_tileBuffer, this->_tileRenderer.buffer, {j, i}); + Utils::merge2DArray(this->_tileBuffer, this->_tileRenderer.buffer, {j, i}); tileOffset.x += 1; } tileOffset.x = 0; @@ -82,9 +82,9 @@ namespace ComSquare::PPU // todo check why i need to invert vertical and horizontal flips if (tileData.verticalFlip) - HFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y}); + Utils::HFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y}); if (tileData.horizontalFlip) - VFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y}); + Utils::VFlipArray(this->_tileBuffer, {this->_characterNbPixels.x, this->_characterNbPixels.y}); for (int i = 0; i < this->_characterNbPixels.y; i++) { for (int j = 0; j < this->_characterNbPixels.x; j++) { this->buffer[pos.x][pos.y] = this->_tileBuffer[i][j]; diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 9857756..194b50b 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -9,15 +9,13 @@ #include "Exceptions/InvalidAddress.hpp" #include "Ram/Ram.hpp" #include "Models/Vector2.hpp" -#include "Debugger/TileViewer/RAMTileRenderer.hpp" -#include namespace ComSquare::PPU { PPU::PPU(Renderer::IRenderer &renderer): - vram(new Ram::Ram(VRAMSIZE, ComSquare::VRam, "VRAM")), - oamram(new Ram::Ram(OAMRAMSIZE, ComSquare::OAMRam, "OAMRAM")), - cgram(new Ram::Ram(CGRAMSIZE, ComSquare::CGRam, "CGRAM")), + vram(new Ram::Ram(VramSize, ComSquare::VRam, "VRAM")), + oamram(new Ram::Ram(OAMRamSize, ComSquare::OAMRam, "OAMRAM")), + cgram(new Ram::Ram(CGRamSize, ComSquare::CGRam, "CGRAM")), _renderer(renderer), _backgrounds{ Background(*this, 1, false), @@ -721,9 +719,9 @@ namespace ComSquare::PPU colorPalette = this->cgram->read(0); colorPalette += this->cgram->read(1) << 8U; - for (unsigned long i = 0; i < this->_subScreen.size(); i++) - for (unsigned long j = 0; j < this->_subScreen[i].size(); j++) - this->_subScreen[i][j] = getRealColor(colorPalette); + uint32_t color = Utils::getRealColor(colorPalette); + for (auto &row : this->_subScreen) + row.fill(color); // 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) { @@ -812,19 +810,6 @@ namespace ComSquare::PPU } } - template - void PPU::add_buffer(std::array, DEST_SIZE_X> &bufferDest, - const std::array, SRC_SIZE_X> &bufferSrc, - const Vector2 &offset) - { - for (unsigned long i = 0; i < bufferSrc.size(); i++) { - for (unsigned long j = 0; j < bufferSrc[i].size(); j++) { - if (bufferSrc[i][j] > 0xFF) // 0xFF correspond to a black pixel with full brightness - bufferDest[i + offset.x ][j + offset.y] = bufferSrc[i][j]; - } - } - } - void PPU::addToMainSubScreen(Background &bg) { if (this->_registers._t[0].raw & (1U << (bg.getBgNumber() - 1U))) diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 7a3be53..f10b1ec 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -17,13 +17,13 @@ #define FALLTHROUGH __attribute__((fallthrough)); -// TODO check if it useful to have defines instead of constexpr -#define VRAMSIZE 65536 -#define CGRAMSIZE 512 -#define OAMRAMSIZE 544 - namespace ComSquare::PPU { + static constexpr uint32_t VramSize = 65536; + static constexpr uint32_t CGRamSize = 512; + static constexpr uint32_t OAMRamSize = 544; + + class Background; //! @brief Enum to access more easily the ppu background array enum BgName { @@ -569,7 +569,7 @@ namespace ComSquare::PPU //! @brief Used for vram read registers (0x2139 - 0x213A) uint16_t _vramReadBuffer = 0; //! @brief Struct that contain all necessary vars for the use of the registers - struct PpuState _ppuState; + struct Utils::PpuState _ppuState; public: @@ -622,8 +622,17 @@ namespace ComSquare::PPU //! @brief Add a bg buffer to another buffer template void add_buffer(std::array, DEST_SIZE_X> &bufferDest, - const std::array, SRC_SIZE_X> &bufferSrc, - const Vector2 &offset = {0, 0}); + const std::array, SRC_SIZE_X> &bufferSrc, + const Vector2 &offset = {0, 0}) + { + // TODO use std::ranges + for (unsigned long i = 0; i < bufferSrc.size(); i++) { + for (unsigned long j = 0; j < bufferSrc[i].size(); j++) { + if (bufferSrc[i][j] > 0xFF) // 0xFF correspond to a black pixel with full brightness + bufferDest[i + offset.x ][j + offset.y] = bufferSrc[i][j]; + } + } + } //! @brief Add a bg to the sub and/or main screen void addToMainSubScreen(Background &bg); //! @brief Get the current background Mode @@ -650,8 +659,6 @@ namespace ComSquare::PPU } }; - //! @brief Transform SNES color code BGR to uint32_t RGB - uint32_t getRealColor(uint16_t color); int *get_dump_vram(); int *get_dump_cgram(); } diff --git a/sources/PPU/PPUUtils.cpp b/sources/PPU/PPUUtils.cpp index 8a1c5d8..f23a72d 100644 --- a/sources/PPU/PPUUtils.cpp +++ b/sources/PPU/PPUUtils.cpp @@ -4,7 +4,7 @@ #include -namespace ComSquare::PPU +namespace ComSquare::PPU::Utils { uint32_t getRealColor(uint16_t color) diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp index 1cdb0a7..2314ea5 100644 --- a/sources/PPU/PPUUtils.hpp +++ b/sources/PPU/PPUUtils.hpp @@ -8,9 +8,9 @@ #include #include #include -#include "Models//Vector2.hpp" +#include "Models/Vector2.hpp" -namespace ComSquare::PPU +namespace ComSquare::PPU::Utils { //! @brief Transform SNES color code BGR to uint32_t RGB diff --git a/sources/PPU/TileRenderer.cpp b/sources/PPU/TileRenderer.cpp index 348049d..28074de 100644 --- a/sources/PPU/TileRenderer.cpp +++ b/sources/PPU/TileRenderer.cpp @@ -34,7 +34,7 @@ namespace ComSquare::PPU for (auto &row : this->buffer) { for (auto &pixel : row) { uint8_t pixelReference = this->getPixelReferenceFromTile(tileAddress, it++); - pixel = pixelReference ? getRealColor(palette[pixelReference]) : 0; + pixel = pixelReference ? Utils::getRealColor(palette[pixelReference]) : 0; } } } diff --git a/sources/SNES.cpp b/sources/SNES.cpp index 51c8bd6..403e78a 100644 --- a/sources/SNES.cpp +++ b/sources/SNES.cpp @@ -185,17 +185,17 @@ namespace ComSquare void SNES::disableTileViewerDebugging() { #ifdef DEBUGGER_ENABLED - this->_tileViewer = nullptr; + this->_tileViewer = nullptr; #endif } void SNES::enableTileViewerDebugging() { #ifdef DEBUGGER_ENABLED - if (this->_tileViewer) - this->_tileViewer->focus(); - else - this->_tileViewer = std::make_unique(*this, *this->ppu); + if (this->_tileViewer) + this->_tileViewer->focus(); + else + this->_tileViewer = std::make_unique(*this, *this->ppu); #endif } }