src/Timer.hh
author nireco
Sat, 31 Jan 2009 12:33:08 +0200
changeset 443 5d1119729f58
parent 300 417183866f35
permissions -rw-r--r--
worm02 two pics to comment
#ifndef TIMER_HH
#define TIMER_HH

#include <ClanLib/core.h>

#include "Types.hh"

/**
 * Used to implement Physics ticks and Graphics frames. This will attempt to trigger sig_tick every <interval> ms,
 * but if we miss some ticks or something else, then the tick_length passed to sig_tick will be longer than interval...
 */
class Timer : public CL_KeepAlive {
    protected:
        /**
         * The target tick interval
         */
        TimeMS interval;

        /**
         * Number of ticks so far, updated by keep_alive
         */
        TickCount ticks;
        
        /**
         * Is the timer running?
         */
        bool enabled;
        
        /**
         * Single-shot mode?
         */
        bool single_shot;

        /**
         * Time of last tick, set to current time by start/fire_once/keep_alive
         */
        TimeMS last_tick;
        
        /** The tick signal */
        CL_Signal_v1<TimeMS> _sig_tick;
        
    public:
        /**
         * Prepare a timer, sig_tick will be fired every interval ms after the timer is started.
         *
         * @param interval interval in milliseconds
         */
        Timer (TimeMS interval);
        
        /*
         * Returns the tick counter
         */
        TickCount get_ticks (void);
        
        /**
         * Start the timer in continuous-call mode, this should be called once keepalive starts getting called.
         */
        void start (void);

        /**
         * Tick the timer, but only once
         */
        void fire_once (void);
        
    private:
        /**
         * If interval time has elapsed, fire sig_tick
         */
        void keep_alive (void);
    
    public:
        /**
         * Triggered approximately every interval MS, but the given interval may also be longer if we've skipped ticks
         */
        CL_Signal_v1<TimeMS>& sig_tick (void) { return _sig_tick; }
};

#endif