src/Timer.cc
author terom
Mon, 08 Dec 2008 01:08:00 +0000
changeset 276 87434abc1ba1
parent 205 905028e58ed1
child 282 e0e4dfc3e528
permissions -rw-r--r--
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading

#include "Timer.hh"

Timer::Timer (TimeMS interval) :
    interval(interval), ticks(0), last_tick(0), enabled(false)
{

}

TickCount Timer::get_ticks (void) {
    return ticks;
}

void Timer::start (void) {
    last_tick = CL_System::get_time();

    enabled = true;
}

void Timer::keep_alive (void) {
    // ignore if disabled
    if (!enabled)
        return;

    // get current time
    TimeMS now = CL_System::get_time();

    // handle overflows
    if (last_tick > now)
        last_tick = now;
    
    // is the tick over?
    if (last_tick + interval < now) {
        // the length of this tick
        TimeMS tick_length = now - last_tick;
        
        // trigger our signal
        _sig_tick(tick_length);
        
        // update state
        last_tick = now;
        ticks += tick_length / interval;
    }
}