adding rendering bg1

This commit is contained in:
Clément Le Bihan
2020-05-13 18:34:20 +02:00
parent e90db35c88
commit fd9da7085b
5 changed files with 97 additions and 13 deletions
+5
View File
@@ -7,6 +7,11 @@
#include <ostream> #include <ostream>
#include <cmath> #include <cmath>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
namespace ComSquare namespace ComSquare
{ {
+83 -10
View File
@@ -260,6 +260,7 @@ namespace ComSquare::PPU
this->_renderer.putPixel(x, y, pixelTmp); this->_renderer.putPixel(x, y, pixelTmp);
} }
} }
renderBackground(1, {0, 0}, 4, false);
this->_renderer.drawScreen(); this->_renderer.drawScreen();
} }
@@ -421,17 +422,20 @@ namespace ComSquare::PPU
void PPU::renderBackground(int bgNumber, std::vector<int> characterSize, int bpp, bool priority) void PPU::renderBackground(int bgNumber, std::vector<int> characterSize, int bpp, bool priority)
{ {
int nbCharactersHeight = (this->_registers._bgsc[bgNumber].tilemapVerticalMirroring) ? 64 : 32; int nbCharactersHeight = (this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring) ? 64 : 32;
int nbCharactersWidth = (this->_registers._bgsc[bgNumber].tilemapHorizontalMirroring) ? 64 : 32; int nbCharactersWidth = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 64 : 32;
uint16_t vramAddress = this->_registers._bgsc[bgNumber].tilemapAddress << 1U; uint16_t vramAddress = this->_registers._bgsc[bgNumber - 1].tilemapAddress << 1U;
uint16_t tilemapValue; int size = 8;
uint16_t tileMapValue;
for (int i = 0; i < nbCharactersHeight * nbCharactersWidth; i++) { if (this->_registers._bgmode.raw & (1U << (bgNumber + 3U)))
for (int j = 0; j < 0x800; j++) { size = 16;
tilemapValue = this->vram->read_internal(vramAddress); for (int i = 0; i < nbCharactersHeight; i++) {
tilemapValue += this->vram->read_internal(vramAddress + 1) << 8U; for (int j = 0; j < nbCharactersWidth; j++) {
tileMapValue = this->vram->read_internal(vramAddress);
tileMapValue += this->vram->read_internal(vramAddress + 1) << 8U;
vramAddress += 2; 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); return baseAddress + (x * 16 * step) + (y * step);
} }
void PPU::drawBgTile(uint16_t data, Vector2<int> pos, int bg, int bpp) void PPU::drawBgTile(uint16_t data, Vector2<int> pos, int bg, int bpp, int size)
{ {
uint16_t graphicAddress; uint16_t graphicAddress;
union TileMapData tileData; union TileMapData tileData;
std::vector<uint16_t> palette;
int index = 0;
uint16_t tmp;
uint8_t reference = 0;
uint32_t color = 0;
tileData.raw = data; tileData.raw = data;
graphicAddress = this->getGraphicVramAddress(tileData.posX, tileData.posY, bg, bpp); graphicAddress = this->getGraphicVramAddress(tileData.posX, tileData.posY, bg, bpp);
// loop on all pixels of the tile 8x8 6x16 16x8 8x16 // 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<uint16_t> PPU::getPalette(int nbPalette)
{
std::vector<uint16_t> 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;
} }
} }
+7 -1
View File
@@ -593,7 +593,13 @@ namespace ComSquare::PPU
//! @brief Get the correct Vram address for a gien x and y //! @brief Get the correct Vram address for a gien x and y
uint16_t getGraphicVramAddress(int x, int y, int bg, int bpp); uint16_t getGraphicVramAddress(int x, int y, int bg, int bpp);
//! @brief Draw a tile on the screen at x y pos //! @brief Draw a tile on the screen at x y pos
void drawBgTile(uint16_t data, Vector2<int> pos, int bg, int bpp); void drawBgTile(uint16_t data, Vector2<int> pos, int bg, int bpp, int size);
//! @brief Get a palette from the number of the palette (0 - 7)
std::vector<uint16_t> 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 #endif //COMSQUARE_PPU_HPP
+1 -1
View File
@@ -7,7 +7,7 @@
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <SFML/Graphics//RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <iostream> #include <iostream>
namespace ComSquare::Renderer namespace ComSquare::Renderer
+1 -1
View File
@@ -76,7 +76,7 @@ int main(int argc, char **argv)
} }
QApplication app(argc, argv); QApplication app(argc, argv);
QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton); QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
Renderer::QtSFML renderer(600, 800); Renderer::QtSFML renderer(1080, 1920);
try { try {
SNES snes(argv[1], renderer); SNES snes(argv[1], renderer);
renderer.createWindow(snes, 60); renderer.createWindow(snes, 60);