mirror of
https://github.com/zoriya/ComSquare.git
synced 2026-05-30 09:08:43 +00:00
tile rendering is working (still some issues)
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// Created by cbihan on 24/05/2021.
|
||||
//
|
||||
|
||||
#include <complex>
|
||||
#include "TileRenderer.hpp"
|
||||
#include "PPU/PPU.hpp"
|
||||
|
||||
namespace ComSquare::Debugger
|
||||
{
|
||||
TileRenderer::TileRenderer()
|
||||
: _ram(nullptr),
|
||||
_cgram(nullptr),
|
||||
_bpp(2),
|
||||
_palette(0),
|
||||
buffer({{{0}}})
|
||||
{
|
||||
}
|
||||
|
||||
void TileRenderer::setRam(std::shared_ptr<Ram::Ram> ram)
|
||||
{
|
||||
this->_ram = std::move(ram);
|
||||
}
|
||||
|
||||
void TileRenderer::render()
|
||||
{
|
||||
uint8_t colorReference;
|
||||
uint24_t color;
|
||||
std::vector<uint16_t> palette = this->getPalette(this->_palette);
|
||||
int bufX = 0;
|
||||
int bufY = 0;
|
||||
int nbTilesDrawn = 0;
|
||||
int resetX = 0;
|
||||
|
||||
for (uint24_t i = 0; i < this->_ram->getSize(); i += this->_bpp) {
|
||||
if (bufX >= 1024 || bufY >= 1024)
|
||||
break;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
colorReference = this->getPixelReferenceFromTileRow(i, j);
|
||||
color = PPU::getRealColor(palette[colorReference]);
|
||||
buffer[bufY][bufX++] = color;
|
||||
}
|
||||
bufY++;
|
||||
bufX = resetX;
|
||||
}
|
||||
}
|
||||
|
||||
void TileRenderer::setPalette(int palette)
|
||||
{
|
||||
this->_palette = palette;
|
||||
}
|
||||
|
||||
void TileRenderer::setBpp(int bpp)
|
||||
{
|
||||
this->_bpp = bpp;
|
||||
}
|
||||
|
||||
uint8_t TileRenderer::getPixelReferenceFromTileRow(uint16_t tileRowAddress, uint8_t pixelIndex)
|
||||
{
|
||||
size_t size = this->_ram->getSize();
|
||||
uint8_t highByte = this->_ram->read(tileRowAddress % size);
|
||||
uint8_t lowByte = this->_ram->read((tileRowAddress + 1) % size);
|
||||
uint8_t secondHighByte;
|
||||
uint8_t secondLowByte;
|
||||
uint16_t result = 0;
|
||||
uint8_t shift = 8 - 1U - pixelIndex;
|
||||
|
||||
switch (this->_bpp) {
|
||||
case 8:
|
||||
return highByte;
|
||||
case 4:
|
||||
secondHighByte = this->_ram->read((tileRowAddress + 16) % size);
|
||||
secondLowByte = this->_ram->read((tileRowAddress + 17) % size);
|
||||
result = ((secondHighByte & (1U << shift)) | ((secondLowByte & (1U << shift)) << 1U));
|
||||
result = (shift - 2 >= 0) ? result >> (shift - 2) : result << ((shift - 2) * -1);
|
||||
FALLTHROUGH
|
||||
case 2:
|
||||
result += ((highByte & (1U << shift)) | ((lowByte & (1U << shift)) << 1U)) >> shift;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<uint16_t> TileRenderer::getPalette(int nbPalette)
|
||||
{
|
||||
uint8_t nbColors = std::pow(2, this->_bpp);
|
||||
uint16_t addr = nbPalette * this->_bpp * this->_bpp * 2; // 2 because it's 2 addr for 1 color
|
||||
std::vector<uint16_t> palette(nbColors);
|
||||
|
||||
for (int i = 0; i < nbColors; i++) {
|
||||
palette[i] = this->_cgram->read(addr);
|
||||
palette[i] += this->_cgram->read(addr + 1) << 8U;
|
||||
addr += 2;
|
||||
}
|
||||
return palette;
|
||||
}
|
||||
|
||||
void TileRenderer::setCgram(std::shared_ptr<Ram::Ram> ram)
|
||||
{
|
||||
this->_cgram = std::move(ram);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by cbihan on 24/05/2021.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include "Ram/Ram.hpp"
|
||||
|
||||
namespace ComSquare::Debugger
|
||||
{
|
||||
class TileRenderer {
|
||||
private:
|
||||
//! @brief ram to render
|
||||
std::shared_ptr<Ram::Ram> _ram;
|
||||
//! @brief cgram to access the colors
|
||||
std::shared_ptr<Ram::Ram> _cgram;
|
||||
//! @brief The bpp to use while rendering
|
||||
int _bpp;
|
||||
//! @brief The palette number to use while rendering
|
||||
int _palette;
|
||||
public:
|
||||
//! @brief internal buffer
|
||||
std::array<std::array<uint32_t, 1024>, 1024> buffer;
|
||||
void setPalette(int palette);
|
||||
void setCgram(std::shared_ptr<Ram::Ram> ram);
|
||||
void setBpp(int bpp);
|
||||
void setRam(std::shared_ptr<Ram::Ram> ram);
|
||||
uint8_t getPixelReferenceFromTileRow(uint16_t tileRowAddress, uint8_t pixelIndex);
|
||||
std::vector<uint16_t> getPalette(int nbPalette);
|
||||
//! @brief render the selected ram
|
||||
void render();
|
||||
TileRenderer();
|
||||
TileRenderer(const TileRenderer &) = default;
|
||||
~TileRenderer() = default;
|
||||
TileRenderer &operator=(const TileRenderer &) = default;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@ namespace ComSquare::Renderer
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <QtWidgets/QTableWidget>
|
||||
#include <utility>
|
||||
#include <complex>
|
||||
#include "Utility/Utility.hpp"
|
||||
#include "PPU/PPU.hpp"
|
||||
|
||||
@@ -56,92 +54,4 @@ namespace ComSquare::Debugger
|
||||
{
|
||||
return this->_ppu.cgramRead(addr);
|
||||
}
|
||||
|
||||
TileRenderer::TileRenderer()
|
||||
: _ram(nullptr),
|
||||
_cgram(nullptr),
|
||||
_bpp(2),
|
||||
_palette(0),
|
||||
buffer({{{0}}})
|
||||
{
|
||||
}
|
||||
|
||||
void TileRenderer::setRam(std::shared_ptr<Ram::Ram> ram)
|
||||
{
|
||||
this->_ram = std::move(ram);
|
||||
}
|
||||
|
||||
void TileRenderer::render()
|
||||
{
|
||||
uint8_t colorReference;
|
||||
uint24_t color;
|
||||
std::vector<uint16_t> palette = this->getPalette(this->_palette);
|
||||
int bufX = 0;
|
||||
int bufY = 0;
|
||||
|
||||
for (uint24_t i = 0; i < this->_ram->getSize(); i += this->_bpp) {
|
||||
for (int j = 0; j < 8; j ++) {
|
||||
colorReference = this->getPixelReferenceFromTileRow(i, j);
|
||||
color = PPU::getRealColor(palette[colorReference]);
|
||||
buffer[bufY++][bufX++] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TileRenderer::setPalette(int palette)
|
||||
{
|
||||
this->_palette = palette;
|
||||
}
|
||||
|
||||
void TileRenderer::setBpp(int bpp)
|
||||
{
|
||||
this->_bpp = bpp;
|
||||
}
|
||||
|
||||
uint8_t TileRenderer::getPixelReferenceFromTileRow(uint16_t tileRowAddress, uint8_t pixelIndex)
|
||||
{
|
||||
size_t size = this->_ram->getSize();
|
||||
uint8_t highByte = this->_ram->read(tileRowAddress % size);
|
||||
uint8_t lowByte = this->_ram->read((tileRowAddress + 1) % size);
|
||||
uint8_t secondHighByte;
|
||||
uint8_t secondLowByte;
|
||||
uint16_t result = 0;
|
||||
uint8_t shift = 8 - 1U - pixelIndex;
|
||||
|
||||
switch (this->_bpp) {
|
||||
case 8:
|
||||
return highByte;
|
||||
case 4:
|
||||
secondHighByte = this->_ram->read((tileRowAddress + 16) % size);
|
||||
secondLowByte = this->_ram->read((tileRowAddress + 17) % size);
|
||||
result = ((secondHighByte & (1U << shift)) | ((secondLowByte & (1U << shift)) << 1U));
|
||||
result = (shift - 2 >= 0) ? result >> (shift - 2) : result << ((shift - 2) * -1);
|
||||
FALLTHROUGH
|
||||
case 2:
|
||||
result += ((highByte & (1U << shift)) | ((lowByte & (1U << shift)) << 1U)) >> shift;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<uint16_t> TileRenderer::getPalette(int nbPalette)
|
||||
{
|
||||
uint8_t nbColors = std::pow(2, this->_bpp);
|
||||
uint16_t addr = nbPalette * this->_bpp * this->_bpp * 2; // 2 because it's 2 addr for 1 color
|
||||
std::vector<uint16_t> palette(nbColors);
|
||||
|
||||
for (int i = 0; i < nbColors; i++) {
|
||||
palette[i] = this->_cgram->read(addr);
|
||||
palette[i] += this->_cgram->read(addr + 1) << 8U;
|
||||
addr += 2;
|
||||
}
|
||||
return palette;
|
||||
}
|
||||
|
||||
void TileRenderer::setCgram(std::shared_ptr<Ram::Ram> ram)
|
||||
{
|
||||
this->_cgram = std::move(ram);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ComSquare::PPU
|
||||
{
|
||||
class PPU;
|
||||
}
|
||||
|
||||
#include <QtCore/QSortFilterProxyModel>
|
||||
#include <QEvent>
|
||||
#include <QMouseEvent>
|
||||
@@ -11,38 +16,12 @@
|
||||
#include "PPU/PPU.hpp"
|
||||
#include "Debugger/ClosableWindow.hpp"
|
||||
#include "Renderer/QtRenderer/QtSFML.hpp"
|
||||
#include "../../ui/ui_tileView.h"
|
||||
#include "../../../ui/ui_tileView.h"
|
||||
#include "Ram/Ram.hpp"
|
||||
|
||||
|
||||
namespace ComSquare::Debugger
|
||||
{
|
||||
class TileRenderer {
|
||||
private:
|
||||
//! @brief ram to render
|
||||
std::shared_ptr<Ram::Ram> _ram;
|
||||
//! @brief cgram to access the colors
|
||||
std::shared_ptr<Ram::Ram> _cgram;
|
||||
//! @brief The bpp to use while rendering
|
||||
int _bpp;
|
||||
//! @brief The palette number to use while rendering
|
||||
int _palette;
|
||||
public:
|
||||
//! @brief internal buffer
|
||||
std::array<std::array<uint32_t, 1024>, 1024> buffer;
|
||||
void setPalette(int palette);
|
||||
void setCgram(std::shared_ptr<Ram::Ram> ram);
|
||||
void setBpp(int bpp);
|
||||
void setRam(std::shared_ptr<Ram::Ram> ram);
|
||||
uint8_t getPixelReferenceFromTileRow(uint16_t tileRowAddress, uint8_t pixelIndex);
|
||||
std::vector<uint16_t> getPalette(int nbPalette);
|
||||
//! @brief render the selected ram
|
||||
void render();
|
||||
TileRenderer();
|
||||
TileRenderer(const TileRenderer &) = default;
|
||||
~TileRenderer() = default;
|
||||
TileRenderer &operator=(const TileRenderer &) = default;
|
||||
};
|
||||
|
||||
//! @brief window that allow the user to view all data going through the memory bus.
|
||||
class TileViewer : public QObject {
|
||||
|
||||
Reference in New Issue
Block a user