/
Input.cpp
189 lines (163 loc) · 3.73 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
#include "Input.h"
#include "GameError.h"
Input::Input()
{
for (int i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
m_keysDown[i] = false;
for (int i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
m_keysPressed[i] = false;
m_newLine = true;
m_textIn = "";
m_charIn = 0;
m_mouseX = 0;
m_mouseY = 0;
m_mouseRawX = 0;
m_mouseRawY = 0;
m_mouseLButton = false;
m_mouseMButton = false;
m_mouseRButton = false;
m_mouseX1Button = false;
m_mouseX2Button = false;
for (int i = 0; i < MAX_CONTROLLERS; i++)
{
m_controllers[i].vibrateTimeLeft = 0;
m_controllers[i].vibrateTimeRight = 0;
}
}
void Input::initialize(HWND hwnd, bool mouseCaptured)
{
try
{
m_mouseCaptured = mouseCaptured;
m_rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
m_rid[0].usUsage = HID_USAGE_GENERIC_MOUSE;
m_rid[0].dwFlags = RIDEV_INPUTSINK;
m_rid[0].hwndTarget = hwnd;
RegisterRawInputDevices(m_rid, 1, sizeof(m_rid[0]));
if (m_mouseCaptured)
SetCapture(hwnd);
ZeroMemory(m_controllers, sizeof(ControllerState) * MAX_CONTROLLERS);
//checkControllers();
}
catch (...)
{
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing input system"));
}
}
void Input::keyDown(WPARAM wParam)
{
if (wParam < inputNS::KEYS_ARRAY_LEN)
{
m_keysDown[wParam] = true;
m_keysPressed[wParam] = true;
}
}
bool Input::isKeyDown(UCHAR vkey) const
{
if (vkey < inputNS::KEYS_ARRAY_LEN)
return m_keysDown[vkey];
else
return false;
}
bool Input::wasKeyPressed(UCHAR vkey) const
{
if (vkey < inputNS::KEYS_ARRAY_LEN)
return m_keysPressed[vkey];
else
return false;
}
bool Input::anyKeyPressed() const
{
for (int i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
if (m_keysPressed[i] == true)
return true;
return false;
}
void Input::keyUp(WPARAM wParam)
{
if (wParam < inputNS::KEYS_ARRAY_LEN)
{
m_keysDown[wParam] = false;
}
}
void Input::keyIn(WPARAM wParam)
{
if (m_newLine)
{
m_textIn.clear();
m_newLine = false;
}
if ((char)wParam == '\b')
{
if (m_textIn.length() > 0)
m_textIn.erase(m_textIn.size() - 1);
}
else
{
m_textIn += wParam;
m_charIn = wParam;
}
if ((char)wParam == '\r')
m_newLine = true;
const UCHAR CONSOLE_KEY = VK_OEM_3;
const UCHAR ESC_KEY = VK_ESCAPE;
const UCHAR ALT_KEY = VK_MENU;
const UCHAR ENTER_KEY = VK_RETURN;
const UCHAR SHIP1_LEFT_KEY = 'A';
const UCHAR SHIP1_RIGHT_KEY = 'D';
const UCHAR SHIP1_FORWARD_KEY = 'W';
const UCHAR SHIP1_FIRE_KEY = 'S';
const UCHAR SHIP2_LEFT_KEY = VK_LEFT;
const UCHAR SHIP2_RIGHT_KEY = VK_RIGHT;
const UCHAR SHIP2_FORWARD_KEY = VK_UP;
const UCHAR SHIP2_FIRE_KEY = VK_DOWN;
}
void Input::clearKeyPress(UCHAR vkey)
{
if (vkey < inputNS::KEYS_ARRAY_LEN)
m_keysPressed[vkey] = false;
}
void Input::mouseIn(LPARAM lParam)
{
m_mouseX = GET_X_LPARAM(lParam);
m_mouseY = GET_Y_LPARAM(lParam);
}
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)
{
m_mouseRawX = raw->data.mouse.lLastX;
m_mouseRawY = raw->data.mouse.lLastY;
}
}
void Input::clear(UCHAR what)
{
if (what & inputNS::KEYS_DOWN)
{
for (int i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
m_keysDown[i] = false;
}
if (what & inputNS::KEYS_PRESSED)
{
for (int i = 0; i < inputNS::KEYS_ARRAY_LEN; i++)
m_keysPressed[i] = false;
}
if (what & inputNS::MOUSE)
{
m_mouseX = 0;
m_mouseY = 0;
m_mouseRawX = 0;
m_mouseRawY = 0;
}
if (what * inputNS::TEXT_IN)
clearTextIn();
}
Input::~Input()
{
if (m_mouseCaptured)
ReleaseCapture();
}