summary refs log tree commit diff stats
path: root/src/input.h
blob: ad8b7617f64dd59b1a3274c7a3912c656ca3776d (plain) (blame)
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
#ifndef INPUT_H_0FB34C42
#define INPUT_H_0FB34C42

#include <SDL.h>
#include <vector>

/**
 * Helper class that detects when a key is newly pressed.
 */
class Input {
public:

  Input()
  {
    const Uint8* s = SDL_GetKeyboardState(&length_);

    curstate_.assign(s, s + length_);
    prevstate_ = curstate_;
  }

  void tick(const Uint8* keystate)
  {
    prevstate_ = std::move(curstate_);
    curstate_.assign(keystate, keystate + length_);
  }

  bool wasPressed(int scancode) const
  {
    return curstate_.at(scancode) && !prevstate_.at(scancode);
  }

private:

  int length_;
  std::vector<Uint8> curstate_;
  std::vector<Uint8> prevstate_;
};

#endif /* end of include guard: INPUT_H_0FB34C42 */