-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.h
More file actions
212 lines (195 loc) · 5.57 KB
/
input.h
File metadata and controls
212 lines (195 loc) · 5.57 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#pragma once
const Uint8 *state = SDL_GetKeyboardState(NULL);
SDL_GameController* gGameController;
SDL_Haptic* haptic_device;
void init_input()
{
if (SDL_NumJoysticks() < 1) {
cout << cyan << "[SDL] No controllers are plugged in." << endl;
}
else {
cout << cyan << "[SDL] There's " << SDL_NumJoysticks() << " controllers plugged in, and " << SDL_NumHaptics() << " haptics detected for every controller! Trying to open controller " << controller << " and haptic device " << haptic << "." << white << endl;
//Load joystick
gGameController = SDL_GameControllerOpen(controller);
if (gGameController == NULL) {
cout << cyan << "[SDL] Controller " << controller << " error : " << SDL_GetError() << white << endl;
}
else {
cout << cyan << "[SDL] Controller " << controller << " is plugged in." << white << endl;
}
//Load haptic
if (haptic >= 0)
{
haptic_device = SDL_HapticOpen(haptic);
if (haptic_device == NULL) {
cout << cyan << "[SDL] Haptic " << haptic << " error : " << SDL_GetError() << white << endl;
}
else {
cout << cyan << "[SDL] Haptic " << haptic << " has been connected." << white << endl;
if (SDL_HapticRumbleInit(haptic_device) != 0) {
cout << cyan << "[SDL] Haptic " << haptic << " rumble init error : " << SDL_GetError() << white << endl;
}
}
}
}
}
void vibrate_controller(double intensity, int time)
{
if (intensity > 1.0)
{
intensity = 1;
}
if (haptic_device != NULL)
{
SDL_HapticRumblePlay(haptic_device, float(intensity), time);
}
}
void check_input()
{
state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_ESCAPE] || SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER))
{
quit = true;
}
SDL_GetMouseState(&mouse_x, &mouse_y);
mouse_x -= sp_offset_x; mouse_y -= sp_offset_y;
mouse_x /= scale; mouse_y /= scale;
mouse_x = min(int(int_res_x), max(0, mouse_x));
mouse_y = min(int(int_res_y), max(0, mouse_y));
uint_fast32_t m_state = SDL_GetMouseState(NULL, NULL);
mouse_down = m_state & SDL_BUTTON(SDL_BUTTON_LEFT);
mouse_down_r = m_state & SDL_BUTTON(SDL_BUTTON_RIGHT);
if (gGameController)
{
BUTTONS_GAMEPAD[0] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_DPAD_UP) || (SDL_GameControllerGetAxis(gGameController, SDL_CONTROLLER_AXIS_LEFTY) < -24576);
BUTTONS_GAMEPAD[1] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) || (SDL_GameControllerGetAxis(gGameController, SDL_CONTROLLER_AXIS_LEFTX) > 24576);
BUTTONS_GAMEPAD[2] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_DPAD_DOWN) || (SDL_GameControllerGetAxis(gGameController, SDL_CONTROLLER_AXIS_LEFTY) > 24576);
BUTTONS_GAMEPAD[3] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_DPAD_LEFT) || (SDL_GameControllerGetAxis(gGameController, SDL_CONTROLLER_AXIS_LEFTX) < -24576);
BUTTONS_GAMEPAD[4] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_A);
BUTTONS_GAMEPAD[5] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_B);
BUTTONS_GAMEPAD[6] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_X);
BUTTONS_GAMEPAD[7] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_Y);
BUTTONS_GAMEPAD[8] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_START);
BUTTONS_GAMEPAD[9] = SDL_GameControllerGetButton(gGameController, SDL_CONTROLLER_BUTTON_BACK);
}
}
#if defined(__linux__)
uint_fast32_t next_type = 0;
auto typemap = new unordered_map<int, uint_fast32_t>();
#endif
#if defined(_WIN32)
bool KeyStates[0x100];
#endif
bool getKey(int what_want)
{
#if defined(_WIN32)
bool stat;
if (what_want == 0x08)
{
stat = GetAsyncKeyState(what_want) & 0x7FFF;
}
else
{
stat = GetKeyState(what_want) & 0x80;
}
if (!isClient && networking)
{
if (GetConsoleWindow() != GetForegroundWindow())
{
return false;
}
}
if (isClient || !networking)
{
if (!(SDL_GetWindowFlags(win) & SDL_WINDOW_INPUT_FOCUS))
{
return false;
}
}
if (KeyStates[what_want] != stat)
{
KeyStates[what_want] = stat;
if (stat)
{
return true;
}
}
return false;
#endif
#if defined(__linux__)
if (global_frame_counter > next_type &&
((what_want >= 0x30 && what_want <= 0x46) || what_want == 0xBE ||
what_want == 0x08) &&
(typemap->find(what_want) == typemap->end() ||
typemap->at(what_want) < global_frame_counter)) {
next_type = global_frame_counter + 10;
auto r = false;
switch (what_want)
{
case 0x30:
r = state[SDL_SCANCODE_0];
break;
case 0x31:
r = state[SDL_SCANCODE_1];
break;
case 0x32:
r = state[SDL_SCANCODE_2];
break;
case 0x33:
r = state[SDL_SCANCODE_3];
break;
case 0x34:
r = state[SDL_SCANCODE_4];
break;
case 0x35:
r = state[SDL_SCANCODE_5];
break;
case 0x36:
r = state[SDL_SCANCODE_6];
break;
case 0x37:
r = state[SDL_SCANCODE_7];
break;
case 0x38:
r = state[SDL_SCANCODE_8];
break;
case 0x39:
r = state[SDL_SCANCODE_9];
break;
case 0x41:
r = state[SDL_SCANCODE_A];
break;
case 0x42:
r = state[SDL_SCANCODE_B];
break;
case 0x43:
r = state[SDL_SCANCODE_C];
break;
case 0x44:
r = state[SDL_SCANCODE_D];
break;
case 0x45:
r = state[SDL_SCANCODE_E];
break;
case 0x46:
r = state[SDL_SCANCODE_F];
break;
case 0xBE:
r = state[SDL_SCANCODE_PERIOD];
break;
case 0x08:
r = state[SDL_SCANCODE_BACKSPACE];
break;
}
if (!r) {
next_type = 0;
}
else if (what_want != 0x08) {
typemap->erase(what_want);
typemap->emplace(what_want, global_frame_counter + 30);
}
return r;
}
#endif
return false;
}