Qt: Allocate screen on heap for setCentralWidget

This commit is contained in:
wheremyfoodat 2024-07-23 17:32:53 +03:00
parent 2e5ab26b46
commit 020a29b577
3 changed files with 15 additions and 14 deletions

View file

@ -101,7 +101,7 @@ class MainWindow : public QMainWindow {
QMenuBar* menuBar = nullptr;
InputMappings keyboardMappings;
ScreenWidget screen;
ScreenWidget* screen;
AboutWindow* aboutWindow;
ConfigWindow* configWindow;
CheatsWindow* cheatsEditor;

View file

@ -11,14 +11,16 @@
#include "input_mappings.hpp"
#include "services/dsp.hpp"
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings::defaultKeyboardMappings()), screen(this) {
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings::defaultKeyboardMappings()) {
setWindowTitle("Alber");
// Enable drop events for loading ROMs
setAcceptDrops(true);
resize(800, 240 * 4);
setCentralWidget(&screen);
screen.show();
screen = new ScreenWidget(this);
setCentralWidget(screen);
screen->show();
appRunning = true;
// Set our menu bar up
@ -70,7 +72,7 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
connect(aboutAction, &QAction::triggered, this, &MainWindow::showAboutMenu);
emu = new Emulator();
emu->setOutputSize(screen.surfaceWidth, screen.surfaceHeight);
emu->setOutputSize(screen->surfaceWidth, screen->surfaceHeight);
// Set up misc objects
aboutWindow = new AboutWindow(nullptr);
@ -102,7 +104,7 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
if (usingGL) {
// Make GL context current for this thread, enable VSync
GL::Context* glContext = screen.getGLContext();
GL::Context* glContext = screen->getGLContext();
glContext->MakeCurrent();
glContext->SetSwapInterval(emu->getConfig().vsyncEnabled ? 1 : 0);
@ -146,13 +148,13 @@ void MainWindow::emuThreadMainLoop() {
// Unbind GL context if we're using GL, otherwise some setups seem to be unable to join this thread
if (usingGL) {
screen.getGLContext()->DoneCurrent();
screen->getGLContext()->DoneCurrent();
}
}
void MainWindow::swapEmuBuffer() {
if (usingGL) {
screen.getGLContext()->SwapBuffers();
screen->getGLContext()->SwapBuffers();
} else {
Helpers::panic("[Qt] Don't know how to swap buffers for the current rendering backend :(");
}
@ -367,7 +369,7 @@ void MainWindow::dispatchMessage(const EmulatorMessage& message) {
const u32 height = message.screenSize.height;
emu->setOutputSize(width, height);
screen.resizeSurface(width, height);
screen->resizeSurface(width, height);
break;
}
}
@ -433,13 +435,13 @@ void MainWindow::keyReleaseEvent(QKeyEvent* event) {
void MainWindow::mousePressEvent(QMouseEvent* event) {
if (event->button() == Qt::MouseButton::LeftButton) {
const QPointF clickPos = event->globalPosition();
const QPointF widgetPos = screen.mapFromGlobal(clickPos);
const QPointF widgetPos = screen->mapFromGlobal(clickPos);
// Press is inside the screen area
if (widgetPos.x() >= 0 && widgetPos.x() < screen.width() && widgetPos.y() >= 0 && widgetPos.y() < screen.height()) {
if (widgetPos.x() >= 0 && widgetPos.x() < screen->width() && widgetPos.y() >= 0 && widgetPos.y() < screen->height()) {
// Go from widget positions to [0, 400) for x and [0, 480) for y
uint x = (uint)std::round(widgetPos.x() / screen.width() * 400.f);
uint y = (uint)std::round(widgetPos.y() / screen.height() * 480.f);
uint x = (uint)std::round(widgetPos.x() / screen->width() * 400.f);
uint y = (uint)std::round(widgetPos.y() / screen->height() * 480.f);
// Check if touch falls in the touch screen area
if (y >= 240 && y <= 480 && x >= 40 && x < 40 + 320) {

View file

@ -39,7 +39,6 @@ ScreenWidget::ScreenWidget(QWidget* parent) : QWidget(parent) {
void ScreenWidget::resizeEvent(QResizeEvent* event) {
previousWidth = surfaceWidth;
previousHeight = surfaceHeight;
QWidget::resizeEvent(event);
// Update surfaceWidth/surfaceHeight following the resize