-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
170 lines (143 loc) · 3.94 KB
/
input.c
File metadata and controls
170 lines (143 loc) · 3.94 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "input.h"
#include "event.h"
#include <stdint.h>
#define BIT(x_) (1llu << (x_))
#define KEYS_COUNT ((KEY_CODE_COUNT + 63) >> 6)
static struct {
uint64_t keys_down[KEYS_COUNT];
uint64_t keys_up[KEYS_COUNT];
uint64_t keys_hold[KEYS_COUNT];
uint64_t keys_prev_hold[KEYS_COUNT];
uint64_t buttons_down[KEYS_COUNT];
uint64_t buttons_up[KEYS_COUNT];
uint64_t buttons_hold[KEYS_COUNT];
uint64_t buttons_prev_hold[KEYS_COUNT];
uint8_t char_trigger;
int32_t mouse_x;
int32_t mouse_y;
} ctx = {0};
void input_register_char(event_t* e);
void input_register_mouse_pos(event_t* e);
void input_register_key_down(event_t* e);
void input_register_key_up(event_t* e);
void input_register_button_down(event_t* e);
void input_register_button_up(event_t* e);
void
input_init()
{
b_event_register(INPUT_CHAR, input_register_char);
b_event_register(INPUT_MOUSE_MOVE, input_register_mouse_pos);
b_event_register(INPUT_KEY_DOWN, input_register_key_down);
b_event_register(INPUT_KEY_UP, input_register_key_up);
b_event_register(INPUT_BUTTON_DOWN, input_register_button_down);
b_event_register(INPUT_BUTTON_UP, input_register_button_up);
}
void
input_pre_update()
{
ctx.char_trigger = 0;
}
void
input_update()
{
int count = KEYS_COUNT;
int k;
for (k = 0; k < count; k += 1)
{
ctx.keys_down[k] = (ctx.keys_hold[k] ^ ctx.keys_prev_hold[k]) & ctx.keys_hold[k];
ctx.keys_up[k] = (ctx.keys_hold[k] ^ ctx.keys_prev_hold[k]) & ctx.keys_prev_hold[k];
ctx.keys_prev_hold[k] = ctx.keys_hold[k];
ctx.buttons_down[k] = (ctx.buttons_hold[k] ^ ctx.buttons_prev_hold[k]) & ctx.buttons_hold[k];
ctx.buttons_up[k] = (ctx.buttons_hold[k] ^ ctx.buttons_prev_hold[k]) & ctx.buttons_prev_hold[k];
ctx.buttons_prev_hold[k] = ctx.buttons_hold[k];
}
x_mouse_position_get(&ctx.mouse_x, &ctx.mouse_y);
}
uint64_t
input_keys_get(int idx) {
return ctx.keys_hold[idx];
}
void
input_register_char(event_t* e)
{
ctx.char_trigger = e->ctx.u8[0];
}
void
input_register_mouse_pos(event_t* e)
{
ctx.mouse_x = e->ctx.i32[0];
ctx.mouse_y = e->ctx.i32[1];
}
void
input_register_key_down(event_t* e)
{
uint32_t key = e->ctx.u32[0];
ctx.keys_hold[key >> 6] |= BIT(key & 63);
}
void
input_register_key_up(event_t* e)
{
uint32_t key = e->ctx.u32[0];
ctx.keys_hold[key >> 6] &= ~BIT(key & 63);
}
void
input_register_button_down(event_t* e)
{
uint32_t button = e->ctx.u32[0];
ctx.buttons_hold[button >> 6] |= BIT(button & 63);
}
void
input_register_button_up(event_t* e)
{
uint32_t button = e->ctx.u32[0];
ctx.buttons_hold[button >> 6] &= ~BIT(button & 63);
}
int
input_get_key_down(key_code_t key)
{
uint64_t key_flag = BIT(key & 63);
return (ctx.keys_down[key >> 6] & key_flag) == key_flag;
}
int
input_get_key_up(key_code_t key)
{
uint64_t key_flag = BIT(key & 63);
return (ctx.keys_up[key >> 6] & key_flag) == key_flag;
}
int
input_get_key(key_code_t key)
{
uint64_t key_flag = BIT(key & 63);
return (ctx.keys_hold[key >> 6] & key_flag) == key_flag;
}
int
input_get_button_down(button_code_t button)
{
uint64_t button_flag = BIT(button & 63);
return (ctx.buttons_down[button >> 6] & button_flag) == button_flag;
}
int
input_get_button_up(button_code_t button)
{
uint64_t button_flag = BIT(button & 63);
return (ctx.buttons_up[button >> 6] & button_flag) == button_flag;
}
int
input_get_button(button_code_t button)
{
uint64_t button_flag = BIT(button & 63);
return (ctx.buttons_hold[button >> 6] &button_flag) == button_flag;
}
void
input_get_mouse_pos(int32_t* out_x,
int32_t* out_y)
{
*out_x = ctx.mouse_x;
*out_y = ctx.mouse_y;
}
int
input_get_char(char* ch)
{
*ch = ctx.char_trigger;
return ctx.char_trigger != 0;
}