From 6d4f1175ba09282f1cccff5646866b10c22521f1 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Mon, 14 Nov 2016 03:41:48 +0100 Subject: [PATCH] Fix input grabbing logic, add related user setting, minor cleanup --- Aquaria/DSQ.cpp | 17 +------- Aquaria/DSQ.h | 2 - Aquaria/UserSettings.cpp | 12 ++++++ Aquaria/UserSettings.h | 3 +- BBGE/Core.cpp | 87 ++++++++++++++-------------------------- BBGE/Core.h | 9 ++--- 6 files changed, 48 insertions(+), 82 deletions(-) diff --git a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp index 8b157da..3c23dae 100644 --- a/Aquaria/DSQ.cpp +++ b/Aquaria/DSQ.cpp @@ -790,8 +790,6 @@ void DSQ::init() { core->settings.runInBackground = true; - weird = 0; - #ifdef BBGE_BUILD_WINDOWS /* const std::string welcomeMessage = \ @@ -902,19 +900,6 @@ This build is not yet final, and as such there are a couple things lacking. They else debugLog("VoiceOvers Disabled"); - -#ifdef _DEBUG - if (!createWindow(800, 600, user.video.bits, false, "Aquaria")) -#else - if (!createWindow(user.video.resx, user.video.resy, user.video.bits, user.video.full, "Aquaria")) -#endif - { - exit_error("Failed to create window"); - return; - } - - - SDL_Init(SDL_INIT_VIDEO); if (fullscreen && !sdlVideoModeOK(user.video.resx, user.video.resy, user.video.bits)) { @@ -1386,7 +1371,7 @@ This build is not yet final, and as such there are a couple things lacking. They core->afterEffectManager = 0; bindInput(); - setInputGrab(1); + setInputGrab(user.system.grabInput); // Go directly to the title in dev mode if(isDeveloperKeys()) diff --git a/Aquaria/DSQ.h b/Aquaria/DSQ.h index 8efb5c0..c87b015 100644 --- a/Aquaria/DSQ.h +++ b/Aquaria/DSQ.h @@ -466,8 +466,6 @@ public: void bindInput(); - int weird; - void setCutscene(bool on, bool canSkip=false); bool isInCutscene(); bool isCutscenePaused(); diff --git a/Aquaria/UserSettings.cpp b/Aquaria/UserSettings.cpp index fafd619..78ec7cf 100644 --- a/Aquaria/UserSettings.cpp +++ b/Aquaria/UserSettings.cpp @@ -63,6 +63,12 @@ void UserSettings::save() xml_unsafe->SetAttribute("on", system.allowDangerousScriptFunctions); } xml_system->InsertEndChild(xml_unsafe); + + XMLElement *xml_grabInp = doc.NewElement("GrabInput"); + { + xml_grabInp->SetAttribute("on", system.grabInput); + } + xml_system->InsertEndChild(xml_grabInp); } doc.InsertEndChild(xml_system); @@ -387,6 +393,12 @@ void UserSettings::load(bool doApply, const std::string &overrideFile) { system.allowDangerousScriptFunctions = xml_unsafe->IntAttribute("on"); } + + XMLElement *xml_grabInp = xml_system->FirstChildElement("GrabInput"); + if (xml_grabInp) + { + system.allowDangerousScriptFunctions = xml_grabInp->IntAttribute("on"); + } } XMLElement *xml_audio = doc.FirstChildElement("Audio"); diff --git a/Aquaria/UserSettings.h b/Aquaria/UserSettings.h index f22c21e..886071d 100644 --- a/Aquaria/UserSettings.h +++ b/Aquaria/UserSettings.h @@ -37,11 +37,12 @@ class UserSettings public: struct System { - System() { debugLogOn = 0; devModeOn = 0; allowDangerousScriptFunctions = 0; } + System() { debugLogOn = 0; devModeOn = 0; allowDangerousScriptFunctions = 0; grabInput=1; } int debugLogOn; std::string locale; int devModeOn; int allowDangerousScriptFunctions; + int grabInput; } system; struct Audio diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index db9e7be..4bd36bd 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -127,8 +127,6 @@ void Core::setup_opengl() { assert(gGLctx); - SDL_SetWindowGrab(gScreen, SDL_TRUE); - glViewport(0, 0, width, height); SDL_ShowCursor(SDL_DISABLE); @@ -413,7 +411,7 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n debugLogTextures = true; - grabInputOnReentry = -1; + grabInput = false; srand(time(NULL)); old_dt = 0; @@ -564,28 +562,22 @@ Core::~Core() core = 0; } -void Core::setInputGrab(bool on) +void Core::updateInputGrab() { - if (isWindowFocus()) - { - #ifdef BBGE_BUILD_SDL2 - SDL_SetWindowGrab(gScreen, on ? SDL_TRUE : SDL_FALSE); - #else - SDL_WM_GrabInput(on?SDL_GRAB_ON:SDL_GRAB_OFF); - #endif - } + // Can and MUST always ungrab if window is not in focus + const bool on = grabInput && isWindowFocus(); + + #ifdef BBGE_BUILD_SDL2 + SDL_SetWindowGrab(gScreen, on ? SDL_TRUE : SDL_FALSE); + #else + SDL_WM_GrabInput(on?SDL_GRAB_ON:SDL_GRAB_OFF); + #endif } -void Core::setReentryInputGrab(int on) +void Core::setInputGrab(bool on) { - if (grabInputOnReentry == -1) - { - setInputGrab(on); - } - else - { - setInputGrab(grabInputOnReentry); - } + grabInput = on; + updateInputGrab(); } bool Core::isFullscreen() @@ -940,27 +932,23 @@ void Core::shutdownSoundLibrary() { } -void Core::shutdownGraphicsLibrary(bool killVideo) +void Core::shutdownGraphicsLibrary() { - glFinish(); - if (killVideo) { - #ifdef BBGE_BUILD_SDL2 - SDL_SetWindowGrab(gScreen, SDL_FALSE); - SDL_GL_MakeCurrent(gScreen, NULL); - SDL_GL_DeleteContext(gGLctx); - SDL_DestroyWindow(gScreen); - gGLctx = 0; - SDL_QuitSubSystem(SDL_INIT_VIDEO); - #else - SDL_QuitSubSystem(SDL_INIT_VIDEO); - SDL_WM_GrabInput(SDL_GRAB_OFF); - #endif + setInputGrab(false); - gScreen = 0; -#if BBGE_BUILD_OPENGL_DYNAMIC - unload_all_glsyms(); + glFinish(); + +#ifdef BBGE_BUILD_SDL2 + SDL_GL_MakeCurrent(gScreen, NULL); + SDL_GL_DeleteContext(gGLctx); + SDL_DestroyWindow(gScreen); + gGLctx = 0; +#endif + SDL_QuitSubSystem(SDL_INIT_VIDEO); + gScreen = 0; +#if BBGE_BUILD_OPENGL_DYNAMIC + unload_all_glsyms(); #endif - } _hasFocus = false; @@ -1011,16 +999,6 @@ void centerWindow(HWND hwnd) #endif - -bool Core::createWindow(int width, int height, int bits, bool fullscreen, std::string windowTitle) -{ - this->width = width; - this->height = height; - return true; - - -} - // No longer part of C/C++ standard #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -1271,9 +1249,7 @@ void Core::run(float runTime) if (wasInactive) { debugLog("WINDOW ACTIVE"); - - setReentryInputGrab(1); - + updateInputGrab(); wasInactive = false; } } @@ -1286,9 +1262,7 @@ void Core::run(float runTime) wasInactive = true; _hasFocus = false; - - setReentryInputGrab(0); - + updateInputGrab(); sound->pause(); while (!isWindowFocus()) @@ -1533,8 +1507,7 @@ void Core::pollEvents(float dt) if ((event.key.keysym.sym == SDLK_g) && (event.key.keysym.mod & KMOD_CTRL)) { - grabInputOnReentry = (grabInputOnReentry)?0:-1; - setReentryInputGrab(1); + setInputGrab(!grabInput); } else if (_hasFocus) { diff --git a/BBGE/Core.h b/BBGE/Core.h index cf7870c..67157cf 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -210,8 +210,6 @@ public: void applyState(const std::string &state); - bool createWindow(int width, int height, int bits, bool fullscreen, std::string windowTitle=""); - void clearBuffers(); void render(int startLayer=-1, int endLayer=-1, bool useFrameBufferIfAvail=true); void showBuffer(); @@ -263,8 +261,6 @@ public: void setMouseConstraint(bool on); void setMouseConstraintCircle(const Vector& pos, float mouseCircle); - void setReentryInputGrab(int on); - virtual void action(int id, int state, int source){} bool exists(const std::string &file); @@ -415,6 +411,7 @@ public: bool debugLogActive; void setInputGrab(bool on); + void updateInputGrab(); bool isFullscreen(); @@ -450,7 +447,7 @@ protected: std::string userDataFolder; - int grabInputOnReentry; + bool grabInput; int virtualOffX, virtualOffY; @@ -487,7 +484,7 @@ protected: bool initGraphicsLibrary(int w, int h, bool fullscreen, bool vsync, int bpp); void shutdownInputLibrary(); void shutdownJoystickLibrary(); - void shutdownGraphicsLibrary(bool kill=true); + void shutdownGraphicsLibrary(); void shutdownSoundLibrary(); void detectJoysticks();