diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/controller.c | 37 | ||||
| -rw-r--r-- | src/controller.h | 2 | ||||
| -rw-r--r-- | src/rumble.c | 50 |
3 files changed, 89 insertions, 0 deletions
diff --git a/src/controller.c b/src/controller.c new file mode 100644 index 0000000..cacada3 --- /dev/null +++ b/src/controller.c @@ -0,0 +1,37 @@ +#include "controller.h" + +#include <raylib.h> + +b32 is_button_pressed(i32 btn) +{ + return IsKeyPressed(btn); +} + +b32 is_button_held(i32 btn) +{ + return IsKeyDown(btn); +} + +b32 is_button_released(i32 btn) +{ + return IsKeyReleased(btn); +} + +vec2 get_dir_input(void) +{ + fx32 cont_x = (fx32)(FIXED_POINT_ONE * GetGamepadAxisMovement(0, 0)); + fx32 cont_y = (fx32)(FIXED_POINT_ONE * GetGamepadAxisMovement(0, 1)); + + fx32 key_w = is_button_held(KEY_W) ? FIXED_POINT_ONE : 0; + fx32 key_s = is_button_held(KEY_S) ? FIXED_POINT_ONE : 0; + fx32 key_a = is_button_held(KEY_A) ? FIXED_POINT_ONE : 0; + fx32 key_d = is_button_held(KEY_D) ? FIXED_POINT_ONE : 0; + + // Either Gamepad or Keyboard control, not both! + if(key_w != 0 || key_s != 0 || key_a != 0 || key_d != 0){ + cont_x = key_d - key_a; + cont_y = key_s - key_w; + } + + return (vec2){cont_x, cont_y}; +} diff --git a/src/controller.h b/src/controller.h index 0d474a7..30c73d1 100644 --- a/src/controller.h +++ b/src/controller.h @@ -8,5 +8,7 @@ b32 is_button_released(i32 btn); vec2 get_dir_input(void); +// This portion is implemented in rumble.c! // void set_rumble(f32 duration, f32 strength); +void poll_rumble(f32 dt); void stop_rumble(void); diff --git a/src/rumble.c b/src/rumble.c new file mode 100644 index 0000000..2313d68 --- /dev/null +++ b/src/rumble.c @@ -0,0 +1,50 @@ +/* +I made this a separate source file because Raylib has some quirks +regarding Windows compilation. And I also suppose because it has +some state of its own. +*/ + +#include "controller.h" + +// Windows stuff starts // +typedef struct _XINPUT_VIBRATION { + unsigned wLeftMotorSpeed; + unsigned wRightMotorSpeed; +} XINPUT_VIBRATION; +unsigned int XInputSetState(unsigned int dwUserIndex, XINPUT_VIBRATION *pVibration); +// Windows stuff ends // + +static fx32 timer = 0; + +void set_rumble(f32 duration, f32 strength) +{ + XINPUT_VIBRATION state = {}; + state.wLeftMotorSpeed = (unsigned)(strength * 65535.0f); + state.wRightMotorSpeed = (unsigned)(strength * 65535.0f); + + if (XInputSetState(0, &state) == 0){ + timer = FIXED_POINT_ONE * duration; + } +} + +void poll_rumble(f32 dt) +{ + if(timer <= 0) + return; + + fx32 delta = FIXED_POINT_ONE * dt; + + timer -= delta; + if(timer <= 0){ + stop_rumble(); + } +} + +void stop_rumble(void) +{ + timer = 0; + + XINPUT_VIBRATION state = {}; + state.wLeftMotorSpeed = 0; + state.wRightMotorSpeed = 0; +}
\ No newline at end of file |
