summary refs log tree commit diff stats
path: root/src/input_lag.h
blob: 40bc4a19ea9c577fa617a9c8a66a561eb6e4640e (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
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
#ifndef INPUT_LAG_H_DF16F381
#define INPUT_LAG_H_DF16F381

/**
 * Helper class that handles key repeat.
 */
class InputLag {
public:

  bool isActive() const
  {
    return active_;
  }

  void reset()
  {
    accum_ = 0.0;
    active_ = false;
    repeat_ = false;
  }

  void accumulate(double dt)
  {
    accum_ += dt;
  }

  bool step()
  {
    if (!active_)
    {
      active_ = true;

      return true;
    } else if (!repeat_)
    {
      if (accum_ > delay_)
      {
        accum_ -= delay_;
        repeat_ = true;

        return true;
      }
    } else {
      if (accum_ > tick_)
      {
        accum_ -= tick_;

        return true;
      }
    }

    return false;
  }

private:

  double tick_ = 1.0 / 15.0;
  double delay_ = tick_ * 3.0;

  double accum_ = 0.0;
  bool active_ = false;
  bool repeat_ = false;

};

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