X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FTimer.h;h=d744fe09966cc203e76982695a1824e05946ed14;hb=c33724dc441c5b21a4eb84aa0105c95e82a66adc;hp=0a085418739be44a53c24e83281de511d201a394;hpb=049f304d41e9aa8b5821e298fcb68c1b9a7e97d6;p=l2e.git diff --git a/src/app/Timer.h b/src/app/Timer.h index 0a08541..d744fe0 100644 --- a/src/app/Timer.h +++ b/src/app/Timer.h @@ -1,10 +1,3 @@ -/* - * Timer.h - * - * Created on: Aug 10, 2012 - * Author: holy - */ - #ifndef APP_TIMER_H_ #define APP_TIMER_H_ @@ -14,20 +7,26 @@ namespace app { +/// Stores timing information. +/// For use by app::Timer. template struct TimerData { - TimerData() : time(0), target(0), refCount(0), justHit(false) { } - TimerData(Time target) : time(0), target(target), refCount(0), justHit(false) { } + 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 { @@ -47,34 +46,68 @@ 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(); } + /// Time from start to hit. + Time TargetTime() const { + return data ? data->target : Time(); + } + /// Get the elapsed time since the timer started or last hit. + Time IterationElapsed() const { + return data ? data->time % data->target : Time(); + } + /// Get the remaining time in this iteration. + Time IterationRemaining() const { + return TargetTime() - IterationElapsed(); + } + /// Get the iteration index for interval timers. int Iteration() const { - return (data && data->target > 0) ? std::floor(data->time / data->target) : 0; + 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 = 0; + 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 > 0 && data->justHit) { + if (data->target > Time() && data->justHit) { data->time -= data->target; } else { - data->time = 0; + data->time = Time(); } } } @@ -85,6 +118,7 @@ private: }; +/// Tracker for timers, responsible for creating and updating them. template class Timers { @@ -92,11 +126,12 @@ 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) { Time intervalTime(i->time); - while (intervalTime > i->target) intervalTime -= i->target; + if (i->repeat) while (intervalTime > i->target) intervalTime -= i->target; i->justHit = intervalTime < i->target && intervalTime + delta >= i->target; } i->time += delta; @@ -107,12 +142,21 @@ public: } } } + /// Start a timer that counts elapsed time until stopped manually. Timer