From 020a29b5773b32e332cd64fc6d05a1600b2c27f3 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:32:53 +0300 Subject: [PATCH] Qt: Allocate screen on heap for setCentralWidget --- include/panda_qt/main_window.hpp | 2 +- src/panda_qt/main_window.cpp | 26 ++++++++++++++------------ src/panda_qt/screen.cpp | 1 - 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/panda_qt/main_window.hpp b/include/panda_qt/main_window.hpp index 717da5e7..c99fb4c2 100644 --- a/include/panda_qt/main_window.hpp +++ b/include/panda_qt/main_window.hpp @@ -101,7 +101,7 @@ class MainWindow : public QMainWindow { QMenuBar* menuBar = nullptr; InputMappings keyboardMappings; - ScreenWidget screen; + ScreenWidget* screen; AboutWindow* aboutWindow; ConfigWindow* configWindow; CheatsWindow* cheatsEditor; diff --git a/src/panda_qt/main_window.cpp b/src/panda_qt/main_window.cpp index a66222b6..18e70a12 100644 --- a/src/panda_qt/main_window.cpp +++ b/src/panda_qt/main_window.cpp @@ -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) { diff --git a/src/panda_qt/screen.cpp b/src/panda_qt/screen.cpp index b2d7caf6..78385f6c 100644 --- a/src/panda_qt/screen.cpp +++ b/src/panda_qt/screen.cpp @@ -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