forked from a544jh/panel-pop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cpp
More file actions
62 lines (53 loc) · 1.08 KB
/
InputManager.cpp
File metadata and controls
62 lines (53 loc) · 1.08 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
/*
* InputManager.cpp
*
* Created on: Aug 19, 2015
* Author: axel
*/
#include "InputManager.h"
#include <SDL2/SDL.h>
InputManager::InputManager() :
_quit(false) {
}
InputManager& InputManager::getInstance() {
static InputManager instance;
return instance;
}
void InputManager::poll() {
_prevKeys = _keys;
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
_quit = true;
} else if (e.type == SDL_KEYDOWN) {
_keys[e.key.keysym.scancode] = true;
} else if (e.type == SDL_KEYUP) {
_keys[e.key.keysym.scancode] = false;
}
}
}
bool InputManager::keyDown(int key) {
return !_prevKeys[key] && _keys[key];
}
bool InputManager::keyUp(int key) {
return _prevKeys[key] && !_keys[key];
}
bool InputManager::keyPressed(int key) {
return _keys[key];
}
bool InputManager::anyKeyDown() {
for (int i = 0; i < KEYBOARD_SIZE; ++i) {
if (!_prevKeys[i] && _keys[i]) {
return true;
}
}
return false;
}
int InputManager::getKeyDown() {
for (int i = 0; i < KEYBOARD_SIZE; ++i) {
if (!_prevKeys[i] && _keys[i]) {
return i;
}
}
return 0;
}