1 #ifndef GONG_APP_INTERVALTIMER_HPP
2 #define GONG_APP_INTERVALTIMER_HPP
10 /// Timer that hits every n Time units. Resolution is that of the
11 /// delta values passed to Update().
12 /// Also tracks the number of iterations as well as Time units
14 template<class Time = int>
18 /// Create a timer that hits every interval Time units.
19 /// Initial state is stopped.
20 explicit IntervalTimer(Time interval_ms = Time(0)) noexcept
21 : intv(interval_ms) { }
23 void Start() noexcept {
26 void Stop() noexcept {
30 void Reset() noexcept {
34 bool Running() const noexcept {
35 return speed != Time(0);
37 /// true if an interval boundary was passed by the last call to Update()
38 bool Hit() const noexcept {
39 return Running() && IntervalElapsed() < last_dt;
41 bool HitOnce() const noexcept {
42 return Running() && value >= intv;
44 Time Elapsed() const noexcept {
47 Time Interval() const noexcept {
50 Time IntervalElapsed() const noexcept {
51 return mod(value, intv);
53 Time IntervalRemain() const noexcept {
54 return intv - IntervalElapsed();
56 int Iteration() const noexcept {
59 void PopIteration() noexcept {
63 void Update(Time dt) noexcept {
68 static Time mod(Time val, Time m) noexcept {
76 Time last_dt = Time(0);
80 using CoarseTimer = IntervalTimer<int>;
81 using FineTimer = IntervalTimer<float>;
84 inline float IntervalTimer<float>::mod(float val, float m) noexcept {
85 return std::fmod(val, m);
89 inline int IntervalTimer<float>::Iteration() const noexcept {
90 return std::floor(value / intv);