X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.hh;h=a429ba955e0206158814ce120661ef54974b6e09;hp=9f3e9846096d7e09600a053ea929c66e7af91d19;hb=HEAD;hpb=af88821a172c4dfd138b91b2a5148ae50b502fa2 diff --git a/src/moof/timer.hh b/src/moof/timer.hh index 9f3e984..a429ba9 100644 --- a/src/moof/timer.hh +++ b/src/moof/timer.hh @@ -1,22 +1,15 @@ -/*] 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_ -/** - * \file timer.hh - * Functions for measuring time in a friendly unit. - */ - #include #include #include @@ -25,9 +18,17 @@ #include +/** + * \file timer.hh + * Functions for measuring time in a friendly unit. + */ + 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,33 +265,29 @@ 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) : - timestep_(timestep), - scale_(timestep) + game_time(scalar timestep = SCALAR(1.0)) : + scale_(timestep), + scalefactor_(SCALAR(1.0)) { - reset(); + reset(timestep); } scalar ticks() const @@ -313,28 +300,34 @@ public: reference_ = SCALAR(0.0); ticks_ = 0; } + void reset(scalar timestep) + { + 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_; };