X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Ftimer.hpp;fp=src%2Ftimer.hpp;h=c5445b2b331a980ed750315671b91955b7fb34da;hb=83ed3de28841d1eecfca39ff540e804cf6809b32;hp=0000000000000000000000000000000000000000;hpb=bee003d1d375789b6533cfd39c65f6d4478f1966;p=blank.git diff --git a/src/timer.hpp b/src/timer.hpp new file mode 100644 index 0000000..c5445b2 --- /dev/null +++ b/src/timer.hpp @@ -0,0 +1,49 @@ +#ifndef BLANK_TIMER_HPP +#define BLANK_TIMER_HPP + + +namespace blank { + +class IntervalTimer { + +public: + explicit IntervalTimer(int interval_ms) noexcept + : intv(interval_ms) { } + + void Start() noexcept { + speed = 1; + } + void Stop() noexcept { + value = 0; + speed = 0; + } + + bool Running() const noexcept { + return speed != 0; + } + bool Hit() const noexcept { + return Running() && value % intv < last_dt; + } + int Elapsed() const noexcept { + return value; + } + int Iteration() const noexcept { + return value / intv; + } + + void Update(int dt) noexcept { + value += dt * speed; + last_dt = dt; + } + +private: + int intv; + int value = 0; + int speed = 0; + int last_dt = 0; + +}; + +} + +#endif