added mouse input
authormatthew <matthew@owens.tech>
Sat, 3 Nov 2018 16:20:06 +0000 (16:20 +0000)
committermatthew <matthew@owens.tech>
Sat, 3 Nov 2018 16:20:06 +0000 (16:20 +0000)
gl/input.c
gl/input.h

index c3c97a2..604de3c 100644 (file)
@@ -2,13 +2,25 @@
 #include "err.h"
 #include <SDL2/SDL.h>
 
-typedef struct InputDevice{
+typedef struct KeyboardDevice{
        Uint8 *state;
        Uint8 *prevState;
        Uint8 *binds;
-}InputDevice;
+}KeyboardDevice;
 
-static InputDevice kb = {NULL,NULL,NULL};
+typedef struct MouseState{
+       Uint8 buttons;
+       int x, y;
+       int relX, relY;         // the xy coords relative to the last frame
+}MouseState;
+
+typedef struct MouseDevice{
+       MouseState state;
+       MouseState prevState;
+}MouseDevice;
+
+static KeyboardDevice kb = {NULL,NULL,NULL};
+static MouseDevice m;  //Setting buttonCount to 5, as SDL supports 5 mouse buttons
 
 bool devicesNotInitilised()
 {
@@ -34,6 +46,16 @@ void loadKeybinds()
        kb.binds[INPUT_CAMERA_PAN_OUT] = SDL_SCANCODE_W;
 }
 
+vec2_t crpgInputMousePos()
+{
+       return vec2(m.state.x, m.state.y);
+}
+
+vec2_t crpgInputMouseRelPos()
+{
+       return vec2(m.state.relX, m.state.relY);
+}
+
 bool crpgInputPressed(int action)
 {
        if(devicesNotInitilised()){
@@ -79,4 +101,9 @@ void crpgInputUpdate()
        //SDL_PumpEvents();
        kb.prevState = kb.state;
        kb.state = SDL_GetKeyboardState(NULL);
+
+       m.prevState = m.state;
+       //m.state.buttons = SDL_GetRelativeMouseState(&(m.state.x), &(m.state.y));
+       m.state.buttons = SDL_GetMouseState(&(m.state.x), &(m.state.y));
+       SDL_GetRelativeMouseState(&(m.state.relX), &(m.state.relY));
 }
index 3d6ae21..5b7cbaa 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef INPUT_H
 #define INPUT_H
 #include <stdbool.h>
+#include "math_3d.h"
 
 typedef enum crpgInputActions{
        INPUT_CAMERA_PAN_DOWN = 0,
@@ -19,4 +20,6 @@ void crpgInputCleanup();
 bool crpgInputPressed(int action);
 bool crpgInputHeld(int action);
 bool crpgInputReleased(int action);
+vec2_t crpgInputMousePos();
+vec2_t crpgInputMouseRelPos();
 #endif//INPUT_H