-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputmanager.cpp
executable file
·134 lines (120 loc) · 2.87 KB
/
inputmanager.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
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
#include "inputmanager.h"
#include <iostream>
// Key state of current frame.
std::map<GLuint, bool> InputManager::keyMap;
// Key state of previous frame.
std::map<GLuint, bool> InputManager::prevKeyMap;
std::map<GLuint, bool> InputManager::events;
glm::ivec2 InputManager::relativeMouseCoord;
glm::ivec2 InputManager::globalMouseCoord;
glm::ivec2 InputManager::deltaMouseCoord;
bool InputManager::wasKeyDown(GLuint keyID)
{
auto it = prevKeyMap.find(keyID);
if (it != prevKeyMap.end())
{
// Found the key.
return it->second;
}
// Did not found the key.
return false;
}
void InputManager::pressEvent(GLuint eventID)
{
events[eventID] = true;
}
void InputManager::updateState()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
pressEvent(SDL_QUIT);
break;
case SDL_KEYDOWN:
pressKey(event.key.keysym.sym);
break;
case SDL_KEYUP:
releaseKey(event.key.keysym.sym);
break;
case SDL_MOUSEBUTTONDOWN:
pressKey(event.button.button);
break;
case SDL_MOUSEBUTTONUP:
releaseKey(event.button.button);
break;
}
}
}
void InputManager::update()
{
SDL_GetRelativeMouseState(&deltaMouseCoord.x, &deltaMouseCoord.y);
// for(auto it:keyMap)
for (auto it = keyMap.begin(); it != keyMap.end(); it++)
{
prevKeyMap[it->first] = it->second;
}
updateState();
}
void InputManager::pressKey(GLuint keyID)
{
keyMap[keyID] = true;
}
void InputManager::releaseKey(GLuint keyID)
{
keyMap[keyID] = false;
}
glm::ivec2 InputManager::getDeltaMouseCoord()
{
return deltaMouseCoord;
}
glm::ivec2 InputManager::getRelativeMouseCoord()
{
SDL_GetMouseState(&relativeMouseCoord.x, &relativeMouseCoord.y);
return relativeMouseCoord;
}
glm::ivec2 InputManager::getGlobalMouseCoord()
{
SDL_GetGlobalMouseState(&globalMouseCoord.x, &globalMouseCoord.y);
return globalMouseCoord;
}
bool InputManager::isKeyDown(GLuint keyID)
{
auto it = keyMap.find(keyID);
if (it != keyMap.end())
{
// Found the key.
return it->second;
}
// Did not found the key.
return false;
}
bool InputManager::isKeyReleased(GLuint keyID)
{
if (wasKeyDown(keyID) == true && isKeyDown(keyID) == false)
{
return true;
}
return false;
}
bool InputManager::isKeyPressed(GLuint keyID)
{
if (wasKeyDown(keyID) == false && isKeyDown(keyID) == true)
{
return true;
}
return false;
}
bool InputManager::isEventPending(GLuint eventID)
{
auto it = events.find(eventID);
if (it != events.end())
{
// Found the key.
return it->second;
}
// Did not found the key.
return false;
}