Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-1-2-2013-Decompilation
Path: blob/main/RSDKv4/Input.cpp
817 views
1
#include "RetroEngine.hpp"
2
3
InputData keyPress = InputData();
4
InputData keyDown = InputData();
5
6
int touchDown[8];
7
int touchX[8];
8
int touchY[8];
9
int touchID[8];
10
float touchXF[8];
11
float touchYF[8];
12
int touches = 0;
13
14
int hapticEffectNum = -2;
15
16
#if !RETRO_USE_ORIGINAL_CODE
17
#include <algorithm>
18
#include <vector>
19
20
InputButton inputDevice[INPUT_BUTTONCOUNT];
21
int inputType = 0;
22
23
// mania deadzone vals lol
24
float LSTICK_DEADZONE = 0.3;
25
float RSTICK_DEADZONE = 0.3;
26
float LTRIGGER_DEADZONE = 0.3;
27
float RTRIGGER_DEADZONE = 0.3;
28
29
int mouseHideTimer = 0;
30
int lastMouseX = 0;
31
int lastMouseY = 0;
32
33
struct InputDevice {
34
#if RETRO_USING_SDL2
35
// we need the controller index reported from SDL2's controller added event
36
int index;
37
SDL_GameController *devicePtr;
38
SDL_Haptic *hapticPtr;
39
#endif
40
#if RETRO_USING_SDL1
41
SDL_Joystick *devicePtr;
42
#endif
43
int id;
44
};
45
46
std::vector<InputDevice> controllers;
47
48
#if RETRO_USING_SDL1
49
byte keyState[SDLK_LAST];
50
#endif
51
52
#define normalize(val, minVal, maxVal) ((float)(val) - (float)(minVal)) / ((float)(maxVal) - (float)(minVal))
53
54
#if RETRO_USING_SDL2
55
bool getControllerButton(byte buttonID)
56
{
57
bool pressed = false;
58
59
for (int i = 0; i < controllers.size(); ++i) {
60
SDL_GameController *controller = controllers[i].devicePtr;
61
62
if (SDL_GameControllerGetButton(controller, (SDL_GameControllerButton)buttonID)) {
63
pressed |= true;
64
continue;
65
}
66
else {
67
switch (buttonID) {
68
default: break;
69
case SDL_CONTROLLER_BUTTON_DPAD_UP: {
70
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
71
float delta = 0;
72
if (axis < 0)
73
delta = -normalize(-axis, 1, 32768);
74
else
75
delta = normalize(axis, 0, 32767);
76
pressed |= delta < -LSTICK_DEADZONE;
77
continue;
78
}
79
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: {
80
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
81
float delta = 0;
82
if (axis < 0)
83
delta = -normalize(-axis, 1, 32768);
84
else
85
delta = normalize(axis, 0, 32767);
86
pressed |= delta > LSTICK_DEADZONE;
87
continue;
88
}
89
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: {
90
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
91
float delta = 0;
92
if (axis < 0)
93
delta = -normalize(-axis, 1, 32768);
94
else
95
delta = normalize(axis, 0, 32767);
96
pressed |= delta < -LSTICK_DEADZONE;
97
continue;
98
}
99
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: {
100
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
101
float delta = 0;
102
if (axis < 0)
103
delta = -normalize(-axis, 1, 32768);
104
else
105
delta = normalize(axis, 0, 32767);
106
pressed |= delta > LSTICK_DEADZONE;
107
continue;
108
}
109
}
110
}
111
112
switch (buttonID) {
113
default: break;
114
case SDL_CONTROLLER_BUTTON_ZL: {
115
float delta = normalize(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT), 0, 32767);
116
pressed |= delta > LTRIGGER_DEADZONE;
117
continue;
118
}
119
case SDL_CONTROLLER_BUTTON_ZR: {
120
float delta = normalize(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT), 0, 32767);
121
pressed |= delta > RTRIGGER_DEADZONE;
122
continue;
123
}
124
case SDL_CONTROLLER_BUTTON_LSTICK_UP: {
125
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
126
float delta = 0;
127
if (axis < 0)
128
delta = -normalize(-axis, 1, 32768);
129
else
130
delta = normalize(axis, 0, 32767);
131
pressed |= delta < -LSTICK_DEADZONE;
132
continue;
133
}
134
case SDL_CONTROLLER_BUTTON_LSTICK_DOWN: {
135
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY);
136
float delta = 0;
137
if (axis < 0)
138
delta = -normalize(-axis, 1, 32768);
139
else
140
delta = normalize(axis, 0, 32767);
141
pressed |= delta > LSTICK_DEADZONE;
142
continue;
143
}
144
case SDL_CONTROLLER_BUTTON_LSTICK_LEFT: {
145
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
146
float delta = 0;
147
if (axis < 0)
148
delta = -normalize(-axis, 1, 32768);
149
else
150
delta = normalize(axis, 0, 32767);
151
pressed |= delta > LSTICK_DEADZONE;
152
continue;
153
}
154
case SDL_CONTROLLER_BUTTON_LSTICK_RIGHT: {
155
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX);
156
float delta = 0;
157
if (axis < 0)
158
delta = -normalize(-axis, 1, 32768);
159
else
160
delta = normalize(axis, 0, 32767);
161
pressed |= delta < -LSTICK_DEADZONE;
162
continue;
163
}
164
case SDL_CONTROLLER_BUTTON_RSTICK_UP: {
165
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY);
166
float delta = 0;
167
if (axis < 0)
168
delta = -normalize(-axis, 1, 32768);
169
else
170
delta = normalize(axis, 0, 32767);
171
pressed |= delta < -RSTICK_DEADZONE;
172
continue;
173
}
174
case SDL_CONTROLLER_BUTTON_RSTICK_DOWN: {
175
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY);
176
float delta = 0;
177
if (axis < 0)
178
delta = -normalize(-axis, 1, 32768);
179
else
180
delta = normalize(axis, 0, 32767);
181
pressed |= delta > RSTICK_DEADZONE;
182
continue;
183
}
184
case SDL_CONTROLLER_BUTTON_RSTICK_LEFT: {
185
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX);
186
float delta = 0;
187
if (axis < 0)
188
delta = -normalize(-axis, 1, 32768);
189
else
190
delta = normalize(axis, 0, 32767);
191
pressed |= delta > RSTICK_DEADZONE;
192
continue;
193
}
194
case SDL_CONTROLLER_BUTTON_RSTICK_RIGHT: {
195
int axis = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX);
196
float delta = 0;
197
if (axis < 0)
198
delta = -normalize(-axis, 1, 32768);
199
else
200
delta = normalize(axis, 0, 32767);
201
pressed |= delta < -RSTICK_DEADZONE;
202
continue;
203
}
204
}
205
}
206
207
return pressed;
208
}
209
#endif //! RETRO_USING_SDL2
210
211
void controllerInit(int controllerID)
212
{
213
for (int i = 0; i < controllers.size(); ++i) {
214
if (controllers[i].id == controllerID) {
215
return; // we already opened this one!
216
}
217
}
218
219
#if RETRO_USING_SDL2
220
SDL_GameController *controller = SDL_GameControllerOpen(controllerID);
221
if (controller) {
222
InputDevice device;
223
device.id = controllerID;
224
device.index = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
225
device.devicePtr = controller;
226
device.hapticPtr = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(controller));
227
if (device.hapticPtr == NULL) {
228
PrintLog("Could not open controller haptics...\nSDL_GetError() -> %s", SDL_GetError());
229
}
230
else {
231
if (SDL_HapticRumbleInit(device.hapticPtr) < 0) {
232
printf("Unable to initialize rumble!\nSDL_GetError() -> %s", SDL_GetError());
233
}
234
}
235
236
controllers.push_back(device);
237
inputType = 1;
238
}
239
else {
240
PrintLog("Could not open controller...\nSDL_GetError() -> %s", SDL_GetError());
241
}
242
#endif
243
}
244
245
void controllerClose(int controllerID)
246
{
247
#if RETRO_USING_SDL2
248
SDL_GameController *controller = SDL_GameControllerFromInstanceID(controllerID);
249
if (controller) {
250
SDL_GameControllerClose(controller);
251
#endif
252
if (controllers.size() == 1) {
253
controllers.clear();
254
} else {
255
for (int i = 0; i < controllers.size(); ++i) {
256
if (controllers[i].index == controllerID) {
257
controllers.erase(controllers.begin() + i);
258
#if RETRO_USING_SDL2
259
if (controllers[i].hapticPtr) {
260
SDL_HapticClose(controllers[i].hapticPtr);
261
}
262
#endif
263
break;
264
}
265
}
266
}
267
#if RETRO_USING_SDL2
268
}
269
#endif
270
271
if (controllers.empty())
272
inputType = 0;
273
}
274
275
void InitInputDevices()
276
{
277
#if RETRO_USING_SDL2
278
PrintLog("Initializing gamepads...");
279
280
// fix for issue #334 on github, not sure what's going wrong, but it seems to not be initializing the gamepad api maybe?
281
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
282
283
int joyStickCount = SDL_NumJoysticks();
284
controllers.clear();
285
int gamepadCount = 0;
286
287
// Count how many controllers there are
288
for (int i = 0; i < joyStickCount; i++)
289
if (SDL_IsGameController(i))
290
gamepadCount++;
291
292
PrintLog("Found %d gamepads!", gamepadCount);
293
for (int i = 0; i < gamepadCount; i++) {
294
SDL_GameController *gamepad = SDL_GameControllerOpen(i);
295
InputDevice device;
296
device.id = 0;
297
device.index = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamepad));
298
device.devicePtr = gamepad;
299
300
if (SDL_GameControllerGetAttached(gamepad))
301
controllers.push_back(device);
302
else
303
PrintLog("InitInputDevices() error -> %s", SDL_GetError());
304
}
305
306
if (gamepadCount > 0)
307
SDL_GameControllerEventState(SDL_ENABLE);
308
#endif
309
}
310
311
void ReleaseInputDevices()
312
{
313
for (int i = 0; i < controllers.size(); i++) {
314
#if RETRO_USING_SDL2
315
if (controllers[i].devicePtr)
316
SDL_GameControllerClose(controllers[i].devicePtr);
317
if (controllers[i].hapticPtr)
318
SDL_HapticClose(controllers[i].hapticPtr);
319
#endif
320
}
321
controllers.clear();
322
}
323
324
void ProcessInput()
325
{
326
#if RETRO_USING_SDL2
327
int length = 0;
328
const byte *keyState = SDL_GetKeyboardState(&length);
329
330
if (inputType == 0) {
331
for (int i = 0; i < INPUT_ANY; i++) {
332
if (keyState[inputDevice[i].keyMappings]) {
333
inputDevice[i].setHeld();
334
if (!inputDevice[INPUT_ANY].hold)
335
inputDevice[INPUT_ANY].setHeld();
336
}
337
else if (inputDevice[i].hold)
338
inputDevice[i].setReleased();
339
}
340
}
341
else if (inputType == 1) {
342
for (int i = 0; i < INPUT_ANY; i++) {
343
if (getControllerButton(inputDevice[i].contMappings)) {
344
inputDevice[i].setHeld();
345
if (!inputDevice[INPUT_ANY].hold)
346
inputDevice[INPUT_ANY].setHeld();
347
}
348
else if (inputDevice[i].hold)
349
inputDevice[i].setReleased();
350
}
351
}
352
353
bool isPressed = false;
354
for (int i = 0; i < INPUT_BUTTONCOUNT; i++) {
355
if (keyState[inputDevice[i].keyMappings]) {
356
isPressed = true;
357
break;
358
}
359
}
360
if (isPressed)
361
inputType = 0;
362
else if (inputType == 0)
363
inputDevice[INPUT_ANY].setReleased();
364
365
isPressed = false;
366
for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++) {
367
if (getControllerButton(i)) {
368
isPressed = true;
369
break;
370
}
371
}
372
if (isPressed)
373
inputType = 1;
374
else if (inputType == 1)
375
inputDevice[INPUT_ANY].setReleased();
376
377
if (inputDevice[INPUT_ANY].press || inputDevice[INPUT_ANY].hold || touches > 1) {
378
Engine.dimTimer = 0;
379
}
380
else if (Engine.dimTimer < Engine.dimLimit && !Engine.masterPaused) {
381
++Engine.dimTimer;
382
}
383
384
#ifdef RETRO_USING_MOUSE
385
if (touches <= 0) { // Touch always takes priority over mouse
386
int mx = 0, my = 0;
387
SDL_GetMouseState(&mx, &my);
388
389
if (mx == lastMouseX && my == lastMouseY) {
390
++mouseHideTimer;
391
if (mouseHideTimer == 120) {
392
SDL_ShowCursor(false);
393
}
394
}
395
else {
396
if (mouseHideTimer >= 120)
397
SDL_ShowCursor(true);
398
mouseHideTimer = 0;
399
Engine.dimTimer = 0;
400
}
401
402
lastMouseX = mx;
403
lastMouseY = my;
404
}
405
#endif //! RETRO_USING_MOUSE
406
407
#elif RETRO_USING_SDL1
408
if (SDL_NumJoysticks() > 0) {
409
controller = SDL_JoystickOpen(0);
410
411
// There's a problem opening the joystick
412
if (controller == NULL) {
413
// Uh oh
414
}
415
else {
416
inputType = 1;
417
}
418
}
419
else {
420
if (controller) {
421
// Close the joystick
422
SDL_JoystickClose(controller);
423
}
424
controller = nullptr;
425
inputType = 0;
426
}
427
428
if (inputType == 0) {
429
for (int i = 0; i < INPUT_BUTTONCOUNT; i++) {
430
if (keyState[inputDevice[i].keyMappings]) {
431
inputDevice[i].setHeld();
432
inputDevice[INPUT_ANY].setHeld();
433
continue;
434
}
435
else if (inputDevice[i].hold)
436
inputDevice[i].setReleased();
437
}
438
}
439
else if (inputType == 1 && controller) {
440
for (int i = 0; i < INPUT_BUTTONCOUNT; i++) {
441
if (SDL_JoystickGetButton(controller, inputDevice[i].contMappings)) {
442
inputDevice[i].setHeld();
443
inputDevice[INPUT_ANY].setHeld();
444
continue;
445
}
446
else if (inputDevice[i].hold)
447
inputDevice[i].setReleased();
448
}
449
}
450
451
bool isPressed = false;
452
for (int i = 0; i < INPUT_BUTTONCOUNT; i++) {
453
if (keyState[inputDevice[i].keyMappings]) {
454
isPressed = true;
455
break;
456
}
457
}
458
if (isPressed)
459
inputType = 0;
460
else if (inputType == 0)
461
inputDevice[INPUT_ANY].setReleased();
462
463
int buttonCnt = 0;
464
if (controller)
465
buttonCnt = SDL_JoystickNumButtons(controller);
466
bool flag = false;
467
for (int i = 0; i < buttonCnt; ++i) {
468
flag = true;
469
inputType = 1;
470
}
471
if (!flag && inputType == 1) {
472
inputDevice[INPUT_ANY].setReleased();
473
}
474
#endif //! RETRO_USING_SDL2
475
}
476
#endif //! !RETRO_USE_ORIGINAL_CODE
477
478
// Pretty much is this code in the original, just formatted differently
479
void CheckKeyPress(InputData *input)
480
{
481
#if !RETRO_USE_ORIGINAL_CODE
482
input->up = inputDevice[INPUT_UP].press;
483
input->down = inputDevice[INPUT_DOWN].press;
484
input->left = inputDevice[INPUT_LEFT].press;
485
input->right = inputDevice[INPUT_RIGHT].press;
486
input->A = inputDevice[INPUT_BUTTONA].press;
487
input->B = inputDevice[INPUT_BUTTONB].press;
488
input->C = inputDevice[INPUT_BUTTONC].press;
489
input->X = inputDevice[INPUT_BUTTONX].press;
490
input->Y = inputDevice[INPUT_BUTTONY].press;
491
input->Z = inputDevice[INPUT_BUTTONZ].press;
492
input->L = inputDevice[INPUT_BUTTONL].press;
493
input->R = inputDevice[INPUT_BUTTONR].press;
494
input->start = inputDevice[INPUT_START].press;
495
input->select = inputDevice[INPUT_SELECT].press;
496
#endif
497
498
#if RETRO_REV03
499
SetGlobalVariableByName("input.pressButton", input->A || input->B || input->C || input->X || input->Y || input->Z || input->L || input->R
500
|| input->start || input->select);
501
#endif
502
}
503
504
void CheckKeyDown(InputData *input)
505
{
506
#if !RETRO_USE_ORIGINAL_CODE
507
input->up = inputDevice[INPUT_UP].hold;
508
input->down = inputDevice[INPUT_DOWN].hold;
509
input->left = inputDevice[INPUT_LEFT].hold;
510
input->right = inputDevice[INPUT_RIGHT].hold;
511
input->A = inputDevice[INPUT_BUTTONA].hold;
512
input->B = inputDevice[INPUT_BUTTONB].hold;
513
input->C = inputDevice[INPUT_BUTTONC].hold;
514
input->X = inputDevice[INPUT_BUTTONX].hold;
515
input->Y = inputDevice[INPUT_BUTTONY].hold;
516
input->Z = inputDevice[INPUT_BUTTONZ].hold;
517
input->L = inputDevice[INPUT_BUTTONL].hold;
518
input->R = inputDevice[INPUT_BUTTONR].hold;
519
input->start = inputDevice[INPUT_START].hold;
520
input->select = inputDevice[INPUT_SELECT].hold;
521
#endif
522
}
523
524
int CheckTouchRect(float x, float y, float w, float h)
525
{
526
for (int f = 0; f < touches; ++f) {
527
if (touchDown[f] && touchXF[f] > (x - w) && touchYF[f] > (y - h) && touchXF[f] <= (x + w) && touchYF[f] <= (y + h)) {
528
return f;
529
}
530
}
531
return -1;
532
}
533
534
int CheckTouchRectMatrix(void *m, float x, float y, float w, float h)
535
{
536
MatrixF *mat = (MatrixF *)m;
537
for (int f = 0; f < touches; ++f) {
538
float tx = touchXF[f];
539
float ty = touchYF[f];
540
if (touchDown[f]) {
541
float posX = (((tx * mat->values[0][0]) + (ty * mat->values[1][0])) + (mat->values[2][0] * SCREEN_YSIZE)) + mat->values[3][0];
542
if (posX > (x - w) && posX <= (x + w)) {
543
float posY = (((tx * mat->values[0][1]) + (ty * mat->values[1][1])) + (mat->values[2][1] * SCREEN_YSIZE)) + mat->values[3][1];
544
if (posY > (y - h) && posY <= (y + h))
545
return f;
546
}
547
}
548
}
549
return -1;
550
}
551
552
#if RETRO_USE_HAPTICS
553
void HapticEffect(int *hapticID, int *a2, int *a3, int *a4)
554
{
555
if (Engine.hapticsEnabled)
556
hapticEffectNum = *hapticID;
557
}
558
#endif
559