-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
155 lines (149 loc) · 4.61 KB
/
input.c
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
#include "input.h"
#include "game.h"
#include "render.h"
float mouse_x = 0.0f;
float mouse_y = 0.0f;
float last_wheel_event = 0.0f;
int mouse_speed = 10; // 0 - 50
bool mouse_invert = false;
bool keys[_KEY_END] = {0};
bool input_quit = false;
void input_consume(SDL_Window * window) {
static uint8_t fs = 0;
const uint8_t * currentstates = SDL_GetKeyboardState(NULL);
SDL_Event e;
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT)
input_quit = true;
if(e.type == SDL_KEYDOWN && e.key.repeat == 0) {
switch (e.key.keysym.sym) {
case SDLK_UP:
case SDLK_w:
keys[KEY_UP] = 1;
break;
case SDLK_LEFT:
case SDLK_a:
keys[KEY_LEFT] = 1;
break;
case SDLK_DOWN:
case SDLK_s:
keys[KEY_DOWN] = 1;
break;
case SDLK_RIGHT:
case SDLK_d:
keys[KEY_RIGHT] = 1;
break;
case SDLK_q:
keys[KEY_PREV] = 1;
break;
case SDLK_e:
keys[KEY_NEXT] = 1;
break;
case SDLK_SPACE:
keys[KEY_JUMP] = 1;
break;
}
}
if(e.type == SDL_KEYUP) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
SDL_SetRelativeMouseMode(false);
break;
case SDLK_UP:
case SDLK_w:
keys[KEY_UP] = 0;
break;
case SDLK_LEFT:
case SDLK_a:
keys[KEY_LEFT] = 0;
break;
case SDLK_DOWN:
case SDLK_s:
keys[KEY_DOWN] = 0;
break;
case SDLK_RIGHT:
case SDLK_d:
keys[KEY_RIGHT] = 0;
break;
case SDLK_q:
keys[KEY_PREV] = 0;
break;
case SDLK_e:
keys[KEY_NEXT] = 0;
break;
case SDLK_SPACE:
keys[KEY_JUMP] = 0;
break;
case SDLK_RETURN:
if (currentstates[SDL_SCANCODE_RALT] || currentstates[SDL_SCANCODE_LALT]) {
uint32_t flag = SDL_WINDOW_FULLSCREEN_DESKTOP;
if (fs)
flag = 0;
fs = (fs + 1) % 2;
SDL_SetWindowFullscreen(window, flag);
}
break;
}
}
if(e.type == SDL_MOUSEBUTTONDOWN) {
switch (e.button.button) {
case SDL_BUTTON_LEFT:
if (SDL_GetRelativeMouseMode() == false) {
SDL_SetRelativeMouseMode(true);
break;
}
keys[KEY_ACTION] = 1;
break;
case SDL_BUTTON_RIGHT:
keys[KEY_ACTION + 1] = 1;
break;
}
}
if(e.type == SDL_MOUSEBUTTONUP) {
switch (e.button.button) {
case SDL_BUTTON_LEFT:
keys[KEY_ACTION] = 0;
break;
case SDL_BUTTON_RIGHT:
keys[KEY_ACTION + 1] = 0;
break;
}
}
if(e.type == SDL_MOUSEWHEEL) {
if (e.wheel.y > 0) { // up
keys[KEY_PREV] = 1;
}
if (e.wheel.y < 0) { // down
keys[KEY_NEXT] = 1;
}
}
if(e.type == SDL_MOUSEMOTION && SDL_GetRelativeMouseMode() == true) {
mouse_x += e.motion.xrel;
mouse_y += e.motion.yrel;
}
if(e.type == SDL_WINDOWEVENT) {
if(e.window.event == SDL_WINDOWEVENT_FOCUS_LOST && SDL_GetRelativeMouseMode() == true) {
SDL_SetRelativeMouseMode(false);
}
if(e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
r_current_window_width = e.window.data1;
r_current_window_height = e.window.data2;
float fw = r_current_window_width;
float fh = r_current_window_height;
float dw = INTERNAL_W;
float dh = INTERNAL_H;
float ratio;
r_pady = 0;
r_padx = 0;
if (fw / dw >= fh / dh) {
ratio = fh / dh;
r_padx = (fw - (dw * ratio)) / 2.0f;
} else {
ratio = fw / dw;
r_pady = (fh - (dh * ratio)) / 2.0f;
}
}
}
}
}