From 6f4930e920b3f368d0fcb9acfd2824095b101681 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Tue, 25 Jul 2023 17:29:32 -0300 Subject: Controller and Rumble stuff. --- src/rumble.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/rumble.c (limited to 'src/rumble.c') 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 -- cgit v1.2.3