terom@205: #ifndef TIMER_HH terom@205: #define TIMER_HH terom@205: terom@205: #include terom@205: terom@300: #include "Types.hh" terom@205: terom@283: /** terom@283: * Used to implement Physics ticks and Graphics frames. This will attempt to trigger sig_tick every ms, terom@283: * but if we miss some ticks or something else, then the tick_length passed to sig_tick will be longer than interval... terom@283: */ terom@205: class Timer : public CL_KeepAlive { terom@205: protected: terom@300: /** terom@300: * The target tick interval terom@300: */ terom@205: TimeMS interval; terom@205: terom@300: /** terom@300: * Number of ticks so far, updated by keep_alive terom@300: */ terom@205: TickCount ticks; terom@300: terom@300: /** terom@300: * Is the timer running? terom@300: */ terom@205: bool enabled; terom@300: terom@300: /** terom@300: * Single-shot mode? terom@300: */ terom@300: bool single_shot; terom@205: terom@300: /** terom@300: * Time of last tick, set to current time by start/fire_once/keep_alive terom@300: */ terom@205: TimeMS last_tick; terom@300: terom@300: /** The tick signal */ terom@205: CL_Signal_v1 _sig_tick; terom@205: terom@205: public: terom@300: /** terom@300: * Prepare a timer, sig_tick will be fired every interval ms after the timer is started. terom@300: * terom@300: * @param interval interval in milliseconds terom@205: */ terom@205: Timer (TimeMS interval); terom@205: terom@205: /* terom@205: * Returns the tick counter terom@205: */ terom@205: TickCount get_ticks (void); terom@205: terom@300: /** terom@300: * Start the timer in continuous-call mode, this should be called once keepalive starts getting called. terom@205: */ terom@205: void start (void); terom@300: terom@300: /** terom@300: * Tick the timer, but only once terom@300: */ terom@300: void fire_once (void); terom@205: terom@205: private: terom@300: /** terom@300: * If interval time has elapsed, fire sig_tick terom@300: */ terom@205: void keep_alive (void); terom@205: terom@205: public: terom@300: /** terom@300: * Triggered approximately every interval MS, but the given interval may also be longer if we've skipped ticks terom@300: */ terom@205: CL_Signal_v1& sig_tick (void) { return _sig_tick; } terom@205: }; terom@205: terom@205: #endif