-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInput.cpp
More file actions
225 lines (192 loc) · 8.81 KB
/
ModuleInput.cpp
File metadata and controls
225 lines (192 loc) · 8.81 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
#include "Globals.h"
#include "Game.h"
#include "ModuleInput.h"
#include "SDL/include/SDL.h"
ModuleInput::ModuleInput (bool startEnabled) : Module(startEnabled) {
for (uint i = 0; i < MAX_KEYS; ++i) { keys[i] = KEY_IDLE; }
memset(&pads[0], 0, sizeof(GamePad) * MAX_PADS);
}
ModuleInput::~ModuleInput() {}
bool ModuleInput::Init() {
SDL_Init(0);
if (SDL_InitSubSystem(SDL_INIT_EVENTS) != 0) {
LOG("SDL_Events could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0) {
LOG("SDL_INIT_GAMECONTROLLER could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
if (SDL_InitSubSystem(SDL_INIT_HAPTIC) != 0) {
LOG("SDL_INIT_HAPTIC could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
if (cursor == NULL) {
LOG("Cursor could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else {
SDL_SetCursor(cursor);
SDL_ShowCursor(0);
}
return true;
}
UPDATE_STATUS ModuleInput::PreUpdate() {
const Uint8* keyboard = SDL_GetKeyboardState(NULL);
for (int i = 0; i < MAX_KEYS; ++i) {
if (keyboard[i]) {
if (keys[i] == KEY_IDLE) { keys[i] = KEY_DOWN; }
else { keys[i] = KEY_REPEAT; }
}
else {
if (keys[i] == KEY_REPEAT || keys[i] == KEY_DOWN) { keys[i] = KEY_UP; }
else { keys[i] = KEY_IDLE; }
}
}
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
switch (event.type) {
case(SDL_CONTROLLERDEVICEADDED): {
HandleDeviceConnection(event.cdevice.which);
break;
}
case(SDL_CONTROLLERDEVICEREMOVED): {
HandleDeviceRemoval(event.cdevice.which);
break;
}
case(SDL_MOUSEBUTTONDOWN): {
mouseX = event.motion.x;
mouseY = event.motion.y;
hasClicked = true;
break;
}
case(SDL_QUIT): {
return UPDATE_STATUS::UPDATE_STOP;
break;
}
default: {
hasClicked = false;
break;
}
}
}
UpdateGamepadsInput();
if (keys[SDL_SCANCODE_F3] == KEY_DOWN) {
if (SDL_ShowCursor(-1)) {
SDL_ShowCursor(0);
}
else {
SDL_ShowCursor(1);
}
}
return UPDATE_STATUS::UPDATE_CONTINUE;
}
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_FreeCursor(cursor);
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) {
// Check if the given id is valid within the array bounds
if (id < 0 || id >= MAX_PADS) { return false; }
// 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 false; }
// 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 false; }
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;
return true;
}
}
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";
}
void ModuleInput::SetKey(const KEY_STATE _key, int i) { keys[i] = _key; }
KEY_STATE ModuleInput::GetKey(int i) const { return keys[i]; }
GamePad ModuleInput::GetGamePad() { return *pads; }
GamePad ModuleInput::GetGamePad(int i) const { return pads[i]; }
int ModuleInput::GetCursorState() const { return SDL_ShowCursor(-1); }
int ModuleInput::GetMouseX() const { return mouseX; };
int ModuleInput::GetMouseY() const { return mouseY; };
bool ModuleInput::CheckIfClicked() const { return hasClicked; };