#include "input.h"
#include "err.h"
-#include <SDL2/SDL.h>
typedef struct KeyboardDevice{
Uint8 state[SDL_NUM_SCANCODES];
// 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()
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);
+ }
}
#define INPUT_H
#include <stdbool.h>
#include "math_3d.h"
+#include <SDL2/SDL.h>
typedef enum crpgInputActions{
INPUT_CAMERA_PAN_DOWN = 0,
}crpgInputActions;
void crpgInputInit();
-void crpgInputUpdate();
+void crpgInputUpdate(Uint32 windowFlags);
void crpgInputCleanup();
bool crpgInputPressed(int action);
}
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());
quit = true;
}
}
- crpgInputUpdate();
+ crpgInputUpdate(SDL_GetWindowFlags(window));
// Updating physics as many times as we need to consume dt
while(crpgTimeStepPhysRequired(physUpdates)){