-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
106 lines (78 loc) · 2.92 KB
/
Copy pathinput.c
File metadata and controls
106 lines (78 loc) · 2.92 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "input.h"
#include <GLFW/glfw3.h>
#include <game/interface.h>
#include <string.h>
static InputState *IS;
void input_refresh_callback(GLFWwindow *window) {}
void input_resize_callback(GLFWwindow *window, i32 width, i32 height) {}
void input_key_callback(GLFWwindow *window, i32 key, i32 scancode, i32 action, i32 mods) {
if (key > -1) {
i32 real_key = (action << 30) + (mods << 24) + key;
KeyBind bind = input_find_binding(IS, real_key);
if (bind.cmd != 0)
input_push(IS, bind.cmd);
}
}
void input_char_callback(GLFWwindow *window, u32 codepoint) { input_push_raw(IS, codepoint); }
void input_cursor_position_callback(GLFWwindow *window, f64 x, f64 y) {
IS->mouse.x = x;
IS->mouse.y = y;
}
void input_mouse_button_callback(GLFWwindow *window, i32 button, i32 action, i32 mods) {
i32 real_key = (action << 30) + (mods << 24) + (button << 21);
KeyBind bind = input_find_binding(IS, real_key);
if (bind.cmd != 0)
input_push(IS, bind.cmd);
}
i32 input_perform(void *args) {
ThreadData *const td = (ThreadData *)args;
GameState *const state = (GameState *)td->state;
mutex_t *lock = (mutex_t *)td->lock;
GLFWwindow *window = (GLFWwindow *)state->window;
IS = input_state_default();
/* Avoid the race condition between render thread and this thread. The render
* thread may also exit before ever creating a window (e.g. failed shader
* compilation), in which case we must bail instead of waiting forever. */
while (window == NULL) {
if (game_should_exit(state)) {
input_state_free(IS);
return 0;
}
window = (GLFWwindow *)state->window;
platform_sleep(1);
}
glfwSetWindowRefreshCallback(window, input_refresh_callback);
glfwSetFramebufferSizeCallback(window, input_resize_callback);
glfwSetKeyCallback(window, input_key_callback);
glfwSetCursorPosCallback(window, input_cursor_position_callback);
glfwSetMouseButtonCallback(window, input_mouse_button_callback);
game_create_bindings(state, lock, IS);
GLFWgamepadstate gs_last;
while (!game_should_exit(state)) {
#if !defined(_WIN32) && !defined(__APPLE__)
glfwPollEvents();
#endif
GLFWgamepadstate gs;
if (glfwGetGamepadState(GLFW_JOYSTICK_1, &gs)) {
IS->left_stick.x = gs.axes[0];
IS->left_stick.y = gs.axes[1];
IS->right_stick.x = gs.axes[2];
IS->right_stick.y = gs.axes[3];
IS->left_trigger = gs.axes[4];
IS->right_trigger = gs.axes[5];
for (u8 i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
IS->gamepad[i] = gs.buttons[i];
if (gs.buttons[i] != gs_last.buttons[i]) {
i32 real_key = (gs.buttons[i] << 30) + (i << 17);
KeyBind bind = input_find_binding(IS, real_key);
if (bind.cmd != 0)
input_push(IS, bind.cmd);
}
}
}
memcpy(&gs_last, &gs, sizeof(GLFWgamepadstate));
platform_sleep(2);
}
input_state_free(IS);
return 0;
}