-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.c
More file actions
121 lines (94 loc) · 2 KB
/
event.c
File metadata and controls
121 lines (94 loc) · 2 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
#include "event.h"
#include <SDL.h>
#include <stdlib.h>
#include <string.h>
// Unwrap
#undef event_key_down
#undef event_key_pressed
static int frame;
static struct {
bool down;
int last_pressed;
} keys[SDL_NUM_SCANCODES];
static const char *error = "";
//
// Utility
//
// Get the last error
const char *event_get_error(void) {
return error;
}
//
// Start and stop
//
// Start the event module
bool event_start(void) {
if(SDL_InitSubSystem(SDL_INIT_EVENTS) == -1) {
error = SDL_GetError();
return false;
}
memset(keys, 0, sizeof(keys));
return true;
}
// Stop the event module
void event_stop(void) {
SDL_QuitSubSystem(SDL_INIT_EVENTS);
}
//
// Keyboard
//
// Return true if the key is currently being pressed
bool event_key_down(int k) {
return k >= 0 && k < SDL_NUM_SCANCODES && keys[k].down;
}
// Return true if the key was pressed this frame
bool event_key_pressed(int k) {
return k >= 0 && k < SDL_NUM_SCANCODES && keys[k].last_pressed == frame;
}
//
// Event processing
//
// Handle keyboard event
static void keyboard(SDL_KeyboardEvent key) {
if(key.repeat)
return;
keys[key.keysym.scancode].down = key.type == SDL_KEYDOWN;
if(key.type == SDL_KEYDOWN)
keys[key.keysym.scancode].last_pressed = frame;
}
// Handle mouse event
static void mouse_motion(SDL_MouseMotionEvent mm) {
}
static void mouse_button(SDL_MouseButtonEvent mb) {
}
static void mouse_wheel(SDL_MouseWheelEvent mw) {
}
// Process waiting events
bool event_process(void) {
frame++;
bool wants_quit = false;
for(SDL_Event e; SDL_PollEvent(&e);) {
switch(e.type) {
case SDL_QUIT:
wants_quit = true;
break;
// Keyboard
case SDL_KEYDOWN:
case SDL_KEYUP:
keyboard(e.key);
break;
// Mouse
case SDL_MOUSEMOTION:
mouse_motion(e.motion);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
mouse_button(e.button);
break;
case SDL_MOUSEWHEEL:
mouse_wheel(e.wheel);
break;
}
}
return wants_quit;
}