forked from vixorien/D3D11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cpp
389 lines (338 loc) · 13.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
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
#include "Input.h"
#include <hidusage.h>
// Singleton requirement
Input* Input::instance;
// --------------- Basic usage -----------------
//
// This class is set up as a singleton, meaning there
// is only ever one instance of the class. You can
// access that instance through the static GetInstance()
// function, like so:
//
// Input::GetInstance().SomeFunctionHere()
//
// To make your code less verbose, I suggest storing
// a reference to this instance in a temporary variable
// if you plan on call multiple functions in a row:
//
// Input& input = Input::GetInstance();
// if (input.KeyDown('W')) { }
// if (input.KeyDown('A')) { }
// if (input.KeyDown('S')) { }
// if (input.KeyDown('D')) { }
//
//
// The keyboard functions all take a single character
// like 'W', ' ' or '8' (which will implicitly cast
// to an int) or a pre-defined virtual key code like
// VK_SHIFT, VK_ESCAPE or VK_TAB. These virtual key
// codes are are accessible through the Windows.h
// file (already included in Input.h). See the
// following for a complete list of virtual key codes:
// https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
//
// Checking if various keys are down or up:
//
// Input& input = Input::GetInstance();
// if (input.KeyDown('W')) { }
// if (input.KeyUp('2')) { }
// if (input.KeyDown(VK_SHIFT)) { }
//
//
// Checking if a key was initially pressed or released
// this frame:
//
// Input& input = Input::GetInstance();
// if (input.KeyPressed('Q')) { }
// if (input.KeyReleased(' ')) { }
//
// (Note that these functions will only return true on
// the FIRST frame that a key is pressed or released.)
//
//
// Checking for mouse button input is similar:
//
// Input& input = Input::GetInstance();
// if (input.MouseLeftDown()) { }
// if (input.MouseRightDown()) { }
// if (input.MouseMiddleUp()) { }
// if (input.MouseLeftPressed()) { }
// if (input.MouseRightReleased()) { }
//
//
// To handle relative mouse movement, you can use either
// "standard" or "raw" mouse input, as shown below:
//
// - *Standard* input simply reads the cursor position on
// the screen each frame and calculates the delta,
// which respects pointer acceleration. Use these
// functions if you expect the same pointer behavior
// as your mouse cursor in Windows.
//
// Input& input = Input::GetInstance();
// int xDelta = input.GetMouseXDelta();
// int yDelta = input.GetMouseYDelta();
//
// - *Raw* input is read directly from the device, and is
// a measure of how far the *mouse* moved, not the *cursor*.
// Use these functions if you want high-precision movements
// independent of the operating system or screen pixels.
//
// Input& input = Input::GetInstance();
// int xRawDelta = input.GetRawMouseXDelta();
// int yRawDelta = input.GetRawMouseYDelta();
// ^^^
//
// ---------------------------------------------
// --------------------------
// Cleans up the key arrays
// --------------------------
Input::~Input()
{
delete[] kbState;
delete[] prevKbState;
}
// ---------------------------------------------------
// Initializes the input variables and sets up the
// initial arrays of key states
//
// windowHandle - the handle (id) of the window,
// which is necessary for mouse input
// ---------------------------------------------------
void Input::Initialize(HWND windowHandle)
{
kbState = new unsigned char[256];
prevKbState = new unsigned char[256];
memset(kbState, 0, sizeof(unsigned char) * 256);
memset(prevKbState, 0, sizeof(unsigned char) * 256);
wheelDelta = 0.0f;
mouseX = 0; mouseY = 0;
prevMouseX = 0; prevMouseY = 0;
mouseXDelta = 0; mouseYDelta = 0;
keyboardCaptured = false; mouseCaptured = false;
this->windowHandle = windowHandle;
// Register for raw input from the mouse
RAWINPUTDEVICE mouse = {};
mouse.usUsagePage = HID_USAGE_PAGE_GENERIC;
mouse.usUsage = HID_USAGE_GENERIC_MOUSE;
mouse.dwFlags = RIDEV_INPUTSINK;
mouse.hwndTarget = windowHandle;
RegisterRawInputDevices(&mouse, 1, sizeof(mouse));
}
// ----------------------------------------------------------
// Updates the input manager for this frame. This should
// be called at the beginning of every Game::Update(),
// before anything that might need input
// ----------------------------------------------------------
void Input::Update()
{
// Copy the old keys so we have last frame's data
memcpy(prevKbState, kbState, sizeof(unsigned char) * 256);
// Get the latest keys (from Windows)
// Note the use of (void), which denotes to the compiler
// that we're intentionally ignoring the return value
(void)GetKeyboardState(kbState);
// Get the current mouse position then make it relative to the window
POINT mousePos = {};
GetCursorPos(&mousePos);
ScreenToClient(windowHandle, &mousePos);
// Save the previous mouse position, then the current mouse
// position and finally calculate the change from the previous frame
prevMouseX = mouseX;
prevMouseY = mouseY;
mouseX = mousePos.x;
mouseY = mousePos.y;
mouseXDelta = mouseX - prevMouseX;
mouseYDelta = mouseY - prevMouseY;
}
// ----------------------------------------------------------
// Resets the mouse wheel value and raw mouse delta at the
// end of the frame. This cannot occur earlier in the frame,
// since these come from Win32 windowing messages, which are
// handled between frames.
// ----------------------------------------------------------
void Input::EndOfFrame()
{
// Reset wheel value
wheelDelta = 0;
rawMouseXDelta = 0;
rawMouseYDelta = 0;
}
// ----------------------------------------------------------
// Get the mouse's current position in pixels relative
// to the top left corner of the window.
// ----------------------------------------------------------
int Input::GetMouseX() { return mouseX; }
int Input::GetMouseY() { return mouseY; }
// ---------------------------------------------------------------
// Get the mouse's change (delta) in position since last
// frame in pixels relative to the top left corner of the window.
// ---------------------------------------------------------------
int Input::GetMouseXDelta() { return mouseXDelta; }
int Input::GetMouseYDelta() { return mouseYDelta; }
// ---------------------------------------------------------------
// Passes raw mouse input data to the input manager to be
// processed. This input is the lParam of the WM_INPUT
// windows message, captured from (presumably) DXCore.
//
// See the following article for a discussion on different
// types of mouse input, not including GetCursorPos():
// https://learn.microsoft.com/en-us/windows/win32/dxtecharts/taking-advantage-of-high-dpi-mouse-movement
// ---------------------------------------------------------------
void Input::ProcessRawMouseInput(LPARAM lParam)
{
// Variables for the raw data and its size
unsigned char rawInputBytes[sizeof(RAWINPUT)] = {};
unsigned int sizeOfData = sizeof(RAWINPUT);
// Get raw input data from the lowest possible level and verify
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, rawInputBytes, &sizeOfData, sizeof(RAWINPUTHEADER)) == -1)
return;
// Got data, so cast to the proper type and check the results
RAWINPUT* raw = (RAWINPUT*)rawInputBytes;
if (raw->header.dwType == RIM_TYPEMOUSE)
{
// This is mouse data, so grab the movement values
rawMouseXDelta = raw->data.mouse.lLastX;
rawMouseYDelta = raw->data.mouse.lLastY;
}
}
// ---------------------------------------------------------------
// Get the mouse's change (delta) in position since last
// frame based on raw mouse data (no pointer acceleration)
// ---------------------------------------------------------------
int Input::GetRawMouseXDelta() { return rawMouseXDelta; }
int Input::GetRawMouseYDelta() { return rawMouseYDelta; }
// ---------------------------------------------------------------
// Get the mouse wheel delta for this frame. Note that there is
// no absolute position for the mouse wheel; this is either a
// positive number, a negative number or zero.
// ---------------------------------------------------------------
float Input::GetMouseWheel() { return wheelDelta; }
// ---------------------------------------------------------------
// Sets the mouse wheel delta for this frame. This is called
// by DXCore whenever an OS-level mouse wheel message is sent
// to the application. You'll never need to call this yourself.
// ---------------------------------------------------------------
void Input::SetWheelDelta(float delta)
{
wheelDelta = delta;
}
// ---------------------------------------------------------------
// Sets whether or not keyboard input is "captured" elsewhere.
// If the keyboard is "captured", the input manager will report
// false on all keyboard input.
// ---------------------------------------------------------------
void Input::SetKeyboardCapture(bool captured)
{
keyboardCaptured = captured;
}
// ---------------------------------------------------------------
// Sets whether or not mouse input is "captured" elsewhere.
// If the mouse is "captured", the input manager will report
// false on all mouse input.
// ---------------------------------------------------------------
void Input::SetMouseCapture(bool captured)
{
mouseCaptured = captured;
}
// ----------------------------------------------------------
// Is the given key down this frame?
//
// key - The key to check, which could be a single character
// like 'W' or '3', or a virtual key code like VK_TAB,
// VK_ESCAPE or VK_SHIFT.
// ----------------------------------------------------------
bool Input::KeyDown(int key)
{
if (key < 0 || key > 255) return false;
return (kbState[key] & 0x80) != 0 && !keyboardCaptured;
}
// ----------------------------------------------------------
// Is the given key up this frame?
//
// key - The key to check, which could be a single character
// like 'W' or '3', or a virtual key code like VK_TAB,
// VK_ESCAPE or VK_SHIFT.
// ----------------------------------------------------------
bool Input::KeyUp(int key)
{
if (key < 0 || key > 255) return false;
return !(kbState[key] & 0x80) && !keyboardCaptured;
}
// ----------------------------------------------------------
// Was the given key initially pressed this frame?
//
// key - The key to check, which could be a single character
// like 'W' or '3', or a virtual key code like VK_TAB,
// VK_ESCAPE or VK_SHIFT.
// ----------------------------------------------------------
bool Input::KeyPress(int key)
{
if (key < 0 || key > 255) return false;
return
kbState[key] & 0x80 && // Down now
!(prevKbState[key] & 0x80) && // Up last frame
!keyboardCaptured;
}
// ----------------------------------------------------------
// Was the given key initially released this frame?
//
// key - The key to check, which could be a single character
// like 'W' or '3', or a virtual key code like VK_TAB,
// VK_ESCAPE or VK_SHIFT.
// ----------------------------------------------------------
bool Input::KeyRelease(int key)
{
if (key < 0 || key > 255) return false;
return
!(kbState[key] & 0x80) && // Up now
prevKbState[key] & 0x80 && // Down last frame
!keyboardCaptured;
}
// ----------------------------------------------------------
// A utility function to fill a given array of booleans
// with the current state of the keyboard. This is most
// useful when hooking the engine's input up to another
// system, such as a user interface library. (You probably
// won't use this very much, if at all!)
//
// keyArray - pointer to a boolean array which will be
// filled with the current keyboard state
// size - the size of the boolean array (up to 256)
//
// Returns true if the size parameter was valid and false
// if it was <= 0 or > 256
// ----------------------------------------------------------
bool Input::GetKeyArray(bool* keyArray, int size)
{
if (size <= 0 || size > 256) return false;
// Loop through the given size and fill the
// boolean array. Note that the double exclamation
// point is on purpose; it's a quick way to
// convert any number to a boolean.
for (int i = 0; i < size; i++)
keyArray[i] = !!(kbState[i] & 0x80);
return true;
}
// ----------------------------------------------------------
// Is the specific mouse button down this frame?
// ----------------------------------------------------------
bool Input::MouseLeftDown() { return (kbState[VK_LBUTTON] & 0x80) != 0 && !mouseCaptured; }
bool Input::MouseRightDown() { return (kbState[VK_RBUTTON] & 0x80) != 0 && !mouseCaptured; }
bool Input::MouseMiddleDown() { return (kbState[VK_MBUTTON] & 0x80) != 0 && !mouseCaptured; }
// ----------------------------------------------------------
// Is the specific mouse button up this frame?
// ----------------------------------------------------------
bool Input::MouseLeftUp() { return !(kbState[VK_LBUTTON] & 0x80) && !mouseCaptured; }
bool Input::MouseRightUp() { return !(kbState[VK_RBUTTON] & 0x80) && !mouseCaptured; }
bool Input::MouseMiddleUp() { return !(kbState[VK_MBUTTON] & 0x80) && !mouseCaptured; }
// ----------------------------------------------------------
// Was the specific mouse button initially
// pressed or released this frame?
// ----------------------------------------------------------
bool Input::MouseLeftPress() { return kbState[VK_LBUTTON] & 0x80 && !(prevKbState[VK_LBUTTON] & 0x80) && !mouseCaptured; }
bool Input::MouseLeftRelease() { return !(kbState[VK_LBUTTON] & 0x80) && prevKbState[VK_LBUTTON] & 0x80 && !mouseCaptured; }
bool Input::MouseRightPress() { return kbState[VK_RBUTTON] & 0x80 && !(prevKbState[VK_RBUTTON] & 0x80) && !mouseCaptured; }
bool Input::MouseRightRelease() { return !(kbState[VK_RBUTTON] & 0x80) && prevKbState[VK_RBUTTON] & 0x80 && !mouseCaptured; }
bool Input::MouseMiddlePress() { return kbState[VK_MBUTTON] & 0x80 && !(prevKbState[VK_MBUTTON] & 0x80) && !mouseCaptured; }
bool Input::MouseMiddleRelease() { return !(kbState[VK_MBUTTON] & 0x80) && prevKbState[VK_MBUTTON] & 0x80 && !mouseCaptured; }