-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
331 lines (306 loc) · 8.48 KB
/
input.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
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
#include "input.h"
//constructor
Input::Input()
{
for(size_t i = 0; i < inputNS::KEYS_ARRAY; i++)
{
keysDown[i] = false;
keysPressed[i] = false;
}
newLine = true;
textIn = "";
charIn = 0;
mouseX = 0;
mouseY = 0;
mouseRawX = 0;
mouseRawY = 0;
mouseLButton = false;
mouseRButton = false;
mouseMButton = false;
mouseX1Button = false;
mouseX2Button = false;
for(int i = 0; i < MAX_CONTROLLERS; i++)
{
controllers[i].vibrateTimeLeft = 0;
controllers[i].vibrateTimeRight = 0;
}
thumbstickDeadzone = GAMEPAD_THUMBSTICK_DEADZONE;
triggerDeadzone = GAMEPAD_TRIGGER_DEADZONE;
}
//destructor
Input::~Input()
{
if(mouseCaptured)
ReleaseCapture();
}
//initialize mouse and controller input.
//throws GameError
//pre: hwnd = window handle
// capture = true to capture mouse.
void Input::initialize(HWND hwnd, bool capture)
{
try
{
mouseCaptured = capture;
//register HD mouse
Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE;
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = hwnd;
RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]));
if (mouseCaptured)
SetCapture(hwnd);
//clear controller state
ZeroMemory(controllers, sizeof(ControllerState) * MAX_CONTROLLERS);
checkControllers();
}
catch(...)
{
throw(GameError(gameErrorNS::FATAL_ERROR, L"Error initializing input system"));
}
}
//save key down state
void Input::keyDown(WPARAM wParam)
{
if(wParam < inputNS::KEYS_ARRAY)
{
keysDown[wParam] = true;
keysPressed[wParam] = true;
}
}
//save key up state
void Input::keyUp(WPARAM wParam)
{
if(wParam < inputNS::KEYS_ARRAY)
keysDown[wParam] = false;
}
//save the char just entered in textIn string
void Input::keyIn(WPARAM wParam)
{
if(newLine)
{
textIn.clear();
newLine = false;
}
if(wParam == '\b')
{
if(textIn.length() > 0)
textIn.erase(textIn.size()-1);
}
else
{
textIn += wParam;
charIn = wParam;
}
if((char)wParam == '\r')
newLine = true;
}
//returns true if the specified VIRTUAL KEY is down, otherwise false.
bool Input::isKeyDown(UCHAR vkey) const
{
if(vkey < inputNS::KEYS_ARRAY)
return keysDown[vkey];
else
return false;
}
//return true if the specified VIRTUAL KEY has been pressed in the most recent frame.
//key presses are erased at the end of each frame.
bool Input::wasKeyPressed(UCHAR vkey) const
{
if(vkey < inputNS::KEYS_ARRAY)
return keysPressed[vkey];
else
return false;
}
//return true if any key was pressed in the most recent frame.
//key presses are erased at the end of each frame.
bool Input::anyKeyPressed() const
{
for (size_t i = 0; i < inputNS::KEYS_ARRAY; i++)
if(keysPressed[i] == true)
return true;
return false;
}
//clear the specified key press
void Input::clearKeyPress(UCHAR vkey)
{
if(vkey < inputNS::KEYS_ARRAY)
keysPressed[vkey] = false;
}
//clear specified input buffers where what is any combination of
//KEYS_DOWN, KEYS_PRESSED, MOUSE, TEXT_IN or KEYS_MOUSE_TEXT.
//use OR '|' operator to combine parmeters.
void Input::clear(UCHAR toClear)
{
if(toClear & inputNS::KEYS_DOWN)
for(size_t i = 0; i < inputNS::KEYS_ARRAY; i++)
keysDown[i] = false;
if(toClear & inputNS::KEYS_PRESSED)
for(size_t i = 0; i < inputNS::KEYS_ARRAY; i++)
keysPressed[i] = false;
if(toClear & inputNS::MOUSE)
{
mouseX = 0;
mouseY = 0;
mouseRawX = 0;
mouseRawY = 0;
}
if(toClear & inputNS::TEXT_IN)
{
clearTextIn();
clearCharIn();
}
}
//reads mouse screen position into mouseX, mouseY
void Input::mouseIn(LPARAM lParam)
{
mouseX = GET_X_LPARAM(lParam);
mouseY = GET_Y_LPARAM(lParam);
}
//wheel data is in multiples of 120. positivie values are "scroll up"
void Input::mouseWheelIn(WPARAM wParam)
{
mouseWheel = GET_WHEEL_DELTA_WPARAM(wParam);
}
//reads raw mouse data into mouseRawX, mouseRawY
//compatible with a high-definition mouse
void Input::mouseRawIn(LPARAM lParam)
{
UINT dwSize = 40;
static BYTE lpb[40];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT *raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
mouseRawX = raw->data.mouse.lLastX;
mouseRawY = raw->data.mouse.lLastY;
}
}
//update connection status of game controllers.
void Input::checkControllers()
{
DWORD result;
for( DWORD i = 0; i < MAX_CONTROLLERS; i++)
{
result = XInputGetState(i, &controllers[i].state);
if(result == ERROR_SUCCESS)
controllers[i].connected = true;
else
controllers[i].connected = false;
}
}
//save input from connected game controllers.
void Input::readControllers()
{
DWORD result;
for(DWORD i = 0; i < MAX_CONTROLLERS; i++)
if(controllers[i].connected)
{
result = XInputGetState(i, &controllers[i].state);
if(result == ERROR_DEVICE_NOT_CONNECTED)
controllers[i].connected = false;
}
}
// Vibrates the connected controllers for the desired time.
void Input::vibrateControllers(float frameTime)
{
for(int i = 0; i< MAX_CONTROLLERS; i++)
if(controllers[i].connected)
{
controllers[i].vibrateTimeLeft -= frameTime;
if(controllers[i].vibrateTimeLeft < 0)
{
controllers[i].vibrateTimeLeft = 0;
controllers[i].vibration.wLeftMotorSpeed = 0;
}
controllers[i].vibrateTimeRight -= frameTime;
if(controllers[i].vibrateTimeRight < 0)
{
controllers[i].vibrateTimeRight = 0;
controllers[i].vibration.wRightMotorSpeed = 0;
}
XInputSetState(i, &controllers[i].vibration);
}
}
BYTE Input::getGamepadLeftTrigger(UINT n)
{
BYTE value = getGamepadLeftTriggerUndead(n);
if(value > triggerDeadzone) // if > dead zone
//adjust magnitude relative to the end of the dead zone
value = (value-triggerDeadzone)*255/
(255-triggerDeadzone);
else // else, < dead zone
value = 0;
return value;
}
BYTE Input::getGamepadRightTrigger(UINT n)
{
BYTE value = getGamepadRightTriggerUndead(n);
if(value > triggerDeadzone) // if > dead zone
//adjust magnitude relative to the end of the dead zone
value = (value-triggerDeadzone)*255/
(255-triggerDeadzone);
else // else, < dead zone
value = 0;
return value;
}
SHORT Input::getGamepadThumbLX(UINT n)
{
int x = getGamepadThumbLXUndead(n);
if(x > thumbstickDeadzone) // if +x outside dead zone
//adjust x relative to the deadzone and scale to 0 through 32767
x = (x-thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else if(x < -thumbstickDeadzone) // if -x outside dead zone
//adjust y relative to the deadzone and scale to 0 through -32767
x = (x+thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else // else, x inside dead zone
x = 0; // return 0
return static_cast<SHORT>(x);
}
SHORT Input::getGamepadThumbLY(UINT n)
{
int y = getGamepadThumbLYUndead(n);
if(y > thumbstickDeadzone) // if +y outside dead zone
//adjust magnitude relative to the end of the dead zone
y = (y-thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else if(y < -thumbstickDeadzone) // if -y outside dead zone
//adjust magnitude relative to the end of the dead zone
y = (y+thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else // else, y inside dead zone
y = 0; // return 0
return static_cast<SHORT>(y);
}
SHORT Input::getGamepadThumbRX(UINT n)
{
int x = getGamepadThumbRXUndead(n);
if(x > thumbstickDeadzone) // if +x outside dead zone
//adjust magnitude relative to the end of the dead zone
x = (x-thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else if(x < -thumbstickDeadzone) // if -x outside dead zone
//adjust magnitude relative to the end of the dead zone
x = (x+thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else // else, x inside dead zone
x = 0; // return 0
return static_cast<SHORT>(x);
}
SHORT Input::getGamepadThumbRY(UINT n)
{
int y = getGamepadThumbRYUndead(n);
if(y > thumbstickDeadzone) // if +y outside dead zone
//adjust magnitude relative to the end of the dead zone
y = (y-thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else if(y < -thumbstickDeadzone) // if -y outside dead zone
//adjust magnitude relative to the end of the dead zone
y = (y+thumbstickDeadzone)*32767/
(32767-thumbstickDeadzone);
else // else, y inside dead zone
y = 0; // return 0
return static_cast<SHORT>(y);
}