summary refs log tree commit diff stats
path: root/src/input.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/input.h')
-rw-r--r--src/input.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..ad8b761 --- /dev/null +++ b/src/input.h
@@ -0,0 +1,39 @@
1#ifndef INPUT_H_0FB34C42
2#define INPUT_H_0FB34C42
3
4#include <SDL.h>
5#include <vector>
6
7/**
8 * Helper class that detects when a key is newly pressed.
9 */
10class Input {
11public:
12
13 Input()
14 {
15 const Uint8* s = SDL_GetKeyboardState(&length_);
16
17 curstate_.assign(s, s + length_);
18 prevstate_ = curstate_;
19 }
20
21 void tick(const Uint8* keystate)
22 {
23 prevstate_ = std::move(curstate_);
24 curstate_.assign(keystate, keystate + length_);
25 }
26
27 bool wasPressed(int scancode) const
28 {
29 return curstate_.at(scancode) && !prevstate_.at(scancode);
30 }
31
32private:
33
34 int length_;
35 std::vector<Uint8> curstate_;
36 std::vector<Uint8> prevstate_;
37};
38
39#endif /* end of include guard: INPUT_H_0FB34C42 */