-
Notifications
You must be signed in to change notification settings - Fork 2
/
inp.c
70 lines (54 loc) · 1.61 KB
/
inp.c
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
59
60
61
62
63
64
65
66
67
68
69
70
#ifdef INP_KEYS
#include "inp.h"
void inp_update(GLFWwindow *win)
{
for (size_t i = 0; i < inp_data.key.count; ++i) {
int handle = inp_data.key.handles[i];
char s = inp_data.key.states[handle];
s = (s << 1) & 2;
s |= glfwGetKey(win, handle) == GLFW_PRESS;
inp_data.key.states[handle] = s;
}
for (size_t i = 0; i < inp_data.btn.count; ++i) {
int handle = inp_data.btn.handles[i];
char s = inp_data.btn.states[handle];
s = (s << 1) & 2;
s |= glfwGetMouseButton(win, handle) == GLFW_PRESS;
inp_data.btn.states[handle] = s;
}
/* Mouse */
double mx, my;
glfwGetCursorPos(win, &mx, &my);
v2 pos = { mx, my * -1.f };
inp_data.mouse.pos = pos;
static v2 mouse_last;
static float dirty;
mouse_last = v2_lerp(inp_data.mouse.pos, mouse_last, dirty);
dirty = 1.f;
inp_data.mouse.delta = v2_sub(pos, mouse_last);
mouse_last = pos;
/* Joy */
if (!glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) return;
GLFWgamepadstate pad;
if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &pad)) return;
for (size_t i = 0; i < inp_data.pad.count; ++i) {
int handle = inp_data.pad.handles[i];
char s = inp_data.pad.states[handle];
s = (s << 1) & 2;
s |= pad.buttons[handle] == GLFW_PRESS;
inp_data.pad.states[handle] = s;
}
inp_data.joy.stick_l = (v2) {
pad.axes[GLFW_GAMEPAD_AXIS_LEFT_X],
-pad.axes[GLFW_GAMEPAD_AXIS_LEFT_Y],
};
inp_data.joy.stick_r = (v2) {
pad.axes[GLFW_GAMEPAD_AXIS_RIGHT_X],
-pad.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y],
};
inp_data.joy.trigg_l = .5f + .5f
* pad.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];
inp_data.joy.trigg_r = .5f + .5f
* pad.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];
}
#endif