X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FTimer.h;h=a66341ac7b962b3ae358834d46d3f226db9fb953;hb=4309d259becd96ead792678257e910c03a6b4a3d;hp=465ed64e45edc14a13faa7f462b70e35275e83a1;hpb=bce16ed519add0d5398d504d2554395c43c74571;p=l2e.git diff --git a/src/app/Timer.h b/src/app/Timer.h index 465ed64..a66341a 100644 --- a/src/app/Timer.h +++ b/src/app/Timer.h @@ -1,31 +1,32 @@ -/* - * Timer.h - * - * Created on: Aug 10, 2012 - * Author: holy - */ - #ifndef APP_TIMER_H_ #define APP_TIMER_H_ #include #include +#include namespace app { +/// Stores timing information. +/// For use by app::Timer. template struct TimerData { - TimerData() : time(0), target(0), refCount(0) { } - TimerData(Time target) : time(0), target(target), refCount(0) { } + TimerData() : time(0), target(0), refCount(0), justHit(false), repeat(false) { } + TimerData(Time target, bool repeat) : time(0), target(target), refCount(0), justHit(false), repeat(repeat) { } Time time; Time target; int refCount; + bool justHit; + bool repeat; }; +/// Timer handle. +/// How the various information returned by the const member functions is to be +/// interpreted highly depends on how the timer was created (by app::Timers). template class Timer { @@ -45,18 +46,59 @@ public: } public: - bool Running() const { + /// Check if the timer was started (and not cleared) yet. + bool Started() const { return data; } + /// Check if the timer has reached its target time (only sensible for + /// countdown timers). bool Finished() const { - return data ? data->time >= data->target : false; + return data && data->target != Time() && !data->repeat && data->time >= data->target; } + /// Check if the timer was started and has not finished yet (in case it's a + /// countdown). + bool Running() const { + return data && !Finished(); + } + /// Get the elapsed time since the timer started. Time Elapsed() const { return data ? data->time : Time(); } + /// Get the time remaining for countdowns. Time Remaining() const { return data ? (data->target - data->time) : Time(); } + /// Get the iteration index for interval timers. + int Iteration() const { + return (data && data->target > Time()) ? std::floor(data->time / data->target) : Time(); + } + /// Check if the timer reached its interval or countdown goal this iteration. + bool JustHit() const { + return data && data->justHit; + } + + /// Unset the timer (does not stop other handles for a shared timer). + void Clear() { + if (data) { + --data->refCount; + data = 0; + } + } + /// Reset the timer, do not stop it if it's running. + void Reset() { + if (data) data->time = Time(); + } + /// Restart the timer. + /// Only works if the timer was started at least once. + void Restart() { + if (data) { + if (data->target > Time() && data->justHit) { + data->time -= data->target; + } else { + data->time = Time(); + } + } + } private: TimerData