X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.hh;fp=src%2Fmoof%2Ftimer.hh;h=edd1de2deaf2cab1a98eff1502dffb0802efed86;hp=f9545eb4f4149810cbf78bc7b605f3948c24c17f;hb=574af38ed616d1adfa5e6ce35f67cda1f707f89d;hpb=6c9943707d4f33035830eba0587a61a34eaecbc2 diff --git a/src/moof/timer.hh b/src/moof/timer.hh index f9545eb..edd1de2 100644 --- a/src/moof/timer.hh +++ b/src/moof/timer.hh @@ -1,13 +1,11 @@ -/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +/*] Copyright (c) 2009-2011, Charles McGarvey [***************************** **] All rights reserved. * -* vi:ts=4 sw=4 tw=75 -* * Distributable under the terms and conditions of the 2-clause BSD license; * see the file COPYING for a complete text of the license. * -**************************************************************************/ +*****************************************************************************/ #ifndef _MOOF_TIMER_HH_ #define _MOOF_TIMER_HH_ @@ -28,6 +26,9 @@ namespace moof { +// forward declarations +class runloop; + /** * A timer source is an object that keeps track of increasing time in units * of seconds. A timer source does not necessarily have to increment at @@ -62,9 +63,6 @@ public: }; -class runloop; - - /** * A class to represent a timer for scheduled events. The timer events * will be executed on or sometime after their schedculed time. The @@ -83,18 +81,17 @@ public: invalid = 0, /// Timer is not scheduled. relative = 1, /// Timer is scheduled by a relative time. absolute = 2, /// Timer is scheduled by an absolute time. - repeat = 3 /// Timer is scheduled by a periodic time. + repeat = 3 /// Timer is scheduled by a periodic time. }; /** * Function protocol for a timer event handler. A function matching * this protocol will be called when the timer expires. The function * takes two parameters: the timer object that just expired, and the - * absolute time. + * absolute time of the time source which caused the timer to expire. */ typedef boost::function function; - /** * Construct an invalid (uninitialized) timer. */ @@ -127,14 +124,12 @@ public: init(function, seconds, mode, source); } - /** * Deconstruct a timer. This will automagically invalidate the timer, * so it will not expire or fire an event. */ ~timer(); - /** * Initialize a timer with a scheduled time. If the timer is already * scheduled, its prior schedule will be invalidated and replaced by @@ -157,20 +152,17 @@ public: enum mode mode = relative, timer_source& source = default_source()); - /** * Manually invalidated the timer, removing any schedule such that the * timer will not expired and no event will be fired. */ void invalidate(); - enum mode mode() const { return mode_; } - /** * Manually fire the timer event. Usually, the timer event will be * fired when the timer expires, but this can be used to fire it @@ -185,7 +177,6 @@ public: fire(source_->ticks()); } - /** * Fire the timer event if it is expired. * \param t The absolute time used as a reference to determine if the @@ -193,17 +184,20 @@ public: * \return The absolute time of the next expiration (if repeating), or * 0.0 otherwise. */ - scalar fire_if_expired(scalar t) + bool fire_if_expired(scalar t) { - if (is_expired(t)) fire(t); - return absolute_; + if (is_expired(t)) + { + fire(t); + return true; + } + return false; } - scalar fire_if_expired() + bool fire_if_expired() { return fire_if_expired(source_->ticks()); } - /** * Get the absolute time of the next expiration of this timer. * \return Seconds. @@ -230,7 +224,6 @@ public: return remaining(source_->ticks()); } - /** * Get whether or not the timer is expired. A timer on a repeating * schedule will never be expired since it will always have a scheduled @@ -250,7 +243,6 @@ public: return is_expired(source_->ticks()); } - static timer_source& default_source(); /** @@ -260,7 +252,6 @@ public: */ static scalar ticks(); - /** * Put the thread to sleep for a certain period of time. If absolute * is true, then it will sleep until the default timer reaches the @@ -274,30 +265,27 @@ public: */ static void sleep(scalar seconds, enum mode mode = relative); - private: void added_to_runloop(runloop& runloop); void detach_from_runloop(); - - function function_; - enum mode mode_; - scalar absolute_; - scalar interval_; + function function_; + enum mode mode_; + scalar absolute_; + scalar interval_; timer_source* source_; - runloop* runloop_; + runloop* runloop_; }; - - class game_time : public timer_source { public: game_time(scalar timestep = SCALAR(1.0)) : - scale_(timestep) + scale_(timestep), + scalefactor_(SCALAR(1.0)) { reset(timestep); } @@ -316,29 +304,30 @@ public: { reset(); timestep_ = timestep; + scale_ = timestep_ * scalefactor_; } void scale(scalar factor) { reference_ = ticks(); - ticks_ = 1; + ticks_ = 0; scale_ = timestep_ * factor; + scalefactor_ = factor; } - - unsigned step(unsigned step = 1) + scalar step(unsigned step = 1) { ticks_ += step; - return ticks_; + return scale_; } - private: scalar reference_; unsigned ticks_; scalar timestep_; scalar scale_; + scalar scalefactor_; };