gui: start the emulator in new thread (#1829)

This commit is contained in:
Quang Ngô 2024-12-22 20:47:28 +07:00 committed by GitHub
parent 45f1ea82e5
commit a081e048d9
2 changed files with 18 additions and 8 deletions

View file

@ -556,7 +556,6 @@ void MainWindow::CreateConnects() {
}
void MainWindow::StartGame() {
isGameRunning = true;
BackgroundMusicPlayer::getInstance().stopMusic();
QString gamePath = "";
int table_mode = Config::getTableMode();
@ -579,13 +578,12 @@ void MainWindow::StartGame() {
}
if (gamePath != "") {
AddRecentFiles(gamePath);
Core::Emulator emulator;
const auto path = Common::FS::PathFromQString(gamePath);
if (!std::filesystem::exists(path)) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found")));
return;
}
emulator.Run(path);
StartEmulator(path);
}
}
@ -682,13 +680,12 @@ void MainWindow::BootGame() {
QString(tr("Only one file can be selected!")));
} else {
std::filesystem::path path = Common::FS::PathFromQString(fileNames[0]);
Core::Emulator emulator;
if (!std::filesystem::exists(path)) {
QMessageBox::critical(nullptr, tr("Run Game"),
QString(tr("Eboot.bin file not found")));
return;
}
emulator.Run(path);
StartEmulator(path);
}
}
}
@ -1042,12 +1039,11 @@ void MainWindow::CreateRecentGameActions() {
connect(m_recent_files_group, &QActionGroup::triggered, this, [this](QAction* action) {
auto gamePath = Common::FS::PathFromQString(action->text());
AddRecentFiles(action->text()); // Update the list.
Core::Emulator emulator;
if (!std::filesystem::exists(gamePath)) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found")));
return;
}
emulator.Run(gamePath);
StartEmulator(gamePath);
});
}
@ -1095,4 +1091,17 @@ bool MainWindow::eventFilter(QObject* obj, QEvent* event) {
}
}
return QMainWindow::eventFilter(obj, event);
}
}
void MainWindow::StartEmulator(std::filesystem::path path) {
if (isGameRunning) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Game is already running!")));
return;
}
std::thread emulator_thread([=] {
Core::Emulator emulator;
emulator.Run(path);
});
emulator_thread.detach();
isGameRunning = true;
}

View file

@ -69,6 +69,7 @@ private:
void LoadTranslation();
void PlayBackgroundMusic();
QIcon RecolorIcon(const QIcon& icon, bool isWhite);
void StartEmulator(std::filesystem::path);
bool isIconBlack = false;
bool isTableList = true;
bool isGameRunning = false;