Automatically pause game when GUI open to better manage event queue

This commit is contained in:
rainmakerv3 2025-06-24 20:34:03 +08:00
parent b74eb796fe
commit db65bf48c4
3 changed files with 23 additions and 19 deletions

View File

@ -8,6 +8,7 @@
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/path_util.h" #include "common/path_util.h"
#include "control_settings.h" #include "control_settings.h"
#include "sdl_window.h"
#include "ui_control_settings.h" #include "ui_control_settings.h"
ControlSettings::ControlSettings(std::shared_ptr<GameInfoClass> game_info_get, bool isGameRunning, ControlSettings::ControlSettings(std::shared_ptr<GameInfoClass> game_info_get, bool isGameRunning,
@ -22,6 +23,9 @@ ControlSettings::ControlSettings(std::shared_ptr<GameInfoClass> game_info_get, b
SDL_InitSubSystem(SDL_INIT_EVENTS); SDL_InitSubSystem(SDL_INIT_EVENTS);
} else { } else {
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
SDL_Event pauseGame{};
pauseGame.type = SDL_EVENT_TOGGLE_PAUSE;
SDL_PushEvent(&pauseGame);
} }
CheckGamePad(); CheckGamePad();
@ -775,19 +779,6 @@ bool ControlSettings::eventFilter(QObject* obj, QEvent* event) {
} }
void ControlSettings::processSDLEvents(int Type, int Input, int Value) { 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 (Type == SDL_EVENT_GAMEPAD_ADDED) {
if (!GameRunning) if (!GameRunning)
CheckGamePad(); 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() {} ControlSettings::~ControlSettings() {}

View File

@ -44,6 +44,7 @@ private:
void SetMapping(QString input); void SetMapping(QString input);
void DisableMappingButtons(); void DisableMappingButtons();
void EnableMappingButtons(); void EnableMappingButtons();
void cleanup();
QList<QPushButton*> ButtonsList; QList<QPushButton*> ButtonsList;
QList<QPushButton*> AxisList; QList<QPushButton*> AxisList;
@ -73,8 +74,6 @@ private:
protected: protected:
void closeEvent(QCloseEvent* event) override { void closeEvent(QCloseEvent* event) override {
SDL_Event quitLoop{}; cleanup();
quitLoop.type = SDL_EVENT_QUIT;
SDL_PushEvent(&quitLoop);
} }
}; };

View File

@ -18,9 +18,6 @@ Wrapper* Wrapper::GetInstance() {
} }
bool Wrapper::ProcessEvent(SDL_Event* event) { bool Wrapper::ProcessEvent(SDL_Event* event) {
if (!wrapperActive)
return false;
switch (event->type) { switch (event->type) {
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
emit SDLEvent(SDL_EVENT_QUIT, 0, 0); emit SDLEvent(SDL_EVENT_QUIT, 0, 0);