From c1fe8e9cd1bb86bb8deeb4909cf833fe4db88ba6 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:22:33 +0300 Subject: [PATCH] Switch to std::function for resize callback --- include/panda_qt/screen.hpp | 7 ++++++- src/panda_qt/main_window.cpp | 3 ++- src/panda_qt/screen.cpp | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/panda_qt/screen.hpp b/include/panda_qt/screen.hpp index a39b5076..1ed4966b 100644 --- a/include/panda_qt/screen.hpp +++ b/include/panda_qt/screen.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include "gl/context.h" @@ -10,7 +11,9 @@ class ScreenWidget : public QWidget { Q_OBJECT public: - ScreenWidget(QWidget* parent = nullptr); + using ResizeCallback = std::function; + + ScreenWidget(ResizeCallback resizeCallback, QWidget* parent = nullptr); void resizeEvent(QResizeEvent* event) override; // Called by the emulator thread for resizing the actual GL surface, since the emulator thread owns the GL context void resizeSurface(u32 width, u32 height); @@ -28,6 +31,8 @@ class ScreenWidget : public QWidget { private: std::unique_ptr glContext = nullptr; + ResizeCallback resizeCallback; + bool createGLContext(); qreal devicePixelRatioFromScreen() const; diff --git a/src/panda_qt/main_window.cpp b/src/panda_qt/main_window.cpp index 18e70a12..1f9b8123 100644 --- a/src/panda_qt/main_window.cpp +++ b/src/panda_qt/main_window.cpp @@ -17,7 +17,8 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent) setAcceptDrops(true); resize(800, 240 * 4); - screen = new ScreenWidget(this); + // We pass a callback to the screen widget that will be triggered every time we resize the screen + screen = new ScreenWidget([this](u32 width, u32 height) { handleScreenResize(width, height); }, this); setCentralWidget(screen); screen->show(); diff --git a/src/panda_qt/screen.cpp b/src/panda_qt/screen.cpp index f4ddd041..08ca67c9 100644 --- a/src/panda_qt/screen.cpp +++ b/src/panda_qt/screen.cpp @@ -20,7 +20,7 @@ // and https://github.com/melonDS-emu/melonDS/blob/master/src/frontend/qt_sdl/main.cpp #ifdef PANDA3DS_ENABLE_OPENGL -ScreenWidget::ScreenWidget(QWidget* parent) : QWidget(parent) { +ScreenWidget::ScreenWidget(ResizeCallback resizeCallback, QWidget* parent) : QWidget(parent), resizeCallback(resizeCallback) { // Create a native window for use with our graphics API of choice resize(800, 240 * 4); @@ -49,7 +49,7 @@ void ScreenWidget::resizeEvent(QResizeEvent* event) { } // This will call take care of calling resizeSurface from the emulator thread - static_cast(parentWidget())->handleScreenResize(surfaceWidth, surfaceHeight); + resizeCallback(surfaceWidth, surfaceHeight); } // Note: This will run on the emulator thread, we don't want any Qt calls happening there.