-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinpututils.c
More file actions
126 lines (104 loc) · 2.8 KB
/
inpututils.c
File metadata and controls
126 lines (104 loc) · 2.8 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
#include "inpututils.h"
#include "mathutils.h"
#include "graphicsutils.h"
#include <string.h>
void inputInit(input* inp){
memset(inp->heldKeys, 0, 256);
memset(inp->pressedKeys, 0, 256);
memset(inp->releasedKeys, 0, 256);
memset(inp->heldMouseButtons, 0, 5);
memset(inp->pressedMouseButtons, 0, 5);
memset(inp->releasedMouseButtons, 0, 5);
inp->scrollDir = 0;
}
void newInputFrame(input* inp){
memset(inp->pressedKeys, 0, 256);
memset(inp->releasedKeys, 0, 256);
memset(inp->pressedMouseButtons, 0, 5);
memset(inp->releasedMouseButtons, 0, 5);
inp->scrollDir = 0;
}
void mouseScrollEvent(input* inp, int8_t dir){
inp->scrollDir = dir;
}
void mouseMoveEvent(input* inp, int32_t x, int32_t y){
inp->mouseX = x;
inp->mouseY = y;
}
int32_t mouseX(input* inp){
return inp->mouseX;
}
int32_t mouseY(input* inp){
return inp->mouseY;
}
v2 mousePos(input* inp){
v2 a = {mouseX(inp), mouseY(inp)};
return a;
}
void mouseUpEvent(input* inp, const SDL_Event event){
inp->releasedMouseButtons[event.button.button-1] = 1;
inp->heldMouseButtons[event.button.button-1] = 0;
}
void mouseDownEvent(input* inp, const SDL_Event event){
inp->pressedMouseButtons[event.button.button-1] = 1;
inp->heldMouseButtons[event.button.button-1] = 1;
}
void keyUpEvent(input* inp, const SDL_Event event){
inp->releasedKeys[event.key.keysym.scancode] = 1;
inp->heldKeys[event.key.keysym.scancode] = 0;
}
void keyDownEvent(input* inp, const SDL_Event event){
inp->pressedKeys[event.key.keysym.scancode] = 1;
inp->heldKeys[event.key.keysym.scancode] = 1;
}
uint8_t mouseScrolled(input* inp, int8_t dir){
return sign(dir)==sign(inp->scrollDir);
}
uint8_t mouseHeld(input* inp, uint8_t button){
return inp->heldMouseButtons[button-1];
}
uint8_t mousePressed(input* inp, uint8_t button){
return inp->pressedMouseButtons[button-1];
}
uint8_t mouseReleased(input* inp, uint8_t button){
return inp->releasedMouseButtons[button-1];
}
uint8_t keyHeld(input* inp, const char* key){
SDL_Scancode k = SDL_GetScancodeFromName(key);
return inp->heldKeys[k];
}
uint8_t keyPressed(input* inp, const char* key){
SDL_Scancode k = SDL_GetScancodeFromName(key);
return inp->pressedKeys[k];
}
uint8_t keyReleased(input* inp, const char* key){
SDL_Scancode k = SDL_GetScancodeFromName(key);
return inp->releasedKeys[k];
}
void mouseInterrupt(input* inp, uint8_t b){
inp->pressedMouseButtons[b-1] = 0;
inp->heldMouseButtons[b-1] = 0;
}
void keyInterrupt(input* inp, const char* key){
SDL_Scancode k = SDL_GetScancodeFromName(key);
inp->pressedKeys[k] = 0;
inp->heldKeys[k] = 0;
}
void keystream(input* inp, char* r, char* s){
uint32_t index = 0;
strcpy(r, "");
uint8_t n = strlen(s);
uint8_t i = 0;
char p;
while (i<n){
p = s[i];
char c[2];
c[0] = p;
c[1] = '\0';
if (keyPressed(inp, c)){
r[index++] = p;
}
i++;
}
r[index] = '\0';
}