From fd9da7085bf66cd2fc18e5878b1e0d2baee80ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 13 May 2020 18:34:20 +0200 Subject: [PATCH] adding rendering bg1 --- sources/Models/Vector2.hpp | 5 ++ sources/PPU/PPU.cpp | 93 +++++++++++++++++++++++++++++---- sources/PPU/PPU.hpp | 8 ++- sources/Renderer/SFRenderer.cpp | 2 +- sources/main.cpp | 2 +- 5 files changed, 97 insertions(+), 13 deletions(-) diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index cfe8d01..0d58acd 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -7,6 +7,11 @@ #include #include +#include +#include +#include +#include +#include namespace ComSquare { diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index ff33d45..54e8542 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -260,6 +260,7 @@ namespace ComSquare::PPU this->_renderer.putPixel(x, y, pixelTmp); } } + renderBackground(1, {0, 0}, 4, false); this->_renderer.drawScreen(); } @@ -421,17 +422,20 @@ namespace ComSquare::PPU void PPU::renderBackground(int bgNumber, std::vector characterSize, int bpp, bool priority) { - int nbCharactersHeight = (this->_registers._bgsc[bgNumber].tilemapVerticalMirroring) ? 64 : 32; - int nbCharactersWidth = (this->_registers._bgsc[bgNumber].tilemapHorizontalMirroring) ? 64 : 32; - uint16_t vramAddress = this->_registers._bgsc[bgNumber].tilemapAddress << 1U; - uint16_t tilemapValue; + int nbCharactersHeight = (this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring) ? 64 : 32; + int nbCharactersWidth = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 64 : 32; + uint16_t vramAddress = this->_registers._bgsc[bgNumber - 1].tilemapAddress << 1U; + int size = 8; + uint16_t tileMapValue; - for (int i = 0; i < nbCharactersHeight * nbCharactersWidth; i++) { - for (int j = 0; j < 0x800; j++) { - tilemapValue = this->vram->read_internal(vramAddress); - tilemapValue += this->vram->read_internal(vramAddress + 1) << 8U; + if (this->_registers._bgmode.raw & (1U << (bgNumber + 3U))) + size = 16; + for (int i = 0; i < nbCharactersHeight; i++) { + for (int j = 0; j < nbCharactersWidth; j++) { + tileMapValue = this->vram->read_internal(vramAddress); + tileMapValue += this->vram->read_internal(vramAddress + 1) << 8U; vramAddress += 2; - + drawBgTile(tileMapValue, {i * size, j * size}, bgNumber, bpp, size); } } } @@ -446,13 +450,82 @@ namespace ComSquare::PPU return baseAddress + (x * 16 * step) + (y * step); } - void PPU::drawBgTile(uint16_t data, Vector2 pos, int bg, int bpp) + void PPU::drawBgTile(uint16_t data, Vector2 pos, int bg, int bpp, int size) { uint16_t graphicAddress; union TileMapData tileData; + std::vector palette; + int index = 0; + uint16_t tmp; + uint8_t reference = 0; + uint32_t color = 0; tileData.raw = data; graphicAddress = this->getGraphicVramAddress(tileData.posX, tileData.posY, bg, bpp); // loop on all pixels of the tile 8x8 6x16 16x8 8x16 + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; 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 -= size; + 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; } } \ No newline at end of file diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index c567aa2..7c8bd82 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -593,7 +593,13 @@ namespace ComSquare::PPU //! @brief Get the correct Vram address for a gien x and y uint16_t getGraphicVramAddress(int x, int y, int bg, int bpp); //! @brief Draw a tile on the screen at x y pos - void drawBgTile(uint16_t data, Vector2 pos, int bg, int bpp); + void drawBgTile(uint16_t data, Vector2 pos, int bg, int bpp, int size); + //! @brief Get a palette from the number of the palette (0 - 7) + std::vector getPalette(int nbPalette); + //! @brief Transform SNES color code BGR to uint32_t RGB + uint32_t getRealColor(uint16_t color); + //! @brief Get the color reference of a nb pixel tile + uint8_t getTilePixelReference(uint16_t addr, int bpp, int nb); }; } #endif //COMSQUARE_PPU_HPP diff --git a/sources/Renderer/SFRenderer.cpp b/sources/Renderer/SFRenderer.cpp index 90149d6..d9de9a5 100644 --- a/sources/Renderer/SFRenderer.cpp +++ b/sources/Renderer/SFRenderer.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include namespace ComSquare::Renderer diff --git a/sources/main.cpp b/sources/main.cpp index b0f0afe..0095428 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -76,7 +76,7 @@ int main(int argc, char **argv) } QApplication app(argc, argv); QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton); - Renderer::QtSFML renderer(600, 800); + Renderer::QtSFML renderer(1080, 1920); try { SNES snes(argv[1], renderer); renderer.createWindow(snes, 60);