mirror of
https://github.com/zoriya/ComSquare.git
synced 2026-05-28 08:33:34 +00:00
Cleaning up the qt renderer
This commit is contained in:
@@ -22,13 +22,16 @@ namespace ComSquare::Renderer
|
||||
{
|
||||
QtSFML::QtSFML(QWidget *parentWidget)
|
||||
: _window(parentWidget),
|
||||
_sfWidget(nullptr)
|
||||
{
|
||||
}
|
||||
_sfWidget(nullptr)
|
||||
{}
|
||||
|
||||
void QtSFML::createWindow(SNES &snes, int maxFPS)
|
||||
{
|
||||
this->_sfWidget = new QtFullSFML(snes, this->_window, QPoint(0, 0), QSize(this->_window->width(), this->_window->height()), maxFPS);
|
||||
this->_sfWidget = new QtFullSFML(snes,
|
||||
this->_window,
|
||||
QPoint(0, 0),
|
||||
QSize(this->_window->width(), this->_window->height()),
|
||||
maxFPS);
|
||||
}
|
||||
|
||||
void QtSFML::putPixel(unsigned y, unsigned x, uint32_t rgba)
|
||||
@@ -48,10 +51,10 @@ namespace ComSquare::Renderer
|
||||
this->_window->setWindowTitle((newWindowName + " - ComSquare").c_str());
|
||||
}
|
||||
|
||||
QtFullSFML::QtFullSFML(SNES &snes, QWidget *parent, const QPoint &position, const QSize &size, int frameRate) :
|
||||
QtWidgetSFML(parent, position, size, frameRate),
|
||||
_snes(snes)
|
||||
{ }
|
||||
QtFullSFML::QtFullSFML(SNES &snes, QWidget *parent, const QPoint &position, const QSize &size, int frameRate)
|
||||
: QtWidgetSFML(parent, position, QSize(1024, 1024), frameRate),
|
||||
_snes(snes)
|
||||
{}
|
||||
|
||||
void QtFullSFML::onUpdate()
|
||||
{
|
||||
@@ -72,8 +75,9 @@ namespace ComSquare::Renderer
|
||||
|
||||
void QtFullSFML::openRom()
|
||||
{
|
||||
auto rom = QFileDialog::getOpenFileName(nullptr, tr("Open a ROM"), QDir::homePath(),
|
||||
tr("Rom files (*.sfc, *.smc);;Audio rom files (*.spc);;All files (*)"));
|
||||
auto rom = QFileDialog::getOpenFileName(nullptr, tr("Open a ROM"),
|
||||
QDir::homePath(),
|
||||
tr("Rom files (*.sfc, *.smc);;Audio rom files (*.spc);;All files (*)"));
|
||||
if (!rom.isEmpty())
|
||||
this->_snes.loadRom(rom.toStdString());
|
||||
}
|
||||
@@ -84,6 +88,7 @@ namespace ComSquare::Renderer
|
||||
}
|
||||
|
||||
#ifdef DEBUGGER_ENABLED
|
||||
|
||||
void QtFullSFML::enableDebugCPU()
|
||||
{
|
||||
this->_snes.enableCPUDebugging();
|
||||
@@ -123,6 +128,7 @@ namespace ComSquare::Renderer
|
||||
{
|
||||
this->_snes.enableTileViewer();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
QtSFMLWindow::QtSFMLWindow(int height, int width)
|
||||
|
||||
@@ -17,12 +17,10 @@ namespace ComSquare::Renderer
|
||||
|
||||
void QtSFMLTileRenderer::onUpdate()
|
||||
{
|
||||
this->_window.clear(sf::Color::Black);
|
||||
for (unsigned long i = 0; i < this->buffer.size(); i++) {
|
||||
for (unsigned long j = 0; j < this->buffer[i].size(); j++) {
|
||||
this->putPixel(j, i, this->buffer[i][j]);
|
||||
}
|
||||
}
|
||||
this->drawScreen();
|
||||
}
|
||||
}
|
||||
@@ -23,21 +23,22 @@ namespace ComSquare::Renderer
|
||||
|
||||
void QtWidgetSFML::showEvent(QShowEvent *)
|
||||
{
|
||||
if (!this->_isInitialized) {
|
||||
// Under X11, we need to flush the commands sent to the server to ensure that
|
||||
// SFML will get an updated view of the windows
|
||||
if (this->_isInitialized)
|
||||
return;
|
||||
#ifdef Q_WS_X11
|
||||
XFlush(QX11Info::display());
|
||||
// Under X11, we need to flush the commands sent to the server to ensure that
|
||||
// SFML will get an updated view of the windows
|
||||
XFlush(QX11Info::display());
|
||||
#endif
|
||||
this->_window.create(static_cast<sf::WindowHandle>(this->winId()));
|
||||
this->_window.setFramerateLimit(60);
|
||||
this->_onInit();
|
||||
this->_window.create(static_cast<sf::WindowHandle>(this->winId()));
|
||||
this->_window.setFramerateLimit(60);
|
||||
this->_onInit();
|
||||
|
||||
this->_timer.setSingleShot(false);
|
||||
connect(&_timer, SIGNAL(timeout()), this, SLOT(onUpdate()));
|
||||
this->_timer.start();
|
||||
this->_isInitialized = true;
|
||||
}
|
||||
this->_timer.setSingleShot(false);
|
||||
connect(&_timer, SIGNAL(timeout()), this, SLOT(repaint()));
|
||||
connect(&_timer, SIGNAL(timeout()), this, SLOT(onUpdate()));
|
||||
this->_timer.start();
|
||||
this->_isInitialized = true;
|
||||
}
|
||||
|
||||
QPaintEngine *QtWidgetSFML::paintEngine() const
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace ComSquare::Renderer
|
||||
|
||||
void SFRenderer::drawScreen()
|
||||
{
|
||||
this->_window.clear();
|
||||
this->_texture.update(reinterpret_cast<sf::Uint8 *>(this->_pixelBuffer));
|
||||
this->_sprite.setTexture(this->_texture);
|
||||
this->_window.draw(this->_sprite);
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
// Created by cbihan on 1/30/20.
|
||||
//
|
||||
|
||||
#ifndef COMSQUARE_SFRENDERER_HPP
|
||||
#define COMSQUARE_SFRENDERER_HPP
|
||||
#pragma once
|
||||
|
||||
#include "IRenderer.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
@@ -14,16 +13,24 @@
|
||||
|
||||
namespace ComSquare::Renderer
|
||||
{
|
||||
class InvalidPixelPosition : public std::exception {
|
||||
class InvalidPixelPosition : public std::exception
|
||||
{
|
||||
private:
|
||||
std::string _msg;
|
||||
public:
|
||||
explicit InvalidPixelPosition(const std::string &name, unsigned int x, unsigned int width)
|
||||
: _msg("Trying to place a pixel at an invalid " + name + " (" + std::to_string(x) + ">=" + std::to_string(width) + ")") {}
|
||||
const char *what() const noexcept override { return this->_msg.c_str(); }
|
||||
: _msg("Trying to place a pixel at an invalid " + name
|
||||
+ " (" + std::to_string(x) + ">=" + std::to_string(width)+ ")")
|
||||
{}
|
||||
|
||||
[[nodiscard]] const char *what() const noexcept override
|
||||
{
|
||||
return this->_msg.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class SFRenderer : public IRenderer {
|
||||
class SFRenderer : public IRenderer
|
||||
{
|
||||
protected:
|
||||
//! @brief The Renderer for the window.
|
||||
sf::RenderWindow _window;
|
||||
@@ -72,5 +79,3 @@ namespace ComSquare::Renderer
|
||||
~SFRenderer();
|
||||
};
|
||||
}
|
||||
|
||||
#endif //COMSQUARE_SFRENDERER_HPP
|
||||
|
||||
+11
-8
@@ -7,24 +7,26 @@
|
||||
|
||||
namespace ComSquare
|
||||
{
|
||||
SNES::SNES(Renderer::IRenderer &renderer)
|
||||
: bus(),
|
||||
SNES::SNES(Renderer::IRenderer &render)
|
||||
: renderer(render),
|
||||
bus(),
|
||||
cartridge(),
|
||||
wram(16384, WRam, "WRam"),
|
||||
sram(0, SRam, "SRam"),
|
||||
cpu(this->bus, cartridge.header),
|
||||
ppu(renderer),
|
||||
apu(renderer)
|
||||
ppu(render),
|
||||
apu(render)
|
||||
{}
|
||||
|
||||
SNES::SNES(const std::string &romPath, Renderer::IRenderer &renderer)
|
||||
: bus(),
|
||||
SNES::SNES(const std::string &romPath, Renderer::IRenderer &render)
|
||||
: renderer(render),
|
||||
bus(),
|
||||
cartridge(romPath),
|
||||
wram(16384, WRam, "WRam"),
|
||||
sram(this->cartridge.header.sramSize, SRam, "SRam"),
|
||||
cpu(this->bus, cartridge.header),
|
||||
ppu(renderer),
|
||||
apu(renderer)
|
||||
ppu(render),
|
||||
apu(render)
|
||||
{
|
||||
this->bus.mapComponents(*this);
|
||||
if (this->cartridge.getType() == Cartridge::Audio)
|
||||
@@ -56,6 +58,7 @@ namespace ComSquare
|
||||
this->apu.reset();
|
||||
if (this->cartridge.getType() == Cartridge::Audio)
|
||||
this->apu.loadFromSPC(this->cartridge);
|
||||
this->renderer.setWindowName(this->cartridge.header.gameName);
|
||||
}
|
||||
|
||||
#ifdef DEBUGGER_ENABLED
|
||||
|
||||
+7
-4
@@ -52,6 +52,9 @@ namespace ComSquare
|
||||
std::optional<Debugger::TileViewer> _tileViewer;
|
||||
#endif
|
||||
public:
|
||||
//! @brief The renderer used to display pixels and play sounds.
|
||||
Renderer::IRenderer &renderer;
|
||||
|
||||
//! @brief The memory bus that map addresses to components.
|
||||
Memory::MemoryBus bus;
|
||||
|
||||
@@ -69,12 +72,12 @@ namespace ComSquare
|
||||
APU::APU apu;
|
||||
|
||||
//! @brief Create all the components using a common memory bus for all of them.
|
||||
//! @param renderer The renderer to use.
|
||||
explicit SNES(Renderer::IRenderer &renderer);
|
||||
//! @param render The render to use.
|
||||
explicit SNES(Renderer::IRenderer &render);
|
||||
//! @brief Create all the components using a common memory bus for all of them and load a rom
|
||||
//! @param ramPath The rom to load.
|
||||
//! @param renderer The renderer to use.
|
||||
SNES(const std::string &ramPath, Renderer::IRenderer &renderer);
|
||||
//! @param render The render to use.
|
||||
SNES(const std::string &ramPath, Renderer::IRenderer &render);
|
||||
//! @brief A SNES is not copyable.
|
||||
SNES(const SNES &) = delete;
|
||||
//! @brief A SNES can't be assigned
|
||||
|
||||
Reference in New Issue
Block a user