-/*] 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_
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
};
-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
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<void(timer&,scalar)> function;
-
/**
* Construct an invalid (uninitialized) timer.
*/
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
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
fire(source_->ticks());
}
-
/**
* Fire the timer event if it is expired.
* \param t The absolute time used as a reference to determine if the
* \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.
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
return is_expired(source_->ticks());
}
-
static timer_source& default_source();
/**
*/
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
*/
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);
}
{
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_;
};