-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_sdl.cpp
More file actions
275 lines (220 loc) · 6.59 KB
/
Copy pathinput_sdl.cpp
File metadata and controls
275 lines (220 loc) · 6.59 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
#include "input_sdl.h"
#include "input_manager.h"
#include "vdc_stub.h"
#include <SDL3/SDL.h>
static JoyState keyboardP1;
static JoyState keyboardP2;
static JoyState buttonP1;
static JoyState buttonP2;
static JoyState axisP1;
static JoyState axisP2;
static SDL_Gamepad* gamepads[2] = { nullptr, nullptr };
static SDL_JoystickID gamepadIds[2] = { 0, 0 };
static const Sint16 AXIS_DEADZONE = 12000;
static bool controllerPortsSwapped = false;
static JoyState CombineState(const JoyState& keyboard, const JoyState& buttons, const JoyState& axes)
{
JoyState result{};
result.up = keyboard.up || buttons.up || axes.up;
result.down = keyboard.down || buttons.down || axes.down;
result.left = keyboard.left || buttons.left || axes.left;
result.right = keyboard.right || buttons.right || axes.right;
result.fire = keyboard.fire || buttons.fire || axes.fire;
return result;
}
static void PushStates()
{
JoyState gamepadP1 = CombineState(JoyState{}, buttonP1, axisP1);
JoyState gamepadP2 = CombineState(JoyState{}, buttonP2, axisP2);
if (controllerPortsSwapped)
{
InputManager_SetPlayer1State(CombineState(keyboardP1, gamepadP2, JoyState{}));
InputManager_SetPlayer2State(CombineState(keyboardP2, gamepadP1, JoyState{}));
}
else
{
InputManager_SetPlayer1State(CombineState(keyboardP1, gamepadP1, JoyState{}));
InputManager_SetPlayer2State(CombineState(keyboardP2, gamepadP2, JoyState{}));
}
}
static void OpenGamepad(SDL_JoystickID id)
{
for (int i = 0; i < 2; i++)
{
if (gamepadIds[i] == id)
return;
}
for (int i = 0; i < 2; i++)
{
if (!gamepads[i])
{
gamepads[i] = SDL_OpenGamepad(id);
if (gamepads[i])
{
gamepadIds[i] = id;
SDL_Log("Gamepad %d connected: %s", i + 1, SDL_GetGamepadName(gamepads[i]));
}
else
{
SDL_Log("Failed to open gamepad: %s", SDL_GetError());
}
return;
}
}
}
static void CloseGamepad(SDL_JoystickID id)
{
for (int i = 0; i < 2; i++)
{
if (gamepadIds[i] == id)
{
SDL_Log("Gamepad %d disconnected", i + 1);
SDL_CloseGamepad(gamepads[i]);
gamepads[i] = nullptr;
gamepadIds[i] = 0;
if (i == 0)
{
buttonP1 = JoyState{};
axisP1 = JoyState{};
}
else
{
buttonP2 = JoyState{};
axisP2 = JoyState{};
}
PushStates();
return;
}
}
}
static int GetSlotFromGamepad(SDL_JoystickID id)
{
if (gamepadIds[0] == id)
return 0;
if (gamepadIds[1] == id)
return 1;
return -1;
}
static JoyState* GetButtonStateFromSlot(int slot)
{
if (slot == 0)
return &buttonP1;
if (slot == 1)
return &buttonP2;
return nullptr;
}
static JoyState* GetAxisStateFromSlot(int slot)
{
if (slot == 0)
return &axisP1;
if (slot == 1)
return &axisP2;
return nullptr;
}
static void HandleGamepadButton(const SDL_Event& event, bool pressed)
{
int slot = GetSlotFromGamepad(event.gbutton.which);
JoyState* joy = GetButtonStateFromSlot(slot);
if (!joy)
return;
switch (event.gbutton.button)
{
case SDL_GAMEPAD_BUTTON_DPAD_UP:
joy->up = pressed;
break;
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
joy->down = pressed;
break;
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
joy->left = pressed;
break;
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
joy->right = pressed;
break;
case SDL_GAMEPAD_BUTTON_SOUTH:
joy->fire = pressed;
break;
case SDL_GAMEPAD_BUTTON_NORTH:
if (pressed)
{
controllerPortsSwapped = !controllerPortsSwapped;
const char* message = controllerPortsSwapped
? "CONTROLLER PORTS SWAPPED"
: "CONTROLLER PORTS NORMAL";
SDL_Log("%s", message);
VDCStub_ShowMessage(message);
}
break;
default:
break;
}
}
static void HandleGamepadAxis(const SDL_Event& event)
{
int slot = GetSlotFromGamepad(event.gaxis.which);
JoyState* joy = GetAxisStateFromSlot(slot);
if (!joy)
return;
if (event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFTX)
{
joy->left = (event.gaxis.value < -AXIS_DEADZONE);
joy->right = (event.gaxis.value > AXIS_DEADZONE);
}
if (event.gaxis.axis == SDL_GAMEPAD_AXIS_LEFTY)
{
joy->up = (event.gaxis.value < -AXIS_DEADZONE);
joy->down = (event.gaxis.value > AXIS_DEADZONE);
}
}
static void HandleKeyboard(const SDL_Event& event, bool pressed)
{
SDL_Keycode key = event.key.key;
// Player 1 keyboard fallback: Arrow keys + Right Shift
if (key == SDLK_UP) keyboardP1.up = pressed;
if (key == SDLK_DOWN) keyboardP1.down = pressed;
if (key == SDLK_LEFT) keyboardP1.left = pressed;
if (key == SDLK_RIGHT) keyboardP1.right = pressed;
if (key == SDLK_RSHIFT) keyboardP1.fire = pressed;
// Player 1 alternative: Numpad 8/5/4/6 + Right Ctrl
if (key == SDLK_KP_8) keyboardP1.up = pressed;
if (key == SDLK_KP_5) keyboardP1.down = pressed;
if (key == SDLK_KP_4) keyboardP1.left = pressed;
if (key == SDLK_KP_6) keyboardP1.right = pressed;
if (key == SDLK_RCTRL) keyboardP1.fire = pressed;
// Player 2 keyboard fallback: WASD + Space
if (key == SDLK_W) keyboardP2.up = pressed;
if (key == SDLK_S) keyboardP2.down = pressed;
if (key == SDLK_A) keyboardP2.left = pressed;
if (key == SDLK_D) keyboardP2.right = pressed;
if (key == SDLK_SPACE) keyboardP2.fire = pressed;
}
void InputSDL_HandleEvent(const SDL_Event& event)
{
switch (event.type)
{
case SDL_EVENT_GAMEPAD_ADDED:
OpenGamepad(event.gdevice.which);
break;
case SDL_EVENT_GAMEPAD_REMOVED:
CloseGamepad(event.gdevice.which);
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
HandleGamepadButton(event, true);
break;
case SDL_EVENT_GAMEPAD_BUTTON_UP:
HandleGamepadButton(event, false);
break;
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
HandleGamepadAxis(event);
break;
case SDL_EVENT_KEY_DOWN:
HandleKeyboard(event, true);
break;
case SDL_EVENT_KEY_UP:
HandleKeyboard(event, false);
break;
default:
return;
}
PushStates();
}