From: matthew Date: Sun, 4 Nov 2018 12:26:09 +0000 (+0000) Subject: Restricted input to when the window is focussed X-Git-Url: https://git.owens.tech///git?a=commitdiff_plain;h=6fbe9f15699412416355e97f5db5e5284a9d1045;p=csrpg.git Restricted input to when the window is focussed --- diff --git a/gl/input.c b/gl/input.c index e1d7111..05b34e9 100644 --- a/gl/input.c +++ b/gl/input.c @@ -1,6 +1,5 @@ #include "input.h" #include "err.h" -#include typedef struct KeyboardDevice{ Uint8 state[SDL_NUM_SCANCODES]; @@ -93,7 +92,7 @@ void crpgInputInit() // constraining the mouse to the window // TODO: only set relative mouse mode once the mouse is in the window, disable input otherwise - SDL_SetRelativeMouseMode(SDL_TRUE); + //SDL_SetRelativeMouseMode(SDL_TRUE); } void crpgInputCleanup() @@ -101,12 +100,25 @@ void crpgInputCleanup() free(kb.binds); } -void crpgInputUpdate() +void crpgInputUpdate(Uint32 windowFlags) { - memcpy(kb.prevState, kb.state, sizeof(Uint8)*SDL_NUM_SCANCODES); - memcpy(kb.state, SDL_GetKeyboardState(NULL), sizeof(Uint8)*SDL_NUM_SCANCODES); + bool windowShown = (windowFlags & SDL_WINDOW_SHOWN) ? true : false; + bool windowMinimized = (windowFlags & SDL_WINDOW_MINIMIZED) ? true : false; + bool windowInputFocus = (windowFlags & SDL_WINDOW_INPUT_FOCUS) ? true : false; + + if(!windowShown || windowMinimized) + return; - m.prevState = m.state; - m.state.buttons = SDL_GetMouseState(&(m.state.x), &(m.state.y)); - SDL_GetRelativeMouseState(&(m.state.relX), &(m.state.relY)); + if(windowInputFocus){ + memcpy(kb.prevState, kb.state, sizeof(Uint8)*SDL_NUM_SCANCODES); + memcpy(kb.state, SDL_GetKeyboardState(NULL), sizeof(Uint8)*SDL_NUM_SCANCODES); + + m.prevState = m.state; + + SDL_SetRelativeMouseMode(SDL_TRUE); + m.state.buttons = SDL_GetMouseState(&(m.state.x), &(m.state.y)); + SDL_GetRelativeMouseState(&(m.state.relX), &(m.state.relY)); + } else { + SDL_SetRelativeMouseMode(SDL_FALSE); + } } diff --git a/gl/input.h b/gl/input.h index 5b7cbaa..66dfea2 100644 --- a/gl/input.h +++ b/gl/input.h @@ -2,6 +2,7 @@ #define INPUT_H #include #include "math_3d.h" +#include typedef enum crpgInputActions{ INPUT_CAMERA_PAN_DOWN = 0, @@ -14,7 +15,7 @@ typedef enum crpgInputActions{ }crpgInputActions; void crpgInputInit(); -void crpgInputUpdate(); +void crpgInputUpdate(Uint32 windowFlags); void crpgInputCleanup(); bool crpgInputPressed(int action); diff --git a/gl/main.c b/gl/main.c index 24e4058..3d6ddfa 100644 --- a/gl/main.c +++ b/gl/main.c @@ -119,7 +119,8 @@ static bool init() } window = SDL_CreateWindow("csrpg - opengl", SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, screen_width, screen_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + SDL_WINDOWPOS_CENTERED, screen_width, screen_height, + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_INPUT_FOCUS); if (window == NULL){ printf("SDL couldn't make an OpenGL window! SDL_Error: %s\n", SDL_GetError()); @@ -160,7 +161,7 @@ static void update() quit = true; } } - crpgInputUpdate(); + crpgInputUpdate(SDL_GetWindowFlags(window)); // Updating physics as many times as we need to consume dt while(crpgTimeStepPhysRequired(physUpdates)){