From db65bf48c407bb1834760ddde05b0eb82fe9967e Mon Sep 17 00:00:00 2001 From: rainmakerv3 <30595646+rainmakerv3@users.noreply.github.com> Date: Tue, 24 Jun 2025 20:34:03 +0800 Subject: [PATCH] Automatically pause game when GUI open to better manage event queue --- src/qt_gui/control_settings.cpp | 34 ++++++++++++++++++++------------ src/qt_gui/control_settings.h | 5 ++--- src/qt_gui/sdl_event_wrapper.cpp | 3 --- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/qt_gui/control_settings.cpp b/src/qt_gui/control_settings.cpp index 0ce6bfb37..e743e6f2d 100644 --- a/src/qt_gui/control_settings.cpp +++ b/src/qt_gui/control_settings.cpp @@ -8,6 +8,7 @@ #include "common/logging/log.h" #include "common/path_util.h" #include "control_settings.h" +#include "sdl_window.h" #include "ui_control_settings.h" ControlSettings::ControlSettings(std::shared_ptr game_info_get, bool isGameRunning, @@ -22,6 +23,9 @@ ControlSettings::ControlSettings(std::shared_ptr game_info_get, b SDL_InitSubSystem(SDL_INIT_EVENTS); } else { SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); + SDL_Event pauseGame{}; + pauseGame.type = SDL_EVENT_TOGGLE_PAUSE; + SDL_PushEvent(&pauseGame); } CheckGamePad(); @@ -775,19 +779,6 @@ bool ControlSettings::eventFilter(QObject* obj, QEvent* event) { } void ControlSettings::processSDLEvents(int Type, int Input, int Value) { - if (Type == SDL_EVENT_QUIT) { - SdlEventWrapper::Wrapper::wrapperActive = false; - if (gamepad) - SDL_CloseGamepad(gamepad); - if (!GameRunning) { - SDL_QuitSubSystem(SDL_INIT_GAMEPAD); - SDL_QuitSubSystem(SDL_INIT_EVENTS); - SDL_Quit(); - } else { - SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "0"); - } - } - if (Type == SDL_EVENT_GAMEPAD_ADDED) { if (!GameRunning) CheckGamePad(); @@ -908,4 +899,21 @@ void ControlSettings::pollSDLEvents() { } } +void ControlSettings::cleanup() { + SdlEventWrapper::Wrapper::wrapperActive = false; + if (gamepad) + SDL_CloseGamepad(gamepad); + + if (!GameRunning) { + SDL_QuitSubSystem(SDL_INIT_GAMEPAD); + SDL_QuitSubSystem(SDL_INIT_EVENTS); + SDL_Quit(); + } else { + SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "0"); + SDL_Event resumeGame{}; + resumeGame.type = SDL_EVENT_TOGGLE_PAUSE; + SDL_PushEvent(&resumeGame); + } +} + ControlSettings::~ControlSettings() {} diff --git a/src/qt_gui/control_settings.h b/src/qt_gui/control_settings.h index 4cb7ef88b..925d12f62 100644 --- a/src/qt_gui/control_settings.h +++ b/src/qt_gui/control_settings.h @@ -44,6 +44,7 @@ private: void SetMapping(QString input); void DisableMappingButtons(); void EnableMappingButtons(); + void cleanup(); QList ButtonsList; QList AxisList; @@ -73,8 +74,6 @@ private: protected: void closeEvent(QCloseEvent* event) override { - SDL_Event quitLoop{}; - quitLoop.type = SDL_EVENT_QUIT; - SDL_PushEvent(&quitLoop); + cleanup(); } }; diff --git a/src/qt_gui/sdl_event_wrapper.cpp b/src/qt_gui/sdl_event_wrapper.cpp index 41ca84c52..736a29ea8 100644 --- a/src/qt_gui/sdl_event_wrapper.cpp +++ b/src/qt_gui/sdl_event_wrapper.cpp @@ -18,9 +18,6 @@ Wrapper* Wrapper::GetInstance() { } bool Wrapper::ProcessEvent(SDL_Event* event) { - if (!wrapperActive) - return false; - switch (event->type) { case SDL_EVENT_QUIT: emit SDLEvent(SDL_EVENT_QUIT, 0, 0);