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/path_util.h"
#include "control_settings.h"
#include "sdl_window.h"
#include "ui_control_settings.h"
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);
} 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() {}

View File

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

View File

@ -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);