Khaotic Engine Reborn
Loading...
Searching...
No Matches
input_class Class Reference

Public Member Functions

 input_class (const input_class &)
 
virtual bool Initialize (HINSTANCE, HWND, int, int)
 
virtual void Shutdown ()
 
virtual bool Frame ()
 
bool IsEscapePressed () const
 
void GetMouseLocation (int &, int &) const
 
bool IsLeftMousePressed () const
 
bool IsRightMousePressed () const
 
void KeyDown (unsigned int)
 
void KeyUp (unsigned int)
 
bool IsLeftArrowPressed () const
 
bool IsRightArrowPressed () const
 
bool IsScrollUp () const
 
bool IsScrollDown () const
 
bool IsUpArrowPressed () const
 
bool IsDownArrowPressed () const
 
bool IsAPressed () const
 
bool IsDPressed () const
 
bool IsWPressed () const
 
bool IsSPressed () const
 
bool IsQPressed () const
 
bool IsEPressed () const
 
bool IsKeyDown (unsigned int) const
 
bool is_key_pressed (const unsigned int)
 

Detailed Description

Definition at line 24 of file input_class.h.

Constructor & Destructor Documentation

◆ input_class() [1/2]

input_class::input_class ( )

Definition at line 4 of file input_class.cpp.

5{
6 m_directInput = 0;
7 m_keyboard = 0;
8 m_mouse = 0;
9 m_previousTildeState = false;
10}

◆ input_class() [2/2]

input_class::input_class ( const input_class & other)

Definition at line 13 of file input_class.cpp.

14{
15}

◆ ~input_class()

input_class::~input_class ( )

Definition at line 18 of file input_class.cpp.

19{
20}

Member Function Documentation

◆ Frame()

bool input_class::Frame ( )
virtual

Definition at line 176 of file input_class.cpp.

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}
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

◆ GetMouseLocation()

void input_class::GetMouseLocation ( int & mouseX,
int & mouseY ) const

Definition at line 387 of file input_class.cpp.

388{
389 mouseX = m_mouseX;
390 mouseY = m_mouseY;
391 return;
392}

◆ Initialize()

bool input_class::Initialize ( HINSTANCE hinstance,
HWND hwnd,
int screenWidth,
int screenHeight )
virtual

Definition at line 22 of file input_class.cpp.

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}

◆ is_key_pressed()

bool input_class::is_key_pressed ( const unsigned int key_code)

Definition at line 436 of file input_class.cpp.

437{
438 if (m_keyboardState[key_code] & 0x80)
439 {
440 return true;
441 }
442
443 return false;
444}

◆ IsAPressed()

bool input_class::IsAPressed ( ) const

Definition at line 324 of file input_class.cpp.

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}

◆ IsDownArrowPressed()

bool input_class::IsDownArrowPressed ( ) const

Definition at line 310 of file input_class.cpp.

311{
312 if (m_keyboardState[DIK_DOWN] & 0x80)
313 {
314 return true;
315 }
316
317 return false;
318}

◆ IsDPressed()

bool input_class::IsDPressed ( ) const

Definition at line 335 of file input_class.cpp.

336{
337 if (m_keyboardState[DIK_D] & 0x80)
338 {
339 return true;
340 }
341
342 return false;
343}

◆ IsEPressed()

bool input_class::IsEPressed ( ) const

Definition at line 377 of file input_class.cpp.

378{
379 if (m_keyboardState[DIK_E] & 0x80)
380 {
381 return true;
382 }
383
384 return false;
385}

◆ IsEscapePressed()

bool input_class::IsEscapePressed ( ) const

Definition at line 267 of file input_class.cpp.

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}

◆ IsKeyDown()

bool input_class::IsKeyDown ( unsigned int key) const

Definition at line 138 of file input_class.cpp.

139{
140 // Return what state the key is in (pressed/not pressed).
141 return m_keys[key];
142}

◆ IsLeftArrowPressed()

bool input_class::IsLeftArrowPressed ( ) const

Definition at line 278 of file input_class.cpp.

279{
280 if (m_keyboardState[DIK_LEFT] & 0x80)
281 {
282 return true;
283 }
284
285 return false;
286}

◆ IsLeftMousePressed()

bool input_class::IsLeftMousePressed ( ) const

Definition at line 394 of file input_class.cpp.

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}

◆ IsQPressed()

bool input_class::IsQPressed ( ) const

Definition at line 366 of file input_class.cpp.

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}

◆ IsRightArrowPressed()

bool input_class::IsRightArrowPressed ( ) const

Definition at line 289 of file input_class.cpp.

290{
291 if (m_keyboardState[DIK_RIGHT] & 0x80)
292 {
293 return true;
294 }
295
296 return false;
297}

◆ IsRightMousePressed()

bool input_class::IsRightMousePressed ( ) const

Definition at line 405 of file input_class.cpp.

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}

◆ IsScrollDown()

bool input_class::IsScrollDown ( ) const

Definition at line 426 of file input_class.cpp.

427{
428 if (m_mouseState.lZ < 0)
429 {
430 return true;
431 }
432
433 return false;
434}

◆ IsScrollUp()

bool input_class::IsScrollUp ( ) const

Definition at line 416 of file input_class.cpp.

417{
418 if (m_mouseState.lZ > 0)
419 {
420 return true;
421 }
422
423 return false;
424}

◆ IsSPressed()

bool input_class::IsSPressed ( ) const

Definition at line 356 of file input_class.cpp.

357{
358 if (m_keyboardState[DIK_S] & 0x80)
359 {
360 return true;
361 }
362
363 return false;
364}

◆ IsUpArrowPressed()

bool input_class::IsUpArrowPressed ( ) const

Definition at line 299 of file input_class.cpp.

300{
301 if (m_keyboardState[DIK_UP] & 0x80)
302 {
303 return true;
304 }
305
306 return false;
307}

◆ IsWPressed()

bool input_class::IsWPressed ( ) const

Definition at line 345 of file input_class.cpp.

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}

◆ KeyDown()

void input_class::KeyDown ( unsigned int input)

Definition at line 121 of file input_class.cpp.

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}

◆ KeyUp()

void input_class::KeyUp ( unsigned int input)

Definition at line 130 of file input_class.cpp.

131{
132 // If a key is released then clear that state in the key array.
133 m_keys[input] = false;
134 return;
135}

◆ Shutdown()

void input_class::Shutdown ( )
virtual

Definition at line 144 of file input_class.cpp.

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}

The documentation for this class was generated from the following files: