-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontrols.cpp
More file actions
303 lines (246 loc) · 7.15 KB
/
controls.cpp
File metadata and controls
303 lines (246 loc) · 7.15 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "controls.hpp"
#include <SDL3/SDL_joystick.h>
#include <cstdio>
extern const bool* keyboardState;
// CONTROLLER VALUES
constexpr float AXIS_MAX = 32768.f;
constexpr float CLAMPVALUE = .3f;
controls::controls()
{
printf("Initing controls\n");
// Scan for initially connected gamepads
scanForGamepads();
}
controls::~controls()
{
for (auto& pad: mGamepads) {
if (pad) {
SDL_CloseGamepad(pad);
pad = nullptr;
}
}
}
void controls::scanForGamepads()
{
int count = 0;
SDL_JoystickID* joysticks = SDL_GetJoysticks(&count);
if (!joysticks) {
printf("SDL_GetJoysticks() failed: %s\n", SDL_GetError());
}
printf("Scanning for gamepads... Found %d joysticks\n", count);
// Open any available gamepads
for (int j = 0; j < count && mNumGamepads < MAX_GAMEPADS; j++) {
if (SDL_IsGamepad(j)) {
// Check if this device is already opened
bool alreadyOpen = false;
for (auto& pad: mGamepads) {
if (pad) {
SDL_Joystick* joy = SDL_GetGamepadJoystick(pad);
if (SDL_GetJoystickID(joy) == joysticks[j]) {
alreadyOpen = true;
break;
}
}
}
if (!alreadyOpen) {
// Find first free slot
for (int i = 0; i < MAX_GAMEPADS; i++) {
if (!mGamepads[i]) {
mGamepads[i] = SDL_OpenGamepad(j);
if (mGamepads[i]) {
printf("Gamepad %d: %s\n", i,
SDL_GetGamepadName(mGamepads[i]));
mNumGamepads++;
}
break;
}
}
}
}
}
SDL_free(joysticks);
printf("Total gamepads opened: %d\n", mNumGamepads);
}
void controls::handleGamepadAdded(SDL_JoystickID instanceId)
{
if (!SDL_IsGamepad(instanceId))
return;
// Find first available slot
for (int i = 0; i < MAX_GAMEPADS; i++) {
if (!mGamepads[i]) {
mGamepads[i] = SDL_OpenGamepad(instanceId);
if (mGamepads[i]) {
printf("Gamepad connected in slot %d: %s\n", i,
SDL_GetGamepadName(mGamepads[i]));
mNumGamepads++;
} else {
printf("Failed to open gamepad %d: %s\n", instanceId, SDL_GetError());
}
break;
}
}
}
void controls::handleGamepadRemoved(SDL_JoystickID instanceId)
{
// Find and remove the disconnected gamepad
for (int i = 0; i < MAX_GAMEPADS; i++) {
if (mGamepads[i]) {
SDL_Joystick* joy = SDL_GetGamepadJoystick(mGamepads[i]);
if (SDL_GetJoystickID(joy) == instanceId) {
printf("Gamepad disconnected from slot %d\n", i);
SDL_CloseGamepad(mGamepads[i]);
mGamepads[i] = nullptr;
mNumGamepads--;
break;
}
}
}
}
Point3d controls::getLeftStick(int player)
{
return readKeyboardLeftStick(player) + readGamepadLeftStick(player);
}
Point3d controls::getRightStick(int player)
{
return readKeyboardRightStick(player) + readGamepadRightStick(player);
}
bool controls::getTriggerButton(int player)
{
return readKeyboardTrigger(player) || readGamepadTrigger(player);
}
bool controls::getStartButton(int player)
{
return readKeyboardStart(player) || readGamepadStart(player);
}
bool controls::getBackButton(int player)
{
return readKeyboardBack(player) || readGamepadBack(player);
}
bool controls::getPauseButton(int player)
{
return readKeyboardPause(player) || readGamepadPause(player);
}
//
// Keyboard controller
//
Point3d controls::readKeyboardLeftStick(int /*player*/)
{
bool up = keyboardState[SDL_SCANCODE_W];
bool down = keyboardState[SDL_SCANCODE_S];
bool left = keyboardState[SDL_SCANCODE_A];
bool right = keyboardState[SDL_SCANCODE_D];
int x = 0;
int y = 0;
x += up ? 1 : 0;
x -= down ? 1 : 0;
y += left ? 1 : 0;
y -= right ? 1 : 0;
return Point3d(x, y, 0);
}
Point3d controls::readKeyboardRightStick(int /*player*/)
{
bool up = keyboardState[SDL_SCANCODE_UP];
bool down = keyboardState[SDL_SCANCODE_DOWN];
bool left = keyboardState[SDL_SCANCODE_LEFT];
bool right = keyboardState[SDL_SCANCODE_RIGHT];
int x = 0;
int y = 0;
x += up ? 1 : 0;
x -= down ? 1 : 0;
y += left ? 1 : 0;
y -= right ? 1 : 0;
return Point3d(x, y, 0);
}
bool controls::readKeyboardTrigger(int /*player*/)
{
return keyboardState[SDL_SCANCODE_SPACE];
}
bool controls::readKeyboardStart(int player)
{
switch (player) {
case 0:
return keyboardState[SDL_SCANCODE_1];
case 1:
return keyboardState[SDL_SCANCODE_2];
case 2:
return keyboardState[SDL_SCANCODE_3];
case 3:
return keyboardState[SDL_SCANCODE_4];
}
return false;
}
bool controls::readKeyboardBack(int /*player*/)
{
return keyboardState[SDL_SCANCODE_BACKSPACE];
}
bool controls::readKeyboardPause(int /*player*/)
{
return keyboardState[SDL_SCANCODE_P];
}
//
// Modern SDL GameController API
//
Point3d controls::readGamepadLeftStick(int player)
{
if (!mGamepads[player])
return Point3d(0, 0, 0);
Point3d vector;
// Note: Y axis is inverted for standard game controls
vector.x = -(SDL_GetGamepadAxis(mGamepads[player], SDL_GAMEPAD_AXIS_LEFTY)) / AXIS_MAX;
vector.y = -(SDL_GetGamepadAxis(mGamepads[player], SDL_GAMEPAD_AXIS_LEFTX)) / AXIS_MAX;
vector.z = 0;
// Apply deadzone
if (fabs(vector.x) < CLAMPVALUE) {
vector.x = 0;
}
if (fabs(vector.y) < CLAMPVALUE) {
vector.y = 0;
}
return vector;
}
Point3d controls::readGamepadRightStick(int player)
{
if (!mGamepads[player])
return Point3d(0, 0, 0);
Point3d vector;
vector.x = -(SDL_GetGamepadAxis(mGamepads[player], SDL_GAMEPAD_AXIS_RIGHTY)) / AXIS_MAX;
vector.y = -(SDL_GetGamepadAxis(mGamepads[player], SDL_GAMEPAD_AXIS_RIGHTX)) / AXIS_MAX;
vector.z = 0;
// Apply deadzone
if (fabs(vector.x) < CLAMPVALUE) {
vector.x = 0;
}
if (fabs(vector.y) < CLAMPVALUE) {
vector.y = 0;
}
return vector;
}
bool controls::readGamepadTrigger(int player)
{
if (!mGamepads[player])
return false;
// Right trigger
float triggerVal = (SDL_GetGamepadAxis(mGamepads[player], SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)) / AXIS_MAX;
if (triggerVal > CLAMPVALUE) {
return true;
}
return false;
}
bool controls::readGamepadStart(int player)
{
if (!mGamepads[player])
return false;
return SDL_GetGamepadButton(mGamepads[player], SDL_GAMEPAD_BUTTON_SOUTH);
}
bool controls::readGamepadBack(int player)
{
if (!mGamepads[player])
return false;
return SDL_GetGamepadButton(mGamepads[player], SDL_GAMEPAD_BUTTON_EAST);
}
bool controls::readGamepadPause(int player)
{
if (!mGamepads[player])
return false;
return SDL_GetGamepadButton(mGamepads[player], SDL_GAMEPAD_BUTTON_START);
}