-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleInput.cpp
242 lines (201 loc) · 7.69 KB
/
ModuleInput.cpp
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
#include "ModuleInput.h"
#include "Globals.h"
#include "Application.h"
#include "ModuleWindow.h"
#include "SDL.h"
ModuleInput::ModuleInput(bool startEnabled) : Module(startEnabled) {
name = "Input";
for (uint i = 0; i < MAX_KEYS; ++i)
keyboard[i] = KEY_IDLE;
memset(&pads[0], 0, sizeof(GamePad) * MAX_PADS);
}
// Destructor
ModuleInput::~ModuleInput() {}
bool ModuleInput::Init() {
LOG("Init SDL input event system");
bool ret = true;
SDL_Init(0);
if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
LOG("SDL_EVENTS could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) < 0) {
LOG("SDL_INIT_GAMECONTROLLER could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
LOG("SDL_INIT_HAPTIC could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
return ret;
}
update_status ModuleInput::PreUpdate() {
SDL_PumpEvents();
const Uint8* keys = SDL_GetKeyboardState(NULL);
for (int i = 0; i < MAX_KEYS; i++) {
if (keys[i] == 1) {
if (keyboard[i] == KEY_IDLE) {
keyboard[i] = KEY_DOWN;
}
else {
keyboard[i] = KEY_REPEAT;
}
}
else {
if (keyboard[i] == KEY_REPEAT || keyboard[i] == KEY_DOWN) {
keyboard[i] = KEY_UP;
}
else {
keyboard[i] = KEY_IDLE;
}
}
}
// CHECKS IF ESC IS CLICKED TO CLOSE
if (keyboard[SDL_SCANCODE_ESCAPE]) {
return update_status::UPDATE_STOP;
}
// CHECKS IF WINDOW X IS CLICKED TO CLOSE OR MAXIMIZE SCREEN
// Read new SDL events and Gamepad connection / removal
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return update_status::UPDATE_STOP;
}
// Maximize window (Button or F11)
else if (event.type == SDL_WINDOWEVENT_MAXIMIZED) {
SDL_MaximizeWindow(App->window->sdlWindow);
}
else if (event.type == SDL_WINDOWEVENT_MINIMIZED){
SDL_MinimizeWindow(App->window->sdlWindow);
}
else if (event.type == SDL_CONTROLLERDEVICEADDED) {
HandleDeviceConnection(event.cdevice.which);
}
else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
HandleDeviceRemoval(event.cdevice.which);
}
}
// F11 to full screen
if (keyboard[SDL_SCANCODE_F11] == KEY_DOWN) {
if (maximized) {
SDL_RestoreWindow(App->window->sdlWindow);
}
else {
SDL_MaximizeWindow(App->window->sdlWindow);
}
maximized = !maximized;
}
UpdateGamepadsInput();
return update_status:: UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleInput::CleanUp() {
LOG("Quitting SDL input event subsystem");
// Stop rumble from all gamepads and deactivate SDL functionallity
for (uint i = 0; i < MAX_PADS; ++i) {
if (pads[i].haptic != nullptr)
{
SDL_HapticStopAll(pads[i].haptic);
SDL_HapticClose(pads[i].haptic);
}
if (pads[i].controller != nullptr) SDL_GameControllerClose(pads[i].controller);
}
SDL_QuitSubSystem(SDL_INIT_HAPTIC);
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
SDL_QuitSubSystem(SDL_INIT_EVENTS);
return true;
}
void ModuleInput::HandleDeviceConnection(int index) {
if (SDL_IsGameController(index)) {
for (int i = 0; i < MAX_PADS; ++i) {
GamePad& pad = pads[i];
if (pad.enabled == false) {
if (pad.controller = SDL_GameControllerOpen(index)) {
LOG("Found a gamepad with id %i named %s", i, SDL_GameControllerName(pad.controller));
pad.enabled = true;
pad.l_dz = pad.r_dz = 0.1f;
pad.haptic = SDL_HapticOpen(index);
if (pad.haptic != nullptr)
LOG("... gamepad has force feedback capabilities");
pad.index = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(pad.controller));
}
}
}
}
}
void ModuleInput::HandleDeviceRemoval(int index) {
// If an existing gamepad has the given index, deactivate all SDL device functionallity
for (int i = 0; i < MAX_PADS; ++i) {
GamePad& pad = pads[i];
if (pad.enabled && pad.index == index)
{
SDL_HapticClose(pad.haptic);
SDL_GameControllerClose(pad.controller);
memset(&pad, 0, sizeof(GamePad));
}
}
}
void ModuleInput::UpdateGamepadsInput() {
// Iterate through all active gamepads and update all input data
for (int i = 0; i < MAX_PADS; ++i) {
GamePad& pad = pads[i];
if (pad.enabled == true) {
pad.a = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_A) == 1;
pad.b = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_B) == 1;
pad.x = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_X) == 1;
pad.y = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_Y) == 1;
pad.l1 = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) == 1;
pad.r1 = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) == 1;
pad.l3 = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_LEFTSTICK) == 1;
pad.r3 = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_RIGHTSTICK) == 1;
pad.up = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_DPAD_UP) == 1;
pad.down = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN) == 1;
pad.left = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT) == 1;
pad.right = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) == 1;
pad.start = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_START) == 1;
pad.guide = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_GUIDE) == 1;
pad.back = SDL_GameControllerGetButton(pad.controller, SDL_CONTROLLER_BUTTON_BACK) == 1;
pad.l2 = float(SDL_GameControllerGetAxis(pad.controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT)) / 32767.0f;
pad.r2 = float(SDL_GameControllerGetAxis(pad.controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) / 32767.0f;
pad.l_x = float(SDL_GameControllerGetAxis(pad.controller, SDL_CONTROLLER_AXIS_LEFTX)) / 32767.0f;
pad.l_y = float(SDL_GameControllerGetAxis(pad.controller, SDL_CONTROLLER_AXIS_LEFTY)) / 32767.0f;
pad.r_x = float(SDL_GameControllerGetAxis(pad.controller, SDL_CONTROLLER_AXIS_RIGHTX)) / 32767.0f;
pad.r_y = float(SDL_GameControllerGetAxis(pad.controller, SDL_CONTROLLER_AXIS_RIGHTY)) / 32767.0f;
// Apply deadzone. All values below the deadzone will be discarded
pad.l_x = (fabsf(pad.l_x) > pad.l_dz) ? pad.l_x : 0.0f;
pad.l_y = (fabsf(pad.l_y) > pad.l_dz) ? pad.l_y : 0.0f;
pad.r_x = (fabsf(pad.r_x) > pad.r_dz) ? pad.r_x : 0.0f;
pad.r_y = (fabsf(pad.r_y) > pad.r_dz) ? pad.r_y : 0.0f;
if (pad.rumble_countdown > 0)
pad.rumble_countdown--;
}
}
}
bool ModuleInput::ShakeController(int id, int duration, float strength) {
bool ret = false;
// Check if the given id is valid within the array bounds
if (id < 0 || id >= MAX_PADS) return ret;
// Check if the gamepad is active and allows rumble
GamePad& pad = pads[id];
if (!pad.enabled || pad.haptic == nullptr || SDL_HapticRumbleSupported(pad.haptic) != SDL_TRUE) return ret;
// If the pad is already in rumble state and the new strength is below the current value, ignore this call
if (duration < pad.rumble_countdown && strength < pad.rumble_strength)
return ret;
if (SDL_HapticRumbleInit(pad.haptic) == -1) {
LOG("Cannot init rumble for controller number %d", id);
}
else {
SDL_HapticRumbleStop(pad.haptic);
SDL_HapticRumblePlay(pad.haptic, strength, duration / 60 * 1000); // Conversion from frames to ms at 60FPS
pad.rumble_countdown = duration;
pad.rumble_strength = strength;
ret = true;
}
return ret;
}
const char* ModuleInput::GetControllerName(int id) const {
// Check if the given id has a valid controller
if (id >= 0 && id < MAX_PADS && pads[id].enabled == true && pads[id].controller != nullptr)
return SDL_GameControllerName(pads[id].controller);
return "unplugged";
}