]> git.localhorst.tv Git - blank.git/blob - src/app/IntervalTimer.hpp
some code reorganization
[blank.git] / src / app / IntervalTimer.hpp
1 #ifndef BLANK_APP_INTERVALTIMER_HPP
2 #define BLANK_APP_INTERVALTIMER_HPP
3
4
5 namespace blank {
6
7 class IntervalTimer {
8
9 public:
10         explicit IntervalTimer(int interval_ms) noexcept
11         : intv(interval_ms) { }
12
13         void Start() noexcept {
14                 speed = 1;
15         }
16         void Stop() noexcept {
17                 value = 0;
18                 speed = 0;
19         }
20
21         bool Running() const noexcept {
22                 return speed != 0;
23         }
24         bool Hit() const noexcept {
25                 return Running() && value % intv < last_dt;
26         }
27         int Elapsed() const noexcept {
28                 return value;
29         }
30         int Iteration() const noexcept {
31                 return value / intv;
32         }
33
34         void Update(int dt) noexcept {
35                 value += dt * speed;
36                 last_dt = dt;
37         }
38
39 private:
40         int intv;
41         int value = 0;
42         int speed = 0;
43         int last_dt = 0;
44
45 };
46
47 }
48
49 #endif