starting the renderMainandSubScreen fct

This commit is contained in:
Clément Le Bihan
2020-05-25 23:56:18 +02:00
parent dac3a52eff
commit ce4f231583
4 changed files with 76 additions and 10 deletions
+6 -4
View File
@@ -9,7 +9,8 @@
namespace ComSquare::PPU
{
Background::Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority):
_priority(priority)
priority(priority),
bgNumber(bgNumber)
{
_cgram = _ppu.cgram;
_vram = _ppu.vram;
@@ -27,8 +28,8 @@ namespace ComSquare::PPU
{
uint16_t vramAddress = this->_TileMapStartAddress;
Vector2<int> offset(0, 0);
this->_backgroundSize.x = this->_tileMaps.x * this->_characterSize.x * 32;
this->_backgroundSize.y = this->_tileMaps.y * this->_characterSize.y * 32;
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->_tileMaps.x == 1) && !(i > 1 && this->_tileMaps.y == 1)) {
@@ -59,7 +60,8 @@ namespace ComSquare::PPU
palette = getPalette(tileData.palette);
reference = getTilePixelReference(graphicAddress, index);
color = getRealColor(palette[reference]);
this->_buffer[pos.x][pos.y] = color;
if (tileData.tilePriority == this->priority)
this->buffer[pos.x][pos.y] = color;
index++;
pos.x++;
if (index == (8 / this->_bpp) - 1) {
+4 -3
View File
@@ -22,7 +22,6 @@ namespace ComSquare::PPU
int _bpp;
bool _directColor;
bool _highRes;
bool _priority;
uint16_t _TileMapStartAddress;
uint16_t _tileSetAddress;
@@ -39,8 +38,10 @@ namespace ComSquare::PPU
//! @brief draw a tilemap 32x32 starting at baseAddress
void drawBasicTileMap(uint16_t baseAddress, Vector2<int> offset);
public:
Vector2<int> _backgroundSize;
std::array<std::array<uint32_t, 1024>, 1024> _buffer;
bool priority;
int bgNumber;
Vector2<int> backgroundSize;
std::array<std::array<uint32_t, 1024>, 1024> buffer;
Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority);
//! @brief Render a background on the screen
void renderBackground(void);
+48 -3
View File
@@ -253,9 +253,9 @@ namespace ComSquare::PPU
(void)cycles;
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]);
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();
@@ -483,4 +483,49 @@ namespace ComSquare::PPU
backgroundSize.x = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 2 : 1;
return backgroundSize;
}
void PPU::renderMainAndSubScreen(void)
{
for (auto & _background : this->_backgrounds)
_background.renderBackground();
// the buffer is overwrite if necessery by a new bg so the background priority is from back to front
switch (this->_registers._bgmode.bgMode) {
case 0:
this->addToMainSubScreen(this->_backgrounds[bgName::bg4NoPriority]);
this->addToMainSubScreen(this->_backgrounds[bgName::bg3NoPriority]);
//sprites priority 0
this->addToMainSubScreen(this->_backgrounds[bgName::bg4Priority]);
this->addToMainSubScreen(this->_backgrounds[bgName::bg3Priority]);
//sprites priority 1
this->addToMainSubScreen(this->_backgrounds[bgName::bg2NoPriority]);
this->addToMainSubScreen(this->_backgrounds[bgName::bg1NoPriority]);
//sprites priority 2
this->addToMainSubScreen(this->_backgrounds[bgName::bg2Priority]);
this->addToMainSubScreen(this->_backgrounds[bgName::bg1Priority]);
//sprites priority 3
break;
}
}
template <std::size_t SIZE>
void PPU::add_buffer(std::array<std::array<uint32_t, SIZE>, SIZE> &buffer, Background &bg)
{
for (int i = 0; i < bg.backgroundSize.y; i++) {
for (int j = 0; j < bg.backgroundSize.x; j++) {
if (bg.buffer[i][j])
buffer[i][j] = bg.buffer[i][j];
}
}
}
void PPU::addToMainSubScreen(Background &bg)
{
int i = bg.bgNumber + bg.priority;
if (this->_registers._t[0].raw & (1U << bg.bgNumber - 1U))
this->add_buffer(this->_mainScreen, this->_backgrounds[i]);
if (this->_registers._t[1].raw & (1U << bg.bgNumber - 1U))
this->add_buffer(this->_subScreen, this->_backgrounds[i]);
}
}
+18
View File
@@ -21,6 +21,17 @@
namespace ComSquare::PPU
{
class Background;
enum bgName {
bg1NoPriority = 0,
bg1Priority,
bg2NoPriority,
bg2Priority,
bg3NoPriority,
bg3Priority,
bg4NoPriority,
bg4Priority
};
enum ppuRegisters {
//! @brief INIDISP Register (F-blank and Brightness)
inidisp = 0x00,
@@ -605,6 +616,13 @@ namespace ComSquare::PPU
uint16_t getTileSetAddress(int bgNumber);
//! @brief Give the number of tilemaps to be rendered
Vector2<int> getBackgroundSize(int bgNumber);
//! @brief Render the Main and sub screen correctly
void renderMainAndSubScreen(void);
//! @brief Add a bg buffer to another buffer
template <std::size_t SIZE>
void PPU::add_buffer(std::array<std::array<uint32_t, SIZE>, SIZE> &buffer, Background &bg);
//! @brief Add a bg to the sub and/or main screen
void addToMainSubScreen(Background &bg);
};
}
#endif //COMSQUARE_PPU_HPP