1 #ifndef BLANK_APP_INTERVALTIMER_HPP
2 #define BLANK_APP_INTERVALTIMER_HPP
9 /// Timer that hits every n Time units. Resolution is that of the
10 /// delta values passed to Update().
11 /// Also tracks the number of iterations as well as Time units
13 template<class Time = int>
17 /// Create a timer that hits every interval Time units.
18 /// Initial state is stopped.
19 explicit IntervalTimer(Time interval_ms = Time(0)) noexcept
20 : intv(interval_ms) { }
22 void Start() noexcept {
25 void Stop() noexcept {
29 void Reset() noexcept {
33 bool Running() const noexcept {
34 return speed != Time(0);
36 /// true if an interval boundary was passed by the last call to Update()
37 bool Hit() const noexcept {
38 return Running() && mod(value, intv) < last_dt;
40 bool HitOnce() const noexcept {
41 return Running() && value >= intv;
43 Time Elapsed() const noexcept {
46 Time Interval() const noexcept {
49 int Iteration() const noexcept {
52 void PopIteration() noexcept {
56 void Update(Time dt) noexcept {
61 static Time mod(Time val, Time m) noexcept {
69 Time last_dt = Time(0);
73 using CoarseTimer = IntervalTimer<int>;
74 using FineTimer = IntervalTimer<float>;
77 inline float IntervalTimer<float>::mod(float val, float m) noexcept {
78 return std::fmod(val, m);
82 inline int IntervalTimer<float>::Iteration() const noexcept {
83 return std::floor(value / intv);