mirror of
https://github.com/zoriya/ComSquare.git
synced 2026-06-05 10:59:38 +00:00
Finishing the bases of the QT renderer
This commit is contained in:
@@ -17,9 +17,6 @@ namespace ComSquare
|
||||
//! @brief Set a new name to the window, if there is already a name it will be overwrite
|
||||
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
|
||||
virtual void drawScreen() = 0;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QFrame>
|
||||
#include <QCloseEvent>
|
||||
#include <QIcon>
|
||||
#include <iostream>
|
||||
#include "QtSFML.hpp"
|
||||
|
||||
@@ -15,24 +15,37 @@
|
||||
|
||||
namespace ComSquare::Renderer
|
||||
{
|
||||
QtSFML::QtSFML(QApplication &app, unsigned int height, unsigned int width) :
|
||||
SFRenderer(height, width), _app(app)
|
||||
QtSFML::QtSFML(QApplication &app, unsigned int h, unsigned int w) :
|
||||
_app(app), _sfWidget(nullptr), width(w), height(h)
|
||||
{ }
|
||||
|
||||
void QtSFML::createWindow(SNES &snes, int maxFPS)
|
||||
{
|
||||
QFrame *frame = new QFrame();
|
||||
frame->show();
|
||||
MainQTWidget *sfView = new MainQTWidget(snes, frame, QPoint(0, 0), QSize(this->_videoMode.width, this->_videoMode.height), maxFPS);
|
||||
sfView->show();
|
||||
this->frame = new QFrame();
|
||||
this->setWindowName(snes.cartridge->header.gameName);
|
||||
this->frame->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),
|
||||
_snes(snes)
|
||||
{ }
|
||||
|
||||
void MainQTWidget::_onUpdate()
|
||||
void QtFullSFML::_onUpdate()
|
||||
{
|
||||
this->_snes.update();
|
||||
}
|
||||
|
||||
@@ -14,14 +14,46 @@
|
||||
|
||||
namespace ComSquare::Renderer
|
||||
{
|
||||
//! @brief A SFML renderer inside a QT window.
|
||||
class QtSFML : public SFRenderer {
|
||||
//! @brief The SFML window that manage the update.
|
||||
class QtFullSFML : public QtWidgetSFML {
|
||||
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;
|
||||
//! @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:
|
||||
//! @brief Use this function to create the window.
|
||||
//! @param maxFPS The number of FPS you aim to run on.
|
||||
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.
|
||||
//! @param height height of the window.
|
||||
//! @param width width of the window.
|
||||
@@ -30,19 +62,6 @@ namespace ComSquare::Renderer
|
||||
QtSFML &operator=(const QtSFML &) = delete;
|
||||
~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
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace ComSquare::Renderer
|
||||
{
|
||||
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_OpaquePaintEvent);
|
||||
@@ -29,7 +29,7 @@ namespace ComSquare::Renderer
|
||||
#ifdef Q_WS_X11
|
||||
XFlush(QX11Info::display());
|
||||
#endif
|
||||
this->sf::RenderWindow::create(this->winId());
|
||||
this->_window.create(this->winId());
|
||||
this->_onInit();
|
||||
|
||||
connect(&_timer, SIGNAL(timeout()), this, SLOT(repaint()));
|
||||
@@ -45,9 +45,8 @@ namespace ComSquare::Renderer
|
||||
|
||||
void QtWidgetSFML::paintEvent(QPaintEvent *)
|
||||
{
|
||||
this->clear();
|
||||
this->_onUpdate();
|
||||
this->display();
|
||||
this->drawScreen();
|
||||
}
|
||||
|
||||
void QtWidgetSFML::_onInit(){ }
|
||||
|
||||
@@ -8,19 +8,29 @@
|
||||
#include <QtWidgets/QWidget>
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <QtCore/QTimer>
|
||||
#include <SFML/Graphics/Sprite.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include "../SFRenderer.hpp"
|
||||
|
||||
namespace ComSquare::Renderer
|
||||
{
|
||||
//! @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:
|
||||
//! @brief Function called when this widget is created.
|
||||
virtual void _onInit();
|
||||
//! @brief Function called to update this widget.
|
||||
virtual void _onUpdate() = 0;
|
||||
//! @brief Qt internal paint engine (always null since we use a custom one)
|
||||
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;
|
||||
//! @brief QT event that refresh the widget. (A draw screen)
|
||||
void paintEvent(QPaintEvent*) override;
|
||||
|
||||
// @brief Internal timer used for update intervals.
|
||||
QTimer _timer;
|
||||
//! @brief Has the SF window been created yet.
|
||||
bool _isInitialized = false;
|
||||
public:
|
||||
QtWidgetSFML(QWidget* parent, const QPoint& position, const QSize& size, int frameRate = 0);
|
||||
|
||||
@@ -14,20 +14,25 @@ namespace ComSquare::Renderer
|
||||
{
|
||||
SFRenderer::SFRenderer(unsigned int height, unsigned int width)
|
||||
{
|
||||
this->shouldExit = false;
|
||||
this->_videoMode = {width, height, 32};
|
||||
this->_texture.create(width, height);
|
||||
this->_sprite.setTexture(this->_texture);
|
||||
this->_pixelBuffer = new sf::Color[height * width];
|
||||
}
|
||||
|
||||
void SFRenderer::createWindow(SNES &, int maxFPS)
|
||||
void SFRenderer::createWindow(SNES &snes, int maxFPS)
|
||||
{
|
||||
sf::Image icon;
|
||||
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.setFramerateLimit(maxFPS);
|
||||
this->setWindowName(snes.cartridge->header.gameName);
|
||||
|
||||
while (!this->shouldExit) {
|
||||
snes.update();
|
||||
this->getEvents();
|
||||
}
|
||||
}
|
||||
|
||||
SFRenderer::~SFRenderer()
|
||||
|
||||
@@ -37,6 +37,8 @@ namespace ComSquare::Renderer
|
||||
//! @brief The texture to render the array of pixels
|
||||
sf::Texture _texture;
|
||||
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.
|
||||
//! @param newWindowName new title for the window.
|
||||
void setWindowName(std::string newWindowName) override;
|
||||
|
||||
Reference in New Issue
Block a user