GameKit  0.0.1a
C++ gamedev tools
InputHandler.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: InputHandler.cpp
5  *
6  * Description:
7  *
8  * Created: 17/01/2018 19:10:34
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
15 #include "gk/core/GameClock.hpp"
16 
17 namespace gk {
18 
20  if(isKeyPressed(key)) {
21  if(!m_keysPressedOnce[key]) {
22  m_keysPressedOnce[key] = true;
23  return true;
24  } else {
25  return false;
26  }
27  } else {
28  m_keysPressedOnce[key] = false;
29  return false;
30  }
31 }
32 
34  if(isKeyPressed(key) && GameClock::getTicks() - m_lastTimePressed[key] > delay) {
36  return true;
37  } else {
38  if(!isKeyPressed(key)) m_lastTimePressed[key] = 0;
39  return false;
40  }
41 }
42 
44  m_keysPressed[key] = false;
45  m_keysPressedOnce[key] = false;
46  m_lastTimePressed[key] = 0;
47 }
48 
49 } // namespace gk
50 
unsigned short u16
Definition: IntTypes.hpp:22
virtual bool isKeyPressedWithDelay(GameKey key, u16 delay)
Check if a key is pressed with delay.
std::map< GameKey, bool > m_keysPressed
Keys state.
virtual bool isKeyPressedOnce(GameKey key)
Check if a key is pressed only one time.
virtual bool isKeyPressed(GameKey key)
Check if a key is pressed.
std::map< GameKey, u32 > m_lastTimePressed
Keys state for isKeyPressedWithDelay.
std::map< GameKey, bool > m_keysPressedOnce
Keys state for isKeyPressedOnce.
u32 GameKey
Alias for game key type.
static u32 getTicks(bool realTime=false)
Definition: GameClock.cpp:21
void addKey(GameKey key)
Add a new key to this input handler.