mirror of
https://github.com/zoriya/ComSquare.git
synced 2026-06-05 10:59:38 +00:00
tile rendering is working (still some issues)
This commit is contained in:
+1
-1
@@ -242,7 +242,7 @@ add_executable(ComSquare
|
|||||||
sources/Renderer/QtRenderer/QtRenderSfml.hpp
|
sources/Renderer/QtRenderer/QtRenderSfml.hpp
|
||||||
sources/Debugger/TileViewer/TileViewer.cpp
|
sources/Debugger/TileViewer/TileViewer.cpp
|
||||||
sources/Debugger/TileViewer/TileViewer.hpp
|
sources/Debugger/TileViewer/TileViewer.hpp
|
||||||
)
|
sources/Debugger/TileViewer/TileRenderer.cpp sources/Debugger/TileViewer/TileRenderer.hpp)
|
||||||
|
|
||||||
include_directories(ComSquare sources)
|
include_directories(ComSquare sources)
|
||||||
|
|
||||||
|
|||||||
@@ -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 <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <QtWidgets/QTableWidget>
|
#include <QtWidgets/QTableWidget>
|
||||||
#include <utility>
|
|
||||||
#include <complex>
|
|
||||||
#include "Utility/Utility.hpp"
|
#include "Utility/Utility.hpp"
|
||||||
#include "PPU/PPU.hpp"
|
#include "PPU/PPU.hpp"
|
||||||
|
|
||||||
@@ -56,92 +54,4 @@ namespace ComSquare::Debugger
|
|||||||
{
|
{
|
||||||
return this->_ppu.cgramRead(addr);
|
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
|
#pragma once
|
||||||
|
|
||||||
|
namespace ComSquare::PPU
|
||||||
|
{
|
||||||
|
class PPU;
|
||||||
|
}
|
||||||
|
|
||||||
#include <QtCore/QSortFilterProxyModel>
|
#include <QtCore/QSortFilterProxyModel>
|
||||||
#include <QEvent>
|
#include <QEvent>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
@@ -11,38 +16,12 @@
|
|||||||
#include "PPU/PPU.hpp"
|
#include "PPU/PPU.hpp"
|
||||||
#include "Debugger/ClosableWindow.hpp"
|
#include "Debugger/ClosableWindow.hpp"
|
||||||
#include "Renderer/QtRenderer/QtSFML.hpp"
|
#include "Renderer/QtRenderer/QtSFML.hpp"
|
||||||
#include "../../ui/ui_tileView.h"
|
#include "../../../ui/ui_tileView.h"
|
||||||
#include "Ram/Ram.hpp"
|
#include "Ram/Ram.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace ComSquare::Debugger
|
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.
|
//! @brief window that allow the user to view all data going through the memory bus.
|
||||||
class TileViewer : public QObject {
|
class TileViewer : public QObject {
|
||||||
|
|||||||
+3
-2
@@ -9,6 +9,7 @@
|
|||||||
#include "Exceptions/InvalidAddress.hpp"
|
#include "Exceptions/InvalidAddress.hpp"
|
||||||
#include "Ram/Ram.hpp"
|
#include "Ram/Ram.hpp"
|
||||||
#include "Models/Vector2.hpp"
|
#include "Models/Vector2.hpp"
|
||||||
|
#include "Debugger/TileViewer/TileRenderer.hpp"
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
namespace ComSquare::PPU
|
namespace ComSquare::PPU
|
||||||
@@ -475,12 +476,12 @@ namespace ComSquare::PPU
|
|||||||
this->add_buffer(this->_screen, this->_subScreen);
|
this->add_buffer(this->_screen, this->_subScreen);
|
||||||
this->add_buffer(this->_screen, this->_mainScreen);
|
this->add_buffer(this->_screen, this->_mainScreen);
|
||||||
//this->_backgrounds[2].renderBackground();
|
//this->_backgrounds[2].renderBackground();
|
||||||
//add_buffer(this->_screen, this->_backgrounds[2].buffer);
|
//add_buffer(this->_screen, this->_backgrounds[2].buffer);*/
|
||||||
for (unsigned long i = 0; i < this->_screen.size(); i++) {
|
for (unsigned long i = 0; i < this->_screen.size(); i++) {
|
||||||
for (unsigned long j = 0; j < this->_screen[i].size(); j++) {
|
for (unsigned long j = 0; j < this->_screen[i].size(); j++) {
|
||||||
this->_renderer.putPixel(j, i, this->_screen[i][j]);
|
this->_renderer.putPixel(j, i, this->_screen[i][j]);
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
this->_renderer.drawScreen();
|
this->_renderer.drawScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -13,11 +13,11 @@
|
|||||||
#include "Models/Vector2.hpp"
|
#include "Models/Vector2.hpp"
|
||||||
#include "Background.hpp"
|
#include "Background.hpp"
|
||||||
#include "PPUUtils.hpp"
|
#include "PPUUtils.hpp"
|
||||||
#include "Debugger/TileViewer/TileViewer.hpp"
|
#include "Debugger/TileViewer/TileRenderer.hpp"
|
||||||
|
|
||||||
#define FALLTHROUGH __attribute__((fallthrough));
|
#define FALLTHROUGH __attribute__((fallthrough));
|
||||||
|
|
||||||
// TODO check if it usefull to have defines instead of constepxr
|
// TODO check if it useful to have defines instead of constexpr
|
||||||
#define VRAMSIZE 65536
|
#define VRAMSIZE 65536
|
||||||
#define CGRAMSIZE 512
|
#define CGRAMSIZE 512
|
||||||
#define OAMRAMSIZE 544
|
#define OAMRAMSIZE 544
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "Debugger/APUDebug.hpp"
|
#include "Debugger/APUDebug.hpp"
|
||||||
#include "Debugger/MemoryBusDebug.hpp"
|
#include "Debugger/MemoryBusDebug.hpp"
|
||||||
#include "Debugger/CGramDebug.hpp"
|
#include "Debugger/CGramDebug.hpp"
|
||||||
|
#include "Debugger/TileViewer/TileViewer.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ComSquare
|
namespace ComSquare
|
||||||
|
|||||||
Reference in New Issue
Block a user