Properly join emulator thread

This commit is contained in:
wheremyfoodat 2023-10-01 23:51:08 +03:00
parent 84044d078e
commit 061c80fd11
3 changed files with 18 additions and 6 deletions

View file

@ -5,6 +5,7 @@
#include <QMenuBar> #include <QMenuBar>
#include <QPalette> #include <QPalette>
#include <QtWidgets> #include <QtWidgets>
#include <atomic>
#include <thread> #include <thread>
#include "emulator.hpp" #include "emulator.hpp"
@ -24,9 +25,11 @@ class MainWindow : public QMainWindow {
Emulator* emu = nullptr; Emulator* emu = nullptr;
std::thread emuThread; std::thread emuThread;
std::atomic<bool> appRunning = true; // Is the application itself running?
ScreenWidget screen;
QComboBox* themeSelect = nullptr; QComboBox* themeSelect = nullptr;
QMenuBar* menuBar = nullptr; QMenuBar* menuBar = nullptr;
ScreenWidget screen;
Theme currentTheme; Theme currentTheme;
void setTheme(Theme theme); void setTheme(Theme theme);

View file

@ -4,15 +4,21 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
setWindowTitle("Alber"); setWindowTitle("Alber");
// Enable drop events for loading ROMs // Enable drop events for loading ROMs
setAcceptDrops(true); setAcceptDrops(true);
resize(400, 240 * 2); resize(800, 240 * 4);
screen.show(); screen.show();
appRunning = true;
// Set our menu bar up // Set our menu bar up
menuBar = new QMenuBar(this); menuBar = new QMenuBar(this);
setMenuBar(menuBar); setMenuBar(menuBar);
auto pandaMenu = menuBar->addMenu(tr("PANDA")); auto fileMenu = menuBar->addMenu(tr("File"));
auto pandaAction = pandaMenu->addAction(tr("panda...")); auto pandaAction = fileMenu->addAction(tr("panda..."));
auto emulationMenu = menuBar->addMenu(tr("Emulation"));
auto helpMenu = menuBar->addMenu(tr("Help"));
auto aboutMenu = menuBar->addMenu(tr("About"));
// Set up theme selection // Set up theme selection
setTheme(Theme::Dark); setTheme(Theme::Dark);
@ -53,7 +59,7 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
Helpers::panic("Failed to load ROM"); Helpers::panic("Failed to load ROM");
} }
while (true) { while (appRunning) {
emu->runFrame(); emu->runFrame();
swapEmuBuffer(); swapEmuBuffer();
} }
@ -70,6 +76,9 @@ void MainWindow::swapEmuBuffer() {
// Cleanup when the main window closes // Cleanup when the main window closes
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
appRunning = false; // Set our running atomic to false in order to make the emulator thread stop, and join it
emuThread.join();
delete emu; delete emu;
delete menuBar; delete menuBar;
delete themeSelect; delete themeSelect;

View file

@ -20,7 +20,7 @@
#ifdef PANDA3DS_ENABLE_OPENGL #ifdef PANDA3DS_ENABLE_OPENGL
ScreenWidget::ScreenWidget(QWidget* parent) : QWidget(parent) { ScreenWidget::ScreenWidget(QWidget* parent) : QWidget(parent) {
// Create a native window for use with our graphics API of choice // Create a native window for use with our graphics API of choice
resize(400, 240 * 2); resize(800, 240 * 4);
setAutoFillBackground(false); setAutoFillBackground(false);
setAttribute(Qt::WA_NativeWindow, true); setAttribute(Qt::WA_NativeWindow, true);