-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputclass.cpp
More file actions
273 lines (216 loc) · 6.85 KB
/
Inputclass.cpp
File metadata and controls
273 lines (216 loc) · 6.85 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
////////////////////////////////////////////////////////////////////////////////
// Filename: inputclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "inputclass.h"
InputClass::InputClass()
{
}
InputClass::InputClass(const InputClass& other)
{
}
InputClass::~InputClass()
{
}
bool InputClass::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
int i;
HRESULT result;
// Store the screen size which will be used for positioning the mouse cursor.
m_screenWidth = screenWidth;
m_screenHeight = screenHeight;
// Initialize the location of the mouse on the screen.
m_mouseX = 0;
m_mouseY = 0;
//This function call will initialize the interface to Direct Input.Once you have a Direct Input object you can initialize other input devices.
// Initialize the main direct input interface.
result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
if (FAILED(result))
{
return false;
}
//The first input device we will initialize will be the keyboard.
// Initialize the direct input interface for the keyboard.
result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
if (FAILED(result))
{
return false;
}
// Set the data format. In this case since it is a keyboard we can use the predefined data format.
result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
if (FAILED(result))
{
return false;
}
//Setting the cooperative level of the keyboard is important in both what it does and how you use the device from that point forward.In this case we will set it to not share with other programs(DISCL_EXCLUSIVE).This way if you press a key only your application can see that input and no other application will have access to it.However if you want other applications to have access to keyboard input while your program is running you can set it to non - exclusive(DISCL_NONEXCLUSIVE).Now the print screen key works again and other running applications can be controlled by the keyboard and so forth.Just remember that if it is non - exclusive and you are running in a windowed mode then you will need to check for when the device loses focus and when it re - gains that focus so it can re - acquire the device for use again.This focus loss generally happens when other windows become the main focus over your window or your window is minimized.
// Set the cooperative level of the keyboard to not share with other programs.
result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if (FAILED(result))
{
return false;
}
//Once they keyboard is setup we then call Acquire to finally get access to the keyboard for use from this point forward.
// Now acquire the keyboard.
result = m_keyboard->Acquire();
if (FAILED(result))
{
return false;
}
//The next input device we setup is the mouse.
// Initialize the direct input interface for the mouse.
result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
if (FAILED(result))
{
return false;
}
// Set the data format for the mouse using the pre-defined mouse data format.
result = m_mouse->SetDataFormat(&c_dfDIMouse);
if (FAILED(result))
{
return false;
}
//We use non - exclusive cooperative settings for the mouse.We will have to check for when it goes in and out of focus and re - acquire it each time.
// Set the cooperative level of the mouse to share with other programs.
result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if (FAILED(result))
{
return false;
}
//Once the mouse is setup we acquire it so that we can begin using it.
// Acquire the mouse.
result = m_mouse->Acquire();
if (FAILED(result))
{
return false;
}
// Initialize all the keys to being released and not pressed.
for (i = 0; i<256; i++)
{
m_keys[i] = false;
}
return true;
}
void InputClass::KeyDown(unsigned int input)
{
// If a key is pressed then save that state in the key array.
m_keys[input] = true;
return;
}
void InputClass::KeyUp(unsigned int input)
{
// If a key is released then clear that state in the key array.
m_keys[input] = false;
return;
}
bool InputClass::IsKeyDown(unsigned int key)
{
// Return what state the key is in (pressed/not pressed).
return m_keys[key];
}
void InputClass::Shutdown()
{
// Release the mouse.
if (m_mouse)
{
m_mouse->Unacquire();
m_mouse->Release();
m_mouse = 0;
}
// Release the keyboard.
if (m_keyboard)
{
m_keyboard->Unacquire();
m_keyboard->Release();
m_keyboard = 0;
}
// Release the main interface to direct input.
if (m_directInput)
{
m_directInput->Release();
m_directInput = 0;
}
return;
}
bool InputClass::Frame()
{
bool result = false;
// Read the current state of the keyboard.
//result = ReadKeyboard();
if (!result)
//{
// return false;
//}
// Read the current state of the mouse.
result = ReadMouse();
if (!result)
{
return false;
}
// Process the changes in the mouse and keyboard.
ProcessInput();
return true;
}
bool InputClass::ReadKeyboard()
{
HRESULT result;
// Read the keyboard device.
result = m_keyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)&m_keyboardState);
if (FAILED(result))
{
// If the keyboard lost focus or was not acquired then try to get control back.
if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_keyboard->Acquire();
}
else
{
return false;
}
}
return true;
}
bool InputClass::ReadMouse()
{
HRESULT result;
// Read the mouse device.
result = m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
if (FAILED(result))
{
// If the mouse lost focus or was not acquired then try to get control back.
if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_mouse->Acquire();
}
else
{
return false;
}
}
return true;
}
void InputClass::ProcessInput()
{
// Update the location of the mouse cursor based on the change of the mouse location during the frame.
m_mouseX += m_mouseState.lX;
m_mouseY += m_mouseState.lY;
// Ensure the mouse location doesn't exceed the screen width or height.
if (m_mouseX < 0) { m_mouseX = 0; }
if (m_mouseY < 0) { m_mouseY = 0; }
if (m_mouseX > m_screenWidth) { m_mouseX = m_screenWidth; }
if (m_mouseY > m_screenHeight) { m_mouseY = m_screenHeight; }
return;
}
bool InputClass::IsEscapePressed()
{
// Do a bitwise and on the keyboard state to check if the escape key is currently being pressed.
if (m_keyboardState[DIK_ESCAPE] & 0x80)
{
return true;
}
return false;
}
void InputClass::GetMouseLocation(int& mouseX, int& mouseY)
{
mouseX = m_mouseX;
mouseY = m_mouseY;
return;
}