blob: b2da19179d3dc6644a3277cc39c1aa3805463e0c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/*
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.
NOTE FOR SPELUNKERS: this requires linking to xinput1_4 AFAIK
*/
#include "controller.h"
#include <stdio.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)
{
// 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 poll_rumble(f32 dt)
{
// Early return
if(timer <= 0)
return;
fx32 delta = FP_TO_FIXED(dt);
timer -= delta;
if(timer <= 0){
stop_rumble();
}
}
void stop_rumble(void)
{
timer = 0;
XINPUT_VIBRATION state = {};
state.wLeftMotorSpeed = 0;
state.wRightMotorSpeed = 0;
XInputSetState(0, &state);
}
|