forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.cpp
More file actions
457 lines (407 loc) · 10.3 KB
/
Copy pathInputSystem.cpp
File metadata and controls
457 lines (407 loc) · 10.3 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "InputSystem.h"
#include <SDL/SDL.h>
#include <sstream>
#include <cstring>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "InputDetector.h"
bool KeyboardState::GetKeyValue(SDL_Scancode keyCode) const
{
return mCurrState[keyCode] == 1;
}
ButtonState KeyboardState::GetKeyState(SDL_Scancode keyCode) const
{
if (mPrevState[keyCode] == 0)
{
if (mCurrState[keyCode] == 0)
{
return ENone;
}
else
{
return EPressed;
}
}
else // Prev state must be 1
{
if (mCurrState[keyCode] == 0)
{
return EReleased;
}
else
{
return EHeld;
}
}
}
bool MouseState::GetButtonValue(int button) const
{
return (SDL_BUTTON(button) & mCurrButtons) == 1;
}
ButtonState MouseState::GetButtonState(int button) const
{
int mask = SDL_BUTTON(button);
if ((mask & mPrevButtons) == 0)
{
if ((mask & mCurrButtons) == 0)
{
return ENone;
}
else
{
return EPressed;
}
}
else
{
if ((mask & mCurrButtons) == 0)
{
return EReleased;
}
else
{
return EHeld;
}
}
}
bool ControllerState::GetButtonValue(SDL_GameControllerButton button) const
{
return mCurrButtons[button] == 1;
}
ButtonState ControllerState::GetButtonState(SDL_GameControllerButton button) const
{
if (mPrevButtons[button] == 0)
{
if (mCurrButtons[button] == 0)
{
return ENone;
}
else
{
return EPressed;
}
}
else // Prev state must be 1
{
if (mCurrButtons[button] == 0)
{
return EReleased;
}
else
{
return EHeld;
}
}
}
InputState::InputState()
{
cButtons[""] = SDL_CONTROLLER_BUTTON_INVALID;
cButtons["A"] = SDL_CONTROLLER_BUTTON_A;
cButtons["B"] = SDL_CONTROLLER_BUTTON_B;
cButtons["X"] = SDL_CONTROLLER_BUTTON_X;
cButtons["Y"] = SDL_CONTROLLER_BUTTON_Y;
cButtons["Back"] = SDL_CONTROLLER_BUTTON_BACK;
cButtons["Guide"] = SDL_CONTROLLER_BUTTON_GUIDE;
cButtons["Start"] = SDL_CONTROLLER_BUTTON_START;
cButtons["LeftStick"] = SDL_CONTROLLER_BUTTON_LEFTSTICK;
cButtons["RightStick"] = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
cButtons["LeftShoulder"] = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
cButtons["RightShoulder"] = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
cButtons["DPadUp"] = SDL_CONTROLLER_BUTTON_DPAD_UP;
cButtons["DPadDown"] = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
cButtons["DPadLeft"] = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
cButtons["DPadRight"] = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
cButtons[""] = SDL_CONTROLLER_BUTTON_MAX;
}
InputState::~InputState()
{
for (auto kv : bindings) {
delete kv.second;
}
ClearBinding();
}
// InputState
bool InputState::GetBoolValue(const std::string & name) const
{
return bindings.at(name)->GetBoolValue(Self());
}
ButtonState InputState::GetButtonValue(const std::string & name) const
{
return bindings.at(name)->GetButtonValue(Self());
}
float InputState::GetFloatValue(const std::string & name) const
{
return bindings.at(name)->GetFloatValue(Self());
}
Vector2 InputState::GetAxisValue(const std::string & name) const
{
return bindings.at(name)->GetAxisValue(Self());
}
void InputState::ParseBindingFromFile(const std::string & path)
{
std::ifstream ifs(path);
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ParseBindingFromString(str);
}
void InputState::ParseBindingFromString(const std::string & source)
{
ClearBinding();
const char* sentence = source.c_str();
std::stringstream ss(sentence);
std::string to;
if (sentence != NULL)
{
while (std::getline(ss, to, '\n')) {
if (to[0] == '#') {
continue;
}
AddBinding(to);
}
}
}
void InputState::AddBinding(const std::string & oneline)
{
std::vector<std::string> words;
split(oneline, words, ' ');
std::string name = words[0];
std::string type = words[1];
if (type == "Key") {
PutBinding(name, new KeyDetector((SDL_Scancode)std::stoi(words[2])));
}
else if (type == "Mouse") {
if (words[2] == "Left") {
PutBinding(name, new MouseDetector(SDL_BUTTON_LEFT));
}
else if (words[2] == "Right") {
PutBinding(name, new MouseDetector(SDL_BUTTON_RIGHT));
}
else {
throw std::logic_error("unknown mouse type: " + words[2]);
}
}
else if (type == "ControllerButton") {
PutBinding(name, new ControllerButtonDetector(cButtons[words[2]]));
}
else if (type == "ControllerStick") {
if (words[2] == "Left") {
PutBinding(name, new ControllerStickDetector(ControllerDirection::Left));
}
else if (words[2] == "Right") {
PutBinding(name, new ControllerStickDetector(ControllerDirection::Right));
}
else {
throw std::logic_error("unknown stick type: " + words[2]);
}
}
else if (type == "ControllerTrigger") {
if (words[2] == "Left") {
PutBinding(name, new ControllerTriggerDetector(ControllerDirection::Left));
}
else if (words[2] == "Right") {
PutBinding(name, new ControllerTriggerDetector(ControllerDirection::Right));
}
else {
throw std::logic_error("unknown stick type: " + words[2]);
}
}
else {
throw std::logic_error("unsupported input type: " + type);
}
}
void InputState::PutBinding(const std::string & key, InputDetector * detector)
{
bindings[key] = detector;
}
void InputState::RemoveBinding(const std::string & key)
{
bindings.erase(key);
}
bool InputState::HasBinding(const std::string & key) const
{
return bindings.count(key) > 0;
}
void InputState::ClearBinding()
{
bindings.clear();
}
const InputState & InputState::Self() const
{
return *this;
}
size_t InputState::split(const std::string & txt, std::vector<std::string>& strs, char ch)
{
//https://stackoverflow.com/questions/5888022/split-string-by-single-spaces
size_t pos = txt.find(ch);
size_t initialPos = 0;
strs.clear();
// Decompose statement
while (pos != std::string::npos) {
strs.push_back(txt.substr(initialPos, pos - initialPos));
initialPos = pos + 1;
pos = txt.find(ch, initialPos);
}
// Add the last one
strs.push_back(txt.substr(initialPos, std::min(pos, txt.size()) - initialPos + 1));
return strs.size();
}
// InputSystem
bool InputSystem::Initialize(const std::string& bindingFile)
{
// Keyboard
// Assign current state pointer
mState.Keyboard.mCurrState = SDL_GetKeyboardState(NULL);
// Clear previous state memory
memset(mState.Keyboard.mPrevState, 0,
SDL_NUM_SCANCODES);
// Mouse (just set everything to 0)
mState.Mouse.mCurrButtons = 0;
mState.Mouse.mPrevButtons = 0;
// Get the connected controller, if it exists
mController = SDL_GameControllerOpen(0);
// Initialize controller state
mState.Controller.mIsConnected = (mController != nullptr);
memset(mState.Controller.mCurrButtons, 0,
SDL_CONTROLLER_BUTTON_MAX);
memset(mState.Controller.mPrevButtons, 0,
SDL_CONTROLLER_BUTTON_MAX);
mState.ParseBindingFromFile(bindingFile);
return true;
}
void InputSystem::Shutdown()
{
}
void InputSystem::PrepareForUpdate()
{
// Copy current state to previous
// Keyboard
memcpy(mState.Keyboard.mPrevState,
mState.Keyboard.mCurrState,
SDL_NUM_SCANCODES);
// Mouse
mState.Mouse.mPrevButtons = mState.Mouse.mCurrButtons;
mState.Mouse.mIsRelative = false;
mState.Mouse.mScrollWheel = Vector2::Zero;
// Controller
memcpy(mState.Controller.mPrevButtons,
mState.Controller.mCurrButtons,
SDL_CONTROLLER_BUTTON_MAX);
}
void InputSystem::Update()
{
// Mouse
int x = 0, y = 0;
if (mState.Mouse.mIsRelative)
{
mState.Mouse.mCurrButtons =
SDL_GetRelativeMouseState(&x, &y);
}
else
{
mState.Mouse.mCurrButtons =
SDL_GetMouseState(&x, &y);
}
mState.Mouse.mMousePos.x = static_cast<float>(x);
mState.Mouse.mMousePos.y = static_cast<float>(y);
// Controller
// Buttons
for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
{
mState.Controller.mCurrButtons[i] =
SDL_GameControllerGetButton(mController,
SDL_GameControllerButton(i));
}
// Triggers
mState.Controller.mLeftTrigger =
Filter1D(SDL_GameControllerGetAxis(mController,
SDL_CONTROLLER_AXIS_TRIGGERLEFT));
mState.Controller.mRightTrigger =
Filter1D(SDL_GameControllerGetAxis(mController,
SDL_CONTROLLER_AXIS_TRIGGERRIGHT));
// Sticks
x = SDL_GameControllerGetAxis(mController,
SDL_CONTROLLER_AXIS_LEFTX);
y = -SDL_GameControllerGetAxis(mController,
SDL_CONTROLLER_AXIS_LEFTY);
mState.Controller.mLeftStick = Filter2D(x, y);
x = SDL_GameControllerGetAxis(mController,
SDL_CONTROLLER_AXIS_RIGHTX);
y = -SDL_GameControllerGetAxis(mController,
SDL_CONTROLLER_AXIS_RIGHTY);
mState.Controller.mRightStick = Filter2D(x, y);
}
void InputSystem::ProcessEvent(SDL_Event& event)
{
switch (event.type)
{
case SDL_MOUSEWHEEL:
mState.Mouse.mScrollWheel = Vector2(
static_cast<float>(event.wheel.x),
static_cast<float>(event.wheel.y));
break;
default:
break;
}
}
void InputSystem::SetRelativeMouseMode(bool value)
{
SDL_bool set = value ? SDL_TRUE : SDL_FALSE;
SDL_SetRelativeMouseMode(set);
mState.Mouse.mIsRelative = value;
}
float InputSystem::Filter1D(int input)
{
// A value < dead zone is interpreted as 0%
const int deadZone = 250;
// A value > max value is interpreted as 100%
const int maxValue = 30000;
float retVal = 0.0f;
// Take absolute value of input
int absValue = input > 0 ? input : -input;
// Ignore input within dead zone
if (absValue > deadZone)
{
// Compute fractional value between dead zone and max value
retVal = static_cast<float>(absValue - deadZone) /
(maxValue - deadZone);
// Make sure sign matches original value
retVal = input > 0 ? retVal : -1.0f * retVal;
// Clamp between -1.0f and 1.0f
retVal = Math::Clamp(retVal, -1.0f, 1.0f);
}
return retVal;
}
Vector2 InputSystem::Filter2D(int inputX, int inputY)
{
const float deadZone = 8000.0f;
const float maxValue = 30000.0f;
// Make into 2D vector
Vector2 dir;
dir.x = static_cast<float>(inputX);
dir.y = static_cast<float>(inputY);
float length = dir.Length();
// If length < deadZone, should be no input
if (length < deadZone)
{
dir = Vector2::Zero;
}
else
{
// Calculate fractional value between
// dead zone and max value circles
float f = (length - deadZone) / (maxValue - deadZone);
// Clamp f between 0.0f and 1.0f
f = Math::Clamp(f, 0.0f, 1.0f);
// Normalize the vector, and then scale it to the
// fractional value
dir *= f / length;
}
return dir;
}