forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cpp
More file actions
72 lines (60 loc) · 1.76 KB
/
InputManager.cpp
File metadata and controls
72 lines (60 loc) · 1.76 KB
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
#include "InputManager.h"
namespace Bengine {
InputManager::InputManager() : _mouseCoords(0.0f)
{
}
InputManager::~InputManager()
{
}
void InputManager::update() {
// Loop through _keyMap using a for each loop, and copy it over to _previousKeyMap
for (auto& it : _keyMap) {
_previousKeyMap[it.first] = it.second;
}
}
void InputManager::pressKey(unsigned int keyID) {
// Here we are treating _keyMap as an associative array.
// if keyID doesn't already exist in _keyMap, it will get added
_keyMap[keyID] = true;
}
void InputManager::releaseKey(unsigned int keyID) {
_keyMap[keyID] = false;
}
void InputManager::setMouseCoords(float x, float y) {
_mouseCoords.x = x;
_mouseCoords.y = y;
}
bool InputManager::isKeyDown(unsigned int keyID) {
// We dont want to use the associative array approach here
// because we don't want to create a key if it doesnt exist.
// So we do it manually
auto it = _keyMap.find(keyID);
if (it != _keyMap.end()) {
// Found the key
return it->second;
} else {
// Didn't find the key
return false;
}
}
bool InputManager::isKeyPressed(unsigned int keyID) {
// Check if it is pressed this frame, and wasn't pressed last frame
if (isKeyDown(keyID) == true && wasKeyDown(keyID) == false) {
return true;
}
return false;
}
bool InputManager::wasKeyDown(unsigned int keyID) {
// We dont want to use the associative array approach here
// because we don't want to create a key if it doesnt exist.
// So we do it manually
auto it = _previousKeyMap.find(keyID);
if (it != _previousKeyMap.end()) {
// Found the key
return it->second;
} else {
// Didn't find the key
return false;
}
}
}