making the constructor of background class much more easier to use

This commit is contained in:
Clément Le Bihan
2020-05-25 17:14:11 +02:00
parent ca07bf3366
commit 1553903eff
4 changed files with 108 additions and 25 deletions
+22 -17
View File
@@ -4,25 +4,28 @@
#include "Background.hpp"
#include "PPUUtils.hpp"
#include "PPU.hpp"
namespace ComSquare::PPU
{
Background::Background(int bpp, Vector2<int> backgroundSize, Vector2<int> characterSize, bool directColor, bool highRes, bool priority, uint16_t vramAddress, uint16_t graphicVramAddress):
_backgroundSize(backgroundSize),
_characterSize(characterSize),
_bpp(bpp),
_directColor(directColor),
_highRes(highRes),
_priority(priority),
_vramAddress(vramAddress),
_graphicVramAddress(graphicVramAddress)
Background::Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority):
_priority(priority)
{
_cgram = _ppu.cgram;
_vram = _ppu.vram;
_bpp = _ppu.getBPP(bgNumber);
_characterSize = _ppu.getCharacterSize(bgNumber);
_TileMapStartAddress = _ppu.getTileMapStartAddress(bgNumber);
_tileSetAddress = _ppu.getTileSetAddress(bgNumber);
_backgroundSize = _ppu.getBackgroundSize(bgNumber);
_directColor = false;
_highRes = false;
}
void Background::renderBackground(void)
std::array<std::array<uint32_t, 1024>, 1024> Background::renderBackground(void)
{
uint16_t vramAddress = this->_vramAddress;
uint16_t vramAddress = this->_TileMapStartAddress;
Vector2<int> offset(0, 0);
for (int i = 0; i < 4; i++) {
@@ -48,7 +51,7 @@ namespace ComSquare::PPU
uint32_t color = 0;
tileData.raw = data;
graphicAddress = this->_graphicVramAddress;
graphicAddress = this->_tileSetAddress + (tileData.posX * 16 * this->_bpp * 8) + (tileData.posY * this->_bpp * 8);
for (int i = 0; i < this->_characterSize.y; i++) {
for (int j = 0; j < this->_characterSize.x; j++) {
palette = getPalette(tileData.palette);
@@ -74,8 +77,8 @@ namespace ComSquare::PPU
uint16_t addr = nbPalette * 0x10;
for (int i = 0; i < 0xF; i++) {
palette[i] = this->cgram->read_internal(addr);
palette[i] += this->cgram->read_internal(addr + 1) << 8U;
palette[i] = this->_cgram->read_internal(addr);
palette[i] += this->_cgram->read_internal(addr + 1) << 8U;
}
return palette;
}
@@ -100,7 +103,7 @@ namespace ComSquare::PPU
uint8_t Background::getTilePixelReference(uint16_t addr, int nb)
{
uint8_t reference = this->vram->read_internal(addr);
uint8_t reference = this->_vram->read_internal(addr);
switch (this->_bpp) {
case 8:
@@ -122,8 +125,8 @@ namespace ComSquare::PPU
uint16_t vramAddress = baseAddress;
while (vramAddress < 0x800 + baseAddress) {
tileMapValue = this->vram->read_internal(vramAddress);
tileMapValue += this->vram->read_internal(vramAddress + 1) << 8U;
tileMapValue = this->_vram->read_internal(vramAddress);
tileMapValue += this->_vram->read_internal(vramAddress + 1) << 8U;
vramAddress += 2;
drawBgTile(tileMapValue, {(pos.x * this->_characterSize.x) + offset.x, (pos.y * this->_characterSize.y) + offset.y});
if (pos.x % 31 == 0 && pos.x) {
@@ -134,4 +137,6 @@ namespace ComSquare::PPU
pos.x++;
}
}
}
+8 -7
View File
@@ -10,6 +10,7 @@
#include <vector>
#include "../Models/Vector2.hpp"
#include "../Ram/Ram.hpp"
#include "PPU.hpp"
namespace ComSquare::PPU
{
@@ -21,12 +22,12 @@ namespace ComSquare::PPU
bool _directColor;
bool _highRes;
bool _priority;
uint16_t _vramAddress;
uint16_t _graphicVramAddress;
uint16_t _TileMapStartAddress;
uint16_t _tileSetAddress;
std::array<std::array<uint32_t, 1024>, 1024> _buffer;
std::shared_ptr<Ram::Ram> vram;
std::shared_ptr<Ram::Ram> cgram;
std::shared_ptr<Ram::Ram> _vram;
std::shared_ptr<Ram::Ram> _cgram;
//! @brief Draw a tile on the screen at x y pos
void drawBgTile(uint16_t data, Vector2<int> pos);
//! @brief Get a palette from the number of the palette (0 - 7)
@@ -38,11 +39,11 @@ namespace ComSquare::PPU
//! @brief draw a tilemap 32x32 starting at baseAddress
void drawBasicTileMap(uint16_t baseAddress, Vector2<int> offset);
public:
Background(int bpp, Vector2<int> backgroundSize, Vector2<int> characterSize, bool directColor, bool highRes, bool priority, uint16_t vramAddress, uint16_t graphicVramAddress);
Background(ComSquare::PPU::PPU &_ppu, int bgNumber, bool priority);
//! @brief Render a background on the screen
void renderBackground(void);
std::array<std::array<uint32_t, 1024>, 1024> renderBackground(void);
};
}
#endif //COMSQUARE_BACKGROUND_HPP
#endif //COMSQUARE_BACKGROUND_HPP
+67
View File
@@ -422,4 +422,71 @@ namespace ComSquare::PPU
{
return this->cgram->read_internal(addr);
}
int PPU::getBPP(int bgNumber)
{
switch (this->_registers._bgmode.bgMode) {
case 0:
return (2);
case 1:
if (bgNumber < 3)
return (4);
return (2);
case 2:
return (4);
case 3:
if (bgNumber == 1)
return (8);
return (4);
case 4:
if (bgNumber == 1)
return (8);
return (2);
case 5:
if (bgNumber == 1)
return (4);
return (2);
case 6:
return (4);
case 7:
if (bgNumber == 1)
return (8);
return (7);
default:
return (-1);
}
}
Vector2<int> PPU::getCharacterSize(int bgNumber)
{
Vector2<int> characterSize(8, 8);
//this wont work for modes 5 and 6 and will be reworked
if (this->_registers._bgmode.raw & (1U << (4 + bgNumber)))
characterSize = {16, 16};
return characterSize;
}
uint16_t PPU::getTileMapStartAddress(int bgNumber)
{
return this->_registers._bgsc[bgNumber - 1].tilemapAddress << 1U;
}
uint16_t PPU::getTileSetAddress(int bgNumber)
{
uint16_t baseAddress = this->_registers._bgnba[bgNumber > 2].raw;
baseAddress = (bgNumber % 2) ? baseAddress & 0xFU : (baseAddress & 0xFU) >> 4U;
baseAddress = baseAddress << 12U;
return baseAddress;
}
Vector2<int> PPU::getBackgroundSize(int bgNumber)
{
Vector2<int> backgroundSize(0,0);
backgroundSize.y = (this->_registers._bgsc[bgNumber - 1].tilemapVerticalMirroring) ? 2 : 1;
backgroundSize.x = (this->_registers._bgsc[bgNumber - 1].tilemapHorizontalMirroring) ? 2 : 1;
return backgroundSize;
}
}
+11 -1
View File
@@ -583,7 +583,7 @@ namespace ComSquare::PPU
std::string getValueName(uint24_t addr);
//! @brief Return true if the CPU is overloaded with debugging features.
virtual bool isDebugger();
//! @brief Allow others components to read the CGRAM (Debuggers)
//! @brief Allow others components to read the CGRAM
uint16_t cgramRead(uint16_t addr);
//! @brief Render a background on the screen
void renderBackground(int bgNumber, Vector2<int> characterSize, int bpp, bool priority);
@@ -599,6 +599,16 @@ namespace ComSquare::PPU
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<int> characterSize, Vector2<int> 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
Vector2<int> getCharacterSize(int bgNumber);
//! @brief Give the address where the tilemap starts
uint16_t getTileMapStartAddress(int bgNumber);
//! @brief Give the address to find the correct tileset for a given x and y
uint16_t getTileSetAddress(int bgNumber);
//! @brief Give the number of tilemaps to be rendered
Vector2<int> getBackgroundSize(int bgNumber);
};
}
#endif //COMSQUARE_PPU_HPP