diff options
Diffstat (limited to 'src/input_lag.h')
| -rw-r--r-- | src/input_lag.h | 66 |
1 files changed, 66 insertions, 0 deletions
| diff --git a/src/input_lag.h b/src/input_lag.h new file mode 100644 index 0000000..40bc4a1 --- /dev/null +++ b/src/input_lag.h | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | #ifndef INPUT_LAG_H_DF16F381 | ||
| 2 | #define INPUT_LAG_H_DF16F381 | ||
| 3 | |||
| 4 | /** | ||
| 5 | * Helper class that handles key repeat. | ||
| 6 | */ | ||
| 7 | class InputLag { | ||
| 8 | public: | ||
| 9 | |||
| 10 | bool isActive() const | ||
| 11 | { | ||
| 12 | return active_; | ||
| 13 | } | ||
| 14 | |||
| 15 | void reset() | ||
| 16 | { | ||
| 17 | accum_ = 0.0; | ||
| 18 | active_ = false; | ||
| 19 | repeat_ = false; | ||
| 20 | } | ||
| 21 | |||
| 22 | void accumulate(double dt) | ||
| 23 | { | ||
| 24 | accum_ += dt; | ||
| 25 | } | ||
| 26 | |||
| 27 | bool step() | ||
| 28 | { | ||
| 29 | if (!active_) | ||
| 30 | { | ||
| 31 | active_ = true; | ||
| 32 | |||
| 33 | return true; | ||
| 34 | } else if (!repeat_) | ||
| 35 | { | ||
| 36 | if (accum_ > delay_) | ||
| 37 | { | ||
| 38 | accum_ -= delay_; | ||
| 39 | repeat_ = true; | ||
| 40 | |||
| 41 | return true; | ||
| 42 | } | ||
| 43 | } else { | ||
| 44 | if (accum_ > tick_) | ||
| 45 | { | ||
| 46 | accum_ -= tick_; | ||
| 47 | |||
| 48 | return true; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return false; | ||
| 53 | } | ||
| 54 | |||
| 55 | private: | ||
| 56 | |||
| 57 | double tick_ = 1.0 / 15.0; | ||
| 58 | double delay_ = tick_ * 3.0; | ||
| 59 | |||
| 60 | double accum_ = 0.0; | ||
| 61 | bool active_ = false; | ||
| 62 | bool repeat_ = false; | ||
| 63 | |||
| 64 | }; | ||
| 65 | |||
| 66 | #endif /* end of include guard: INPUT_LAG_H_DF16F381 */ | ||
