This repository has been archived by the owner on Mar 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
293 lines (265 loc) · 9.9 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
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// input.cpp v1.0
#include "input.h"
//=============================================================================
// default constructor
//=============================================================================
Input::Input()
{
// clear key down array
for (size_t i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
keysDown[i] = false;
// clear key pressed array
for (size_t i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
keysPressed[i] = false;
newLine = true; // start new line
textIn = ""; // clear textIn
charIn = 0; // clear charIn
// mouse data
mouseX = 0; // screen X
mouseY = 0; // screen Y
mouseRawX = 0; // high-definition X
mouseRawY = 0; // high-definition Y
mouseLButton = false; // true if left mouse button is down
mouseMButton = false; // true if middle mouse button is down
mouseRButton = false; // true if right mouse button is down
mouseX1Button = false; // true if X1 mouse button is down
mouseX2Button = false; // true if X2 mouse button is down
for(int i=0; i<MAX_CONTROLLERS; i++)
{
controllers[i].vibrateTimeLeft = 0;
controllers[i].vibrateTimeRight = 0;
}
}
//=============================================================================
// destructor
//=============================================================================
Input::~Input()
{
if(mouseCaptured)
ReleaseCapture(); // release mouse
}
//=============================================================================
// Initialize mouse and controller input
// Set capture=true to capture mouse
// Throws GameError
//=============================================================================
void Input::initialize(HWND hwnd, bool capture)
{
try{
mouseCaptured = capture;
// register high-definition 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); // capture mouse
// Clear controllers state
ZeroMemory( controllers, sizeof(ControllerState) * MAX_CONTROLLERS );
checkControllers(); // check for connected controllers
}
catch(...)
{
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing input system"));
}
}
//=============================================================================
// Set true in the keysDown and keysPessed array for this key
// Pre: wParam contains the virtual key code (0--255)
//=============================================================================
void Input::keyDown(WPARAM wParam)
{
// make sure key code is within buffer range
if (wParam < inputNS::KEYS_ARRAY_LEN)
{
keysDown[wParam] = true; // update keysDown array
// key has been "pressed, erased by clear()
keysPressed[wParam] = true; // update keysPressed array
}
}
//=============================================================================
// Set false in the keysDown array for this key
// Pre: wParam contains the virtual key code (0--255)
//=============================================================================
void Input::keyUp(WPARAM wParam)
{
// make sure key code is within buffer range
if (wParam < inputNS::KEYS_ARRAY_LEN)
// update state table
keysDown[wParam] = false;
}
//=============================================================================
// Save the char just entered in textIn string
// Pre: wParam contains the char
//=============================================================================
void Input::keyIn(WPARAM wParam)
{
if (newLine) // if start of new line
{
textIn.clear();
newLine = false;
}
if (wParam == '\b') // if backspace
{
if(textIn.length() > 0) // if characters exist
textIn.erase(textIn.size()-1); // erase last character entered
}
else
{
textIn += wParam; // add character to textIn
charIn = wParam; // save last char entered
}
if ((char)wParam == '\r') // if return
newLine = true; // start new line
}
//=============================================================================
// Returns true if the specified VIRTUAL KEY is down, otherwise false.
//=============================================================================
bool Input::isKeyDown(UCHAR vkey) const
{
if (vkey < inputNS::KEYS_ARRAY_LEN)
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_LEN)
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_LEN; i++)
if(keysPressed[i] == true)
return true;
return false;
}
//=============================================================================
// Clear the specified key press
//=============================================================================
void Input::clearKeyPress(UCHAR vkey)
{
if (vkey < inputNS::KEYS_ARRAY_LEN)
keysPressed[vkey] = false;
}
//=============================================================================
// Clear specified input buffers
// See input.h for what values
//=============================================================================
void Input::clear(UCHAR what)
{
if(what & inputNS::KEYS_DOWN) // if clear keys down
{
for (size_t i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
keysDown[i] = false;
}
if(what & inputNS::KEYS_PRESSED) // if clear keys pressed
{
for (size_t i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
keysPressed[i] = false;
}
if(what & inputNS::MOUSE) // if clear mouse
{
mouseX = 0;
mouseY = 0;
mouseRawX = 0;
mouseRawY = 0;
}
if(what & inputNS::TEXT_IN)
clearTextIn();
}
//=============================================================================
// Reads mouse screen position into mouseX, mouseY
//=============================================================================
void Input::mouseIn(LPARAM lParam)
{
mouseX = GET_X_LPARAM(lParam);
mouseY = GET_Y_LPARAM(lParam);
}
//=============================================================================
// Reads raw mouse data into mouseRawX, mouseRawY
// This routine is 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;
}
}
//=============================================================================
// Check for connected 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;
}
}
//=============================================================================
// Read state of connected 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) // if controller disconnected
controllers[i].connected = false;
}
}
}
//=============================================================================
// Vibrate connected controllers
//=============================================================================
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);
}
}
}