]> git.localhorst.tv Git - blank.git/blob - src/app/IntervalTimer.hpp
some cleaning :)
[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 /// Timer that hits every n milliseconds. Resolution is that of the
8 /// delta values passed to Update(), minimum 1ms.
9 /// Also tracks the number of iterations as well as milliseconds
10 /// passed.
11 class IntervalTimer {
12
13 public:
14         /// Create a timer that hits every interval_ms milliseconds.
15         /// Initial state is stopped.
16         explicit IntervalTimer(int interval_ms) noexcept
17         : intv(interval_ms) { }
18
19         void Start() noexcept {
20                 speed = 1;
21         }
22         void Stop() noexcept {
23                 value = 0;
24                 speed = 0;
25         }
26
27         bool Running() const noexcept {
28                 return speed != 0;
29         }
30         /// true if an interval boundary was passed by the last call to Update()
31         bool Hit() const noexcept {
32                 return Running() && value % intv < last_dt;
33         }
34         int Elapsed() const noexcept {
35                 return value;
36         }
37         int Iteration() const noexcept {
38                 return value / intv;
39         }
40
41         void Update(int dt) noexcept {
42                 value += dt * speed;
43                 last_dt = dt;
44         }
45
46 private:
47         int intv;
48         int value = 0;
49         int speed = 0;
50         int last_dt = 0;
51
52 };
53
54 }
55
56 #endif