-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardDevice.cpp
94 lines (79 loc) · 1.67 KB
/
KeyboardDevice.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
/**
* Implementation of the KeyboardDevice class.
*
* @author Francis BISSON
*/
// INCLUDES ////////////////////////////////////////////////////////////////////
#include <cstring>
#include <SDL/SDL.h>
#include "Debug.h"
#include "InputDevice.h"
#include "KeyboardDevice.h"
#include "Keys.h"
// IMPLEMENTATION //////////////////////////////////////////////////////////////
KeyboardDevice::KeyboardDevice()
:InputDevice()
,m_KeyStates(NULL)
{
m_PreviousKeyStates = new Uint8[SDLK_LAST];
}
KeyboardDevice::~KeyboardDevice()
{
delete[] m_PreviousKeyStates;
}
bool KeyboardDevice::Init()
{
bool init = InputDevice::Init();
return init;
}
void KeyboardDevice::Update()
{
InputDevice::Update();
if(m_KeyStates != NULL)
{
memcpy(m_PreviousKeyStates, m_KeyStates, SDLK_LAST * sizeof(Uint8));
}
SDL_PumpEvents();
m_KeyStates = SDL_GetKeyState(NULL);
}
bool KeyboardDevice::IsKeyPressed(Keys key) const
{
return IsKeyPressedImpl(key, false);
}
bool KeyboardDevice::IsKeyTriggered(Keys key) const
{
return IsKeyPressedImpl(key, true);
}
bool KeyboardDevice::IsKeyPressedImpl(Keys key, bool triggered) const
{
int index = -1;
switch(key)
{
case UP:
index = SDLK_UP;
break;
case DOWN:
index = SDLK_DOWN;
break;
case LEFT:
index = SDLK_LEFT;
break;
case RIGHT:
index = SDLK_RIGHT;
break;
case RETURN:
index = SDLK_RETURN;
break;
case ESCAPE:
index = SDLK_ESCAPE;
break;
default:
return false;
}
bool isPressed = (bool)m_KeyStates[index];
if(triggered && isPressed)
{
isPressed = isPressed && !m_PreviousKeyStates[index];
}
return isPressed;
}