From 1c0cc775732201f4c4d3ee0d6772be786b3b4aa1 Mon Sep 17 00:00:00 2001 From: Uneven Prankster Date: Sun, 15 Oct 2023 21:28:29 -0300 Subject: A lot has certainly happened! --- src/gunner/rumble.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/gunner/rumble.c (limited to 'src/gunner/rumble.c') diff --git a/src/gunner/rumble.c b/src/gunner/rumble.c new file mode 100644 index 0000000..dca4f08 --- /dev/null +++ b/src/gunner/rumble.c @@ -0,0 +1,52 @@ +/* +NOTE FOR SPELUNKERS: this requires linking to xinput1_4 AFAIK +*/ + +// 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) +{ + // If game code is surpassing this something is wrong!! + assert(strength >= 0.f && strength <= 1.f); + + XINPUT_VIBRATION state = {}; + state.wLeftMotorSpeed = (unsigned)(strength * 65535.0f); + state.wRightMotorSpeed = (unsigned)(strength * 65535.0f); + + if (XInputSetState(0, &state) == 0){ + timer = FP_TO_FIXED(duration); + } +} + +void stop_rumble(void) +{ + timer = 0; + + XINPUT_VIBRATION state = {}; + state.wLeftMotorSpeed = 0; + state.wRightMotorSpeed = 0; + XInputSetState(0, &state); +} + +void poll_rumble(f32 dt) +{ + // Early return + if(timer <= 0) + return; + + fx32 delta = FP_TO_FIXED(dt); + + timer -= delta; + if(timer <= 0){ + stop_rumble(); + } +} + -- cgit v1.2.3