From 8a40625dea9575fb995275483b63c3ac88d61a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 25 May 2020 19:33:10 +0200 Subject: [PATCH] Background run by class is working --- CMakeLists.txt | 2 - sources/PPU/Background.cpp | 12 +-- sources/PPU/Background.hpp | 8 +- sources/PPU/Backgrounds.cpp | 143 ------------------------------------ sources/PPU/PPU.cpp | 21 +++++- sources/PPU/PPU.hpp | 20 +++-- sources/PPU/PPUUtils.hpp | 2 +- 7 files changed, 46 insertions(+), 162 deletions(-) delete mode 100644 sources/PPU/Backgrounds.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0624057..6f593e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,6 @@ add_executable(unit_tests tests/CPU/Math/testOthersMath.cpp tests/testRectangleMemory.cpp tests/CPU/Math/testCMP.cpp - sources/PPU/Backgrounds.cpp sources/PPU/Background.cpp sources/PPU/Background.hpp) # include criterion & coverage @@ -214,7 +213,6 @@ add_executable(ComSquare sources/Debugger/CGramDebug.cpp sources/Debugger/CGramDebug.hpp sources/Models/Vector2.hpp - sources/PPU/Backgrounds.cpp sources/PPU/Background.cpp sources/PPU/Background.hpp) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) diff --git a/sources/PPU/Background.cpp b/sources/PPU/Background.cpp index 723a3b4..48922b1 100644 --- a/sources/PPU/Background.cpp +++ b/sources/PPU/Background.cpp @@ -2,9 +2,9 @@ // Created by cbihan on 5/14/20. // -#include "Background.hpp" #include "PPUUtils.hpp" #include "PPU.hpp" +#include "Background.hpp" namespace ComSquare::PPU { @@ -17,19 +17,21 @@ namespace ComSquare::PPU _characterSize = _ppu.getCharacterSize(bgNumber); _TileMapStartAddress = _ppu.getTileMapStartAddress(bgNumber); _tileSetAddress = _ppu.getTileSetAddress(bgNumber); - _backgroundSize = _ppu.getBackgroundSize(bgNumber); + _tileMaps = _ppu.getBackgroundSize(bgNumber); _directColor = false; _highRes = false; } - std::array, 1024> Background::renderBackground(void) + void Background::renderBackground(void) { 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; for (int i = 0; i < 4; i++) { - if (!(i == 1 && this->_backgroundSize.x == 1) && !(i > 1 && this->_backgroundSize.y == 1)) { + if (!(i == 1 && this->_tileMaps.x == 1) && !(i > 1 && this->_tileMaps.y == 1)) { drawBasicTileMap(vramAddress, offset); } vramAddress+= 0x800; @@ -94,7 +96,7 @@ namespace ComSquare::PPU green = (color & 0x03E0U) >> 5U; red = (color & 0x001FU); - pixelTmp = 0xFFFF; + pixelTmp = 0xFF; pixelTmp += (red * 255U / 31U) << 24U; pixelTmp += (green * 255U / 31U) << 16U; pixelTmp += (blue * 255U / 31U) << 8U; diff --git a/sources/PPU/Background.hpp b/sources/PPU/Background.hpp index b379f51..fc663f0 100644 --- a/sources/PPU/Background.hpp +++ b/sources/PPU/Background.hpp @@ -14,9 +14,10 @@ namespace ComSquare::PPU { + class PPU; class Background { private: - Vector2 _backgroundSize; + Vector2 _tileMaps; Vector2 _characterSize; int _bpp; bool _directColor; @@ -24,7 +25,6 @@ namespace ComSquare::PPU bool _priority; uint16_t _TileMapStartAddress; uint16_t _tileSetAddress; - std::array, 1024> _buffer; std::shared_ptr _vram; std::shared_ptr _cgram; @@ -39,9 +39,11 @@ 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; Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority); //! @brief Render a background on the screen - std::array, 1024> renderBackground(void); + void renderBackground(void); }; } diff --git a/sources/PPU/Backgrounds.cpp b/sources/PPU/Backgrounds.cpp deleted file mode 100644 index c7d01dc..0000000 --- a/sources/PPU/Backgrounds.cpp +++ /dev/null @@ -1,143 +0,0 @@ -// -// Created by cbihan on 5/14/20. -// - -#include -#include -#include "PPU.hpp" -#include "PPUUtils.hpp" -#include "../Exceptions/NotImplementedException.hpp" -#include "../Exceptions/InvalidAddress.hpp" -#include "../Ram/Ram.hpp" -#include "../Models/Vector2.hpp" - -namespace ComSquare::PPU -{ - void PPU::renderBackground(int bgNumber, Vector2 characterSize, int bpp, bool priority) - { - (void)priority; - int nbBgHeight = (this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring) ? 2 : 1; - int nbBgWidth = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 2 : 1; - uint16_t vramAddress = this->_registers._bgsc[bgNumber - 1].tilemapAddress << 1U; - Vector2 offset(0, 0); - - for (int i = 0; i < 4; i++) { - if (!(i == 1 && nbBgWidth == 1) && !(i > 1 && nbBgHeight == 1)) { - drawBasicTileMap(vramAddress, bgNumber, bpp, characterSize, offset); - } - vramAddress+= 0x800; - offset.x += 32 * characterSize.x; - if (i == 2) { - offset.x = 0; - offset.y += 32 * characterSize.y; - } - } - } - - uint16_t PPU::getGraphicVramAddress(int x, int y, int bg, int bpp) - { - uint16_t baseAddress = this->_registers._bgnba[bg > 2].raw; - int step = bpp * 8; - - baseAddress = (bg % 2) ? baseAddress & 0xFU : (baseAddress & 0xFU) >> 4U; - baseAddress = baseAddress << 12U; - return baseAddress + (x * 16 * step) + (y * step); - } - - void PPU::drawBgTile(uint16_t data, Vector2 pos, int bg, int bpp, Vector2 characterSize) - { - uint16_t graphicAddress; - union TileMapData tileData; - std::vector palette; - int index = 0; - uint8_t reference = 0; - uint32_t color = 0; - - tileData.raw = data; - graphicAddress = this->getGraphicVramAddress(tileData.posX, tileData.posY, bg, bpp); - for (int i = 0; i < characterSize.y; i++) { - for (int j = 0; j < characterSize.x; j++) { - palette = getPalette(tileData.palette); - reference = getTilePixelReference(graphicAddress, bpp, index); - color = getRealColor(palette[reference]); - this->_renderer.putPixel(pos.x, pos.y, color); - index++; - pos.x++; - if (index == (8 / bpp) - 1) { - index = 0; - graphicAddress++; - } - } - index = 0; - pos.x -= characterSize.x; - pos.y++; - } - } - - std::vector PPU::getPalette(int nbPalette) - { - std::vector palette(0xF); - - uint16_t addr = nbPalette * 0x10; - for (int i = 0; i < 0xF; i++) { - palette[i] = this->cgramRead(addr); - palette[i] += this->cgramRead(addr + 1) << 8U; - } - return palette; - } - - uint32_t PPU::getRealColor(uint16_t color) - { - uint8_t blue; - uint8_t red; - uint8_t green; - uint32_t pixelTmp; - - blue = (color & 0x7D00U) >> 10U; - green = (color & 0x03E0U) >> 5U; - red = (color & 0x001FU); - - pixelTmp = this->_registers._inidisp.brightness * 255U / 15U; - pixelTmp += (red * 255U / 31U) << 24U; - pixelTmp += (green * 255U / 31U) << 16U; - pixelTmp += (blue * 255U / 31U) << 8U; - return pixelTmp; - } - - uint8_t PPU::getTilePixelReference(uint16_t addr, int bpp, int nb) - { - uint8_t reference = this->vram->read_internal(addr); - - switch (bpp) { - case 8: - return reference; - case 4: - return (reference & (0xFU << ((1 - nb) * 4U))) >> (1 - nb) * 4U; - case 2: - return (reference & (0x3U << ((3 - nb) * 2U))) >> (3 - nb) * 2U; - default: - break; - } - return 0; - } - - void PPU::drawBasicTileMap(uint16_t baseAddress, int bgNumber, int bpp, Vector2 characterSize, Vector2 offset) - { - uint16_t tileMapValue = 0; - Vector2 pos(0,0); - uint16_t vramAddress = baseAddress; - - while (vramAddress < 0x800 + baseAddress) { - tileMapValue = this->vram->read_internal(vramAddress); - tileMapValue += this->vram->read_internal(vramAddress + 1) << 8U; - vramAddress += 2; - drawBgTile(tileMapValue, {(pos.x * characterSize.x) + offset.x, (pos.y * characterSize.y) + offset.y}, bgNumber, bpp, characterSize); - if (pos.x % 31 == 0 && pos.x) { - pos.y++; - pos.x = 0; - } - else - pos.x++; - } - } -} \ No newline at end of file diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index cc4bdca..f8a3e32 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -15,10 +15,20 @@ namespace ComSquare::PPU { PPU::PPU(Renderer::IRenderer &renderer): - _renderer(renderer), vram(new Ram::Ram(65536, ComSquare::VRam, "VRAM")), oamram(new Ram::Ram(544, ComSquare::OAMRam, "OAMRAM")), - cgram(new Ram::Ram(512, ComSquare::CGRam, "CGRAM")) + cgram(new Ram::Ram(512, ComSquare::CGRam, "CGRAM")), + _renderer(renderer), + _backgrounds{ + Background(*this, 1, false), + Background(*this, 1, true), + Background(*this, 2, false), + Background(*this, 2, true), + Background(*this, 3, false), + Background(*this, 3, true), + Background(*this, 4, false), + Background(*this, 4, true) + } { this->_registers._isLowByte = true; for (int i = 0; i < 512; i++) { @@ -263,7 +273,12 @@ namespace ComSquare::PPU this->_renderer.putPixel(x, y, pixelTmp); } } - this->renderBackground(1, {8, 8}, 4, false); + 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]); + } + } this->_renderer.drawScreen(); } diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index 7cbe6d1..9e519ad 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -12,6 +12,7 @@ //#include "../Ram/ExtendedRam.hpp" #include "../Ram/Ram.hpp" #include "../Models/Vector2.hpp" +#include "Background.hpp" //#define max2BitTiles 4096 //#define max4BitTiles 2048 @@ -19,6 +20,7 @@ namespace ComSquare::PPU { + class Background; enum ppuRegisters { //! @brief INIDISP Register (F-blank and Brightness) inidisp = 0x00, @@ -545,14 +547,22 @@ namespace ComSquare::PPU //! @brief The class containing all the registers of the PPU class PPU : public Memory::AMemory { + public: + //! @brief Rams + std::shared_ptr vram; + std::shared_ptr oamram; + std::shared_ptr cgram; private: //! @brief Init ppuRegisters Registers _registers{}; Renderer::IRenderer &_renderer; + //! @brief Backgrounds buffers + Background _backgrounds[8]; + //! @brief Main Screen buffer + std::array, 1024> _mainScreen; + //! @brief Sub Screen buffer + std::array, 1024> _subScreen; public: - std::shared_ptr vram; - std::shared_ptr oamram; - std::shared_ptr cgram; explicit PPU(Renderer::IRenderer &renderer); PPU(const PPU &) = delete; @@ -585,7 +595,7 @@ namespace ComSquare::PPU virtual bool isDebugger(); //! @brief Allow others components to read the CGRAM uint16_t cgramRead(uint16_t addr); - //! @brief Render a background on the screen + /* //! @brief Render a background on the screen void renderBackground(int bgNumber, Vector2 characterSize, int bpp, bool priority); //! @brief Get the correct Vram address for a gien x and y uint16_t getGraphicVramAddress(int x, int y, int bg, int bpp); @@ -598,7 +608,7 @@ namespace ComSquare::PPU //! @brief Get the color reference of a nb pixel tile 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); + 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 diff --git a/sources/PPU/PPUUtils.hpp b/sources/PPU/PPUUtils.hpp index 94db8d3..11f963e 100644 --- a/sources/PPU/PPUUtils.hpp +++ b/sources/PPU/PPUUtils.hpp @@ -5,7 +5,7 @@ #ifndef COMSQUARE_PPU_UTILS_HPP #define COMSQUARE_PPU_UTILS_HPP - +#include namespace ComSquare::PPU {