X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FTimer.h;fp=src%2Fapp%2FTimer.h;h=05d295ee77d01cd21304edc660f70293131b3fd2;hb=ede708d4e15a34a4443727fc64fd28946fcbeb41;hp=13f48f3d7d38ec884c38234fc4fdc7355e1f990c;hpb=1014598c02e9635d9d4a008010d43c3c0b845e06;p=l2e.git diff --git a/src/app/Timer.h b/src/app/Timer.h index 13f48f3..05d295e 100644 --- a/src/app/Timer.h +++ b/src/app/Timer.h @@ -14,6 +14,8 @@ namespace app { +/// Stores timing information. +/// For use by app::Timer. template struct TimerData { @@ -29,6 +31,9 @@ struct TimerData { }; +/// 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 { @@ -48,37 +53,50 @@ public: } public: + /// 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->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) { @@ -95,6 +113,7 @@ private: }; +/// Tracker for timers, responsible for creating and updating them. template class Timers { @@ -102,6 +121,7 @@ public: Timers() { } public: + /// Move all timers forward by delta. void Update(Time delta) { for (typename std::list >::iterator i(data.begin()), end(data.end()); i != end;) { if (i->target > 0) { @@ -117,14 +137,19 @@ public: } } } + /// Start a timer that counts elapsed time until stopped manually. Timer