Khaotic Engine Reborn
Loading...
Searching...
No Matches
input_class.cpp
1#include "input_class.h"
2
3
4input_class::input_class()
5{
6 m_directInput = 0;
7 m_keyboard = 0;
8 m_mouse = 0;
9 m_previousTildeState = false;
10}
11
12
13input_class::input_class(const input_class& other)
14{
15}
16
17
18input_class::~input_class()
19{
20}
21
22bool input_class::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
23{
24 Logger::Get().Log("Initializing input class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
25
26 HRESULT result;
27 int i;
28
29 // Initialize all the keys to being released and not pressed.
30 for (i = 0; i < 256; i++)
31 {
32 m_keys[i] = false;
33 }
34
35 // Store the screen size which will be used for positioning the mouse cursor.
36 m_screenWidth = screenWidth;
37 m_screenHeight = screenHeight;
38
39 // Initialize the location of the mouse on the screen.
40 m_mouseX = 0;
41 m_mouseY = 0;
42
43 // Initialize the main direct input interface.
44 result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
45 if (FAILED(result))
46 {
47 Logger::Get().Log("Failed to create direct input interface", __FILE__, __LINE__, Logger::LogLevel::Error);
48 return false;
49 }
50
51 // Initialize the direct input interface for the keyboard.
52 result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
53 if (FAILED(result))
54 {
55 Logger::Get().Log("Failed to create direct input interface for the keyboard", __FILE__, __LINE__, Logger::LogLevel::Error);
56 return false;
57 }
58
59 // Set the data format. In this case since it is a keyboard we can use the predefined data format.
60 result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
61 if (FAILED(result))
62 {
63 Logger::Get().Log("Failed to set data format for the keyboard", __FILE__, __LINE__, Logger::LogLevel::Error);
64 return false;
65 }
66
67 // Set the cooperative level of the keyboard to share with other programs.
68 result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
69 if (FAILED(result))
70 {
71 Logger::Get().Log("Failed to set cooperative level of the keyboard", __FILE__, __LINE__, Logger::LogLevel::Error);
72 return false;
73 }
74
75 // Now acquire the keyboard.
76 result = m_keyboard->Acquire();
77 if (FAILED(result))
78 {
79 Logger::Get().Log("Failed to acquire the keyboard", __FILE__, __LINE__, Logger::LogLevel::Error);
80 return false;
81 }
82
83 // Initialize the direct input interface for the mouse.
84 result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
85 if (FAILED(result))
86 {
87 Logger::Get().Log("Failed to create direct input interface for the mouse", __FILE__, __LINE__, Logger::LogLevel::Error);
88 return false;
89 }
90
91 // Set the data format for the mouse using the pre-defined mouse data format.
92 result = m_mouse->SetDataFormat(&c_dfDIMouse);
93 if (FAILED(result))
94 {
95 Logger::Get().Log("Failed to set data format for the mouse", __FILE__, __LINE__, Logger::LogLevel::Error);
96 return false;
97 }
98
99 // Set the cooperative level of the mouse to share with other programs.
100 result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
101 if (FAILED(result))
102 {
103 Logger::Get().Log("Failed to set cooperative level of the mouse", __FILE__, __LINE__, Logger::LogLevel::Error);
104 return false;
105 }
106
107 // Acquire the mouse.
108 result = m_mouse->Acquire();
109 if (FAILED(result))
110 {
111 Logger::Get().Log("Failed to acquire the mouse", __FILE__, __LINE__, Logger::LogLevel::Error);
112 return false;
113 }
114
115 Logger::Get().Log("Input class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
116
117 return true;
118}
119
120
121void input_class::KeyDown(unsigned int input)
122{
123 // If a key is pressed then save that state in the key array.
124 Logger::Get().Log("Key down: " + std::to_string(input), __FILE__, __LINE__, Logger::LogLevel::Input);
125 m_keys[input] = true;
126 return;
127}
128
129
130void input_class::KeyUp(unsigned int input)
131{
132 // If a key is released then clear that state in the key array.
133 m_keys[input] = false;
134 return;
135}
136
137
138bool input_class::IsKeyDown(unsigned int key) const
139{
140 // Return what state the key is in (pressed/not pressed).
141 return m_keys[key];
142}
143
144void input_class::Shutdown()
145{
146 Logger::Get().Log("Shutting down input class", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
147
148 // Release the mouse.
149 if (m_mouse)
150 {
151 m_mouse->Unacquire();
152 m_mouse->Release();
153 m_mouse = 0;
154 }
155
156 // Release the keyboard.
157 if (m_keyboard)
158 {
159 m_keyboard->Unacquire();
160 m_keyboard->Release();
161 m_keyboard = 0;
162 }
163
164 // Release the main interface to direct input.
165 if (m_directInput)
166 {
167 m_directInput->Release();
168 m_directInput = 0;
169 }
170
171 Logger::Get().Log("Input class shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
172
173 return;
174}
175
176bool input_class::Frame()
177{
178 bool result;
179
180
181 // Read the current state of the keyboard.
182 result = ReadKeyboard();
183 if (!result)
184 {
185 Logger::Get().Log("Failed to read keyboard state", __FILE__, __LINE__, Logger::LogLevel::Error);
186 return false;
187 }
188
189 // Read the current state of the mouse.
190 result = ReadMouse();
191 if (!result)
192 {
193 Logger::Get().Log("Failed to read mouse state", __FILE__, __LINE__, Logger::LogLevel::Error);
194 return false;
195 }
196
197 // Process the changes in the mouse and keyboard.
198 ProcessInput();
199
200 return true;
201}
202
203bool input_class::ReadKeyboard()
204{
205 HRESULT result;
206
207
208 // Read the keyboard device.
209 result = m_keyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)&m_keyboardState);
210 if (FAILED(result))
211 {
212 // If the keyboard lost focus or was not acquired then try to get control back.
213 if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
214 {
215 m_keyboard->Acquire();
216 }
217 else
218 {
219 Logger::Get().Log("Failed to get keyboard device state", __FILE__, __LINE__, Logger::LogLevel::Error);
220 return false;
221 }
222 }
223
224 return true;
225}
226
227bool input_class::ReadMouse()
228{
229 HRESULT result;
230
231
232 // Read the mouse device.
233 result = m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
234 if (FAILED(result))
235 {
236 // If the mouse lost focus or was not acquired then try to get control back.
237 if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
238 {
239 m_mouse->Acquire();
240 }
241 else
242 {
243 Logger::Get().Log("Failed to get mouse device state", __FILE__, __LINE__, Logger::LogLevel::Error);
244 return false;
245 }
246 }
247
248 return true;
249}
250
251void input_class::ProcessInput()
252{
253 // Update the location of the mouse cursor based on the change of the mouse location during the frame.
254 m_mouseX += m_mouseState.lX;
255 m_mouseY += m_mouseState.lY;
256
258 //if (m_mouseX < 0) { m_mouseX = 0; }
259 if (m_mouseY < -m_screenHeight) { m_mouseY = -m_screenHeight; }
260
261 //if (m_mouseX > m_screenWidth) { m_mouseX = m_screenWidth; }
262 if (m_mouseY > m_screenHeight) { m_mouseY = m_screenHeight; }
263
264 return;
265}
266
267bool input_class::IsEscapePressed() const
268{
269 // Do a bitwise and on the keyboard state to check if the escape key is currently being pressed.
270 if (m_keyboardState[DIK_ESCAPE] & 0x80)
271 {
272 return true;
273 }
274
275 return false;
276}
277
278bool input_class::IsLeftArrowPressed() const
279{
280 if (m_keyboardState[DIK_LEFT] & 0x80)
281 {
282 return true;
283 }
284
285 return false;
286}
287
288
289bool input_class::IsRightArrowPressed() const
290{
291 if (m_keyboardState[DIK_RIGHT] & 0x80)
292 {
293 return true;
294 }
295
296 return false;
297}
298
299bool input_class::IsUpArrowPressed() const
300{
301 if (m_keyboardState[DIK_UP] & 0x80)
302 {
303 return true;
304 }
305
306 return false;
307}
308
309
310bool input_class::IsDownArrowPressed() const
311{
312 if (m_keyboardState[DIK_DOWN] & 0x80)
313 {
314 return true;
315 }
316
317 return false;
318}
319
321// Les touches correspondent aux claviers QWERTY //
323
324bool input_class::IsAPressed() const
325{
326 // Touche A sur QWERTY, Q sur AZERTY
327 if (m_keyboardState[DIK_A] & 0x80)
328 {
329 return true;
330 }
331
332 return false;
333}
334
335bool input_class::IsDPressed() const
336{
337 if (m_keyboardState[DIK_D] & 0x80)
338 {
339 return true;
340 }
341
342 return false;
343}
344
345bool input_class::IsWPressed() const
346{
347 // Touche W sur QWERTY, Z sur AZERTY
348 if (m_keyboardState[DIK_W] & 0x80)
349 {
350 return true;
351 }
352
353 return false;
354}
355
356bool input_class::IsSPressed() const
357{
358 if (m_keyboardState[DIK_S] & 0x80)
359 {
360 return true;
361 }
362
363 return false;
364}
365
366bool input_class::IsQPressed() const
367{
368 // Touche Q sur QWERTY, A sur AZERTY
369 if (m_keyboardState[DIK_Q] & 0x80)
370 {
371 return true;
372 }
373
374 return false;
375}
376
377bool input_class::IsEPressed() const
378{
379 if (m_keyboardState[DIK_E] & 0x80)
380 {
381 return true;
382 }
383
384 return false;
385}
386
387void input_class::GetMouseLocation(int& mouseX, int& mouseY) const
388{
389 mouseX = m_mouseX;
390 mouseY = m_mouseY;
391 return;
392}
393
394bool input_class::IsLeftMousePressed() const
395{
396 // Check the left mouse button state.
397 if (m_mouseState.rgbButtons[0] & 0x80)
398 {
399 return true;
400 }
401
402 return false;
403}
404
405bool input_class::IsRightMousePressed() const
406{
407 // Check the left mouse button state.
408 if (m_mouseState.rgbButtons[1] & 0x80)
409 {
410 return true;
411 }
412
413 return false;
414}
415
416bool input_class::IsScrollUp() const
417{
418 if (m_mouseState.lZ > 0)
419 {
420 return true;
421 }
422
423 return false;
424}
425
426bool input_class::IsScrollDown() const
427{
428 if (m_mouseState.lZ < 0)
429 {
430 return true;
431 }
432
433 return false;
434}
435
436bool input_class::is_key_pressed(const unsigned int key_code)
437{
438 if (m_keyboardState[key_code] & 0x80)
439 {
440 return true;
441 }
442
443 return false;
444}
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158