This repository was archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.cpp
More file actions
102 lines (93 loc) · 2.6 KB
/
input.cpp
File metadata and controls
102 lines (93 loc) · 2.6 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
#include "input.h"
#include <SDL.h>
#include <unordered_map>
std::unordered_map<SDL_Scancode, bool> held_state;
std::unordered_map<SDL_Scancode, bool> already_pressed_state;
std::unordered_map<Uint8, bool> mouse_held_state;
std::unordered_map<Uint8, bool> mouse_already_pressed_state;
int scroll_dir = 0;
bool is_pressed(SDL_Scancode scancode) {
if (already_pressed_state[scancode] == false && held_state[scancode] == true) {
return true;
}
return false;
}
bool is_down(SDL_Scancode scancode) {
return held_state[scancode];
}
bool is_up(SDL_Scancode scancode) {
return !held_state[scancode];
}
bool is_mouse_pressed(Uint8 event) {
if (mouse_already_pressed_state[event] == false && mouse_held_state[event] == true) {
return true;
}
return false;
}
bool is_mouse_down(Uint8 event) {
return mouse_held_state[event];
}
bool is_mouse_up(Uint8 event) {
return !mouse_held_state[event];
}
int is_mouse_scrolling() {
return scroll_dir;
}
void process_raw_input(bool *quit) {
scroll_dir = 0;
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
*quit = true;
break;
case SDL_MOUSEMOTION:
break;
case SDL_MOUSEWHEEL:
if (e.wheel.y > 0) {
scroll_dir = 1;
} else if (e.wheel.y < 0) {
scroll_dir = -1;
}
break;
case SDL_WINDOWEVENT:
switch (e.window.event) {
case SDL_WINDOWEVENT_RESIZED:
puts("window resized");
break;
}
break;
default:
break;
}
}
// Preprocess the keyboard state
int num_keys;
const Uint8 *key_state = SDL_GetKeyboardState(&num_keys);
for (int i = 0; i < num_keys; i++) {
SDL_Scancode scancode = (SDL_Scancode) i;
// printf("scancode %d\n", scancode);
if (key_state[scancode]) {
// Check if we're already pressing
if (!already_pressed_state[scancode] && held_state[scancode]) {
already_pressed_state[scancode] = true;
}
held_state[scancode] = true;
} else if (!key_state[scancode]) {
held_state[scancode] = false;
already_pressed_state[scancode] = false;
}
}
// Go through all the mouse buttons that SDL supports
for (int i = 1; i <= 5; i++) {
if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(i)) {
if (!mouse_already_pressed_state[i] && mouse_held_state[i]) {
mouse_already_pressed_state[i] = true;
}
mouse_held_state[e.button.button] = true;
} else if (!(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(i))) {
mouse_held_state[i] = false;
mouse_already_pressed_state[i] = false;
}
}
}