Finishing the bases of the QT renderer

This commit is contained in:
Anonymus Raccoon
2020-02-16 03:23:26 +01:00
parent ad5aacbf56
commit 6ff4de3e0b
14 changed files with 81 additions and 38 deletions
-1
View File
@@ -137,6 +137,5 @@ target_link_libraries(ComSquare
sfml-system sfml-system
sfml-audio sfml-audio
sfml-network sfml-network
tgui
Qt5::Widgets Qt5::Widgets
) )
+1 -1
View File
@@ -1,4 +1,4 @@
<p align="center"><img src="./ressources/Black.svg" width="400"></p> <p align="center"><img src="resources/Black.svg" width="400"></p>
<p align="center"> <p align="center">
<a href="./LICENSE"><img src="https://img.shields.io/github/license/AnonymusRaccoon/ComSquare?style=flat-square" alt="License"></a> <a href="./LICENSE"><img src="https://img.shields.io/github/license/AnonymusRaccoon/ComSquare?style=flat-square" alt="License"></a>

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

-3
View File
@@ -17,9 +17,6 @@ namespace ComSquare
//! @brief Set a new name to the window, if there is already a name it will be overwrite //! @brief Set a new name to the window, if there is already a name it will be overwrite
virtual void setWindowName(std::string) = 0; virtual void setWindowName(std::string) = 0;
//! @brief Tells to the program if the window has been closed, and therefore if he should stop
bool shouldExit = true;
//! @brief Render the buffer to the window //! @brief Render the buffer to the window
virtual void drawScreen() = 0; virtual void drawScreen() = 0;
+22 -9
View File
@@ -4,7 +4,7 @@
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QtWidgets/QFrame> #include <QtWidgets/QFrame>
#include <QCloseEvent> #include <QIcon>
#include <iostream> #include <iostream>
#include "QtSFML.hpp" #include "QtSFML.hpp"
@@ -15,24 +15,37 @@
namespace ComSquare::Renderer namespace ComSquare::Renderer
{ {
QtSFML::QtSFML(QApplication &app, unsigned int height, unsigned int width) : QtSFML::QtSFML(QApplication &app, unsigned int h, unsigned int w) :
SFRenderer(height, width), _app(app) _app(app), _sfWidget(nullptr), width(w), height(h)
{ } { }
void QtSFML::createWindow(SNES &snes, int maxFPS) void QtSFML::createWindow(SNES &snes, int maxFPS)
{ {
QFrame *frame = new QFrame(); this->frame = new QFrame();
frame->show(); this->setWindowName(snes.cartridge->header.gameName);
MainQTWidget *sfView = new MainQTWidget(snes, frame, QPoint(0, 0), QSize(this->_videoMode.width, this->_videoMode.height), maxFPS); this->frame->show();
sfView->show(); this->_sfWidget = new QtFullSFML(snes, frame, QPoint(0, 0), QSize(this->width, this->height), maxFPS);
this->_sfWidget->show();
} }
MainQTWidget::MainQTWidget(SNES &snes, QWidget *parent, const QPoint &position, const QSize &size, int frameRate) : void QtSFML::setWindowName(std::string newWindowName)
{
this->frame->setWindowTitle((newWindowName + " - ComSquare").c_str());
}
void QtSFML::putPixel(unsigned y, unsigned x, uint32_t rgba)
{
this->_sfWidget->putPixel(y, x, rgba);
}
void QtSFML::drawScreen() { }
QtFullSFML::QtFullSFML(SNES &snes, QWidget *parent, const QPoint &position, const QSize &size, int frameRate) :
QtWidgetSFML(parent, position, size, frameRate), QtWidgetSFML(parent, position, size, frameRate),
_snes(snes) _snes(snes)
{ } { }
void MainQTWidget::_onUpdate() void QtFullSFML::_onUpdate()
{ {
this->_snes.update(); this->_snes.update();
} }
+34 -15
View File
@@ -14,14 +14,46 @@
namespace ComSquare::Renderer namespace ComSquare::Renderer
{ {
//! @brief A SFML renderer inside a QT window. //! @brief The SFML window that manage the update.
class QtSFML : public SFRenderer { class QtFullSFML : public QtWidgetSFML {
private: private:
//! @brief The snes to update.
SNES &_snes;
void _onUpdate() override;
public:
QtFullSFML(SNES &snes, QWidget* parent, const QPoint& position, const QSize& size, int frameRate = 0);
QtFullSFML(const QtFullSFML &) = delete;
QtFullSFML &operator=(const QtFullSFML &) = delete;
~QtFullSFML() override = default;
};
//! @brief A SFML renderer inside a QT window.
class QtSFML : public IRenderer {
private:
//! @brief The QT app instance.
QApplication &_app; QApplication &_app;
//! @brief The SFML frame.
QFrame *frame{};
//! @brief The SFML widget.
QtFullSFML *_sfWidget{};
//! @brief The width of the window.
unsigned int width;
//! @brief The height of the window.
unsigned int height;
public: public:
//! @brief Use this function to create the window. //! @brief Use this function to create the window.
//! @param maxFPS The number of FPS you aim to run on. //! @param maxFPS The number of FPS you aim to run on.
void createWindow(SNES &snes, int maxFPS) override; void createWindow(SNES &snes, int maxFPS) override;
//! @brief Set a new name to the window, if there is already a name it will be overwrite.
//! @param newWindowName new title for the window.
void setWindowName(std::string newWindowName) override;
//! @brief Add a pixel to the buffer to the coordinates x, y with the color rgba.
//! @param X horizontal index.
//! @param Y vertical index.
//! @param rgba The color of the pixel.
void putPixel(unsigned y, unsigned x, uint32_t rgba) override;
//! @brief This function doesn't do anything because QT internally handle drawing to the screen.
void drawScreen() override;
//! @brief Constructor that return a SFML renderer inside a QT window. //! @brief Constructor that return a SFML renderer inside a QT window.
//! @param height height of the window. //! @param height height of the window.
//! @param width width of the window. //! @param width width of the window.
@@ -30,19 +62,6 @@ namespace ComSquare::Renderer
QtSFML &operator=(const QtSFML &) = delete; QtSFML &operator=(const QtSFML &) = delete;
~QtSFML() = default; ~QtSFML() = default;
}; };
//! @brief The SFML window that manage the update.
class MainQTWidget : public QtWidgetSFML {
private:
//! @brief The snes to update.
SNES &_snes;
void _onUpdate() override;
public:
MainQTWidget(SNES &snes, QWidget* parent, const QPoint& position, const QSize& size, int frameRate = 0);
MainQTWidget(const MainQTWidget &) = delete;
MainQTWidget &operator=(const MainQTWidget &) = delete;
~MainQTWidget() override = default;
};
} }
#endif //COMSQUARE_QTSFML_HPP #endif //COMSQUARE_QTSFML_HPP
+3 -4
View File
@@ -7,7 +7,7 @@
namespace ComSquare::Renderer namespace ComSquare::Renderer
{ {
QtWidgetSFML::QtWidgetSFML(QWidget *parent, const QPoint &position, const QSize &size, int frameRate) : QtWidgetSFML::QtWidgetSFML(QWidget *parent, const QPoint &position, const QSize &size, int frameRate) :
QWidget(parent) QWidget(parent), SFRenderer(size.height(), size.width())
{ {
this->setAttribute(Qt::WA_PaintOnScreen); this->setAttribute(Qt::WA_PaintOnScreen);
this->setAttribute(Qt::WA_OpaquePaintEvent); this->setAttribute(Qt::WA_OpaquePaintEvent);
@@ -29,7 +29,7 @@ namespace ComSquare::Renderer
#ifdef Q_WS_X11 #ifdef Q_WS_X11
XFlush(QX11Info::display()); XFlush(QX11Info::display());
#endif #endif
this->sf::RenderWindow::create(this->winId()); this->_window.create(this->winId());
this->_onInit(); this->_onInit();
connect(&_timer, SIGNAL(timeout()), this, SLOT(repaint())); connect(&_timer, SIGNAL(timeout()), this, SLOT(repaint()));
@@ -45,9 +45,8 @@ namespace ComSquare::Renderer
void QtWidgetSFML::paintEvent(QPaintEvent *) void QtWidgetSFML::paintEvent(QPaintEvent *)
{ {
this->clear();
this->_onUpdate(); this->_onUpdate();
this->display(); this->drawScreen();
} }
void QtWidgetSFML::_onInit(){ } void QtWidgetSFML::_onInit(){ }
+11 -1
View File
@@ -8,19 +8,29 @@
#include <QtWidgets/QWidget> #include <QtWidgets/QWidget>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <QtCore/QTimer> #include <QtCore/QTimer>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include "../SFRenderer.hpp"
namespace ComSquare::Renderer namespace ComSquare::Renderer
{ {
//! @brief A widget that you can put inside a QT application that render using the SFML. //! @brief A widget that you can put inside a QT application that render using the SFML.
class QtWidgetSFML : public QWidget, public sf::RenderWindow { class QtWidgetSFML : public QWidget, public SFRenderer {
private: private:
//! @brief Function called when this widget is created.
virtual void _onInit(); virtual void _onInit();
//! @brief Function called to update this widget.
virtual void _onUpdate() = 0; virtual void _onUpdate() = 0;
//! @brief Qt internal paint engine (always null since we use a custom one)
QPaintEngine* paintEngine() const override; QPaintEngine* paintEngine() const override;
//! @brief Used to create the SF window and bind it to the window manager of the user.
void showEvent(QShowEvent*) override; void showEvent(QShowEvent*) override;
//! @brief QT event that refresh the widget. (A draw screen)
void paintEvent(QPaintEvent*) override; void paintEvent(QPaintEvent*) override;
// @brief Internal timer used for update intervals.
QTimer _timer; QTimer _timer;
//! @brief Has the SF window been created yet.
bool _isInitialized = false; bool _isInitialized = false;
public: public:
QtWidgetSFML(QWidget* parent, const QPoint& position, const QSize& size, int frameRate = 0); QtWidgetSFML(QWidget* parent, const QPoint& position, const QSize& size, int frameRate = 0);
+8 -3
View File
@@ -14,20 +14,25 @@ namespace ComSquare::Renderer
{ {
SFRenderer::SFRenderer(unsigned int height, unsigned int width) SFRenderer::SFRenderer(unsigned int height, unsigned int width)
{ {
this->shouldExit = false;
this->_videoMode = {width, height, 32}; this->_videoMode = {width, height, 32};
this->_texture.create(width, height); this->_texture.create(width, height);
this->_sprite.setTexture(this->_texture); this->_sprite.setTexture(this->_texture);
this->_pixelBuffer = new sf::Color[height * width]; this->_pixelBuffer = new sf::Color[height * width];
} }
void SFRenderer::createWindow(SNES &, int maxFPS) void SFRenderer::createWindow(SNES &snes, int maxFPS)
{ {
sf::Image icon; sf::Image icon;
this->_window.create(this->_videoMode, "ComSquare Emulator", sf::Style::Default); this->_window.create(this->_videoMode, "ComSquare Emulator", sf::Style::Default);
if (icon.loadFromFile("../ressources/Logo.png")) if (icon.loadFromFile("resources/Logo.png"))
this->_window.setIcon(314, 314, icon.getPixelsPtr()); this->_window.setIcon(314, 314, icon.getPixelsPtr());
this->_window.setFramerateLimit(maxFPS); this->_window.setFramerateLimit(maxFPS);
this->setWindowName(snes.cartridge->header.gameName);
while (!this->shouldExit) {
snes.update();
this->getEvents();
}
} }
SFRenderer::~SFRenderer() SFRenderer::~SFRenderer()
+2
View File
@@ -37,6 +37,8 @@ namespace ComSquare::Renderer
//! @brief The texture to render the array of pixels //! @brief The texture to render the array of pixels
sf::Texture _texture; sf::Texture _texture;
public: public:
//! @brief Tells to the program if the window has been closed, and therefore if he should stop
bool shouldExit = false;
//! @brief Set a new name to the window, if there is already a name it will be overwrite. //! @brief Set a new name to the window, if there is already a name it will be overwrite.
//! @param newWindowName new title for the window. //! @param newWindowName new title for the window.
void setWindowName(std::string newWindowName) override; void setWindowName(std::string newWindowName) override;
-1
View File
@@ -18,7 +18,6 @@ namespace ComSquare
sram(new Ram::Ram(this->cartridge->header.sramSize)) sram(new Ram::Ram(this->cartridge->header.sramSize))
{ {
bus->mapComponents(*this); bus->mapComponents(*this);
renderer.setWindowName(this->cartridge->header.gameName);
} }
void SNES::enableCPUDebugging() void SNES::enableCPUDebugging()