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() { void MainWindow::StartGame() {
isGameRunning = true;
BackgroundMusicPlayer::getInstance().stopMusic(); BackgroundMusicPlayer::getInstance().stopMusic();
QString gamePath = ""; QString gamePath = "";
int table_mode = Config::getTableMode(); int table_mode = Config::getTableMode();
@ -579,13 +578,12 @@ void MainWindow::StartGame() {
} }
if (gamePath != "") { if (gamePath != "") {
AddRecentFiles(gamePath); AddRecentFiles(gamePath);
Core::Emulator emulator;
const auto path = Common::FS::PathFromQString(gamePath); const auto path = Common::FS::PathFromQString(gamePath);
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found"))); QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found")));
return; return;
} }
emulator.Run(path); StartEmulator(path);
} }
} }
@ -682,13 +680,12 @@ void MainWindow::BootGame() {
QString(tr("Only one file can be selected!"))); QString(tr("Only one file can be selected!")));
} else { } else {
std::filesystem::path path = Common::FS::PathFromQString(fileNames[0]); std::filesystem::path path = Common::FS::PathFromQString(fileNames[0]);
Core::Emulator emulator;
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
QMessageBox::critical(nullptr, tr("Run Game"), QMessageBox::critical(nullptr, tr("Run Game"),
QString(tr("Eboot.bin file not found"))); QString(tr("Eboot.bin file not found")));
return; return;
} }
emulator.Run(path); StartEmulator(path);
} }
} }
} }
@ -1042,12 +1039,11 @@ void MainWindow::CreateRecentGameActions() {
connect(m_recent_files_group, &QActionGroup::triggered, this, [this](QAction* action) { connect(m_recent_files_group, &QActionGroup::triggered, this, [this](QAction* action) {
auto gamePath = Common::FS::PathFromQString(action->text()); auto gamePath = Common::FS::PathFromQString(action->text());
AddRecentFiles(action->text()); // Update the list. AddRecentFiles(action->text()); // Update the list.
Core::Emulator emulator;
if (!std::filesystem::exists(gamePath)) { if (!std::filesystem::exists(gamePath)) {
QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found"))); QMessageBox::critical(nullptr, tr("Run Game"), QString(tr("Eboot.bin file not found")));
return; return;
} }
emulator.Run(gamePath); StartEmulator(gamePath);
}); });
} }
@ -1096,3 +1092,16 @@ bool MainWindow::eventFilter(QObject* obj, QEvent* event) {
} }
return QMainWindow::eventFilter(obj, 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 LoadTranslation();
void PlayBackgroundMusic(); void PlayBackgroundMusic();
QIcon RecolorIcon(const QIcon& icon, bool isWhite); QIcon RecolorIcon(const QIcon& icon, bool isWhite);
void StartEmulator(std::filesystem::path);
bool isIconBlack = false; bool isIconBlack = false;
bool isTableList = true; bool isTableList = true;
bool isGameRunning = false; bool isGameRunning = false;