terom@205: terom@205: #include "Timer.hh" terom@205: terom@205: Timer::Timer (TimeMS interval) : terom@282: interval(interval), terom@282: ticks(0), terom@282: enabled(false), terom@282: last_tick(0) terom@205: { terom@205: terom@205: } terom@205: terom@205: TickCount Timer::get_ticks (void) { terom@205: return ticks; terom@205: } terom@205: terom@205: void Timer::start (void) { terom@205: last_tick = CL_System::get_time(); terom@205: terom@205: enabled = true; terom@300: single_shot = false; terom@300: } terom@300: terom@300: void Timer::fire_once (void) { terom@300: last_tick = CL_System::get_time(); terom@300: terom@300: enabled = true; terom@300: single_shot = true; terom@205: } terom@205: terom@205: void Timer::keep_alive (void) { terom@205: // ignore if disabled terom@205: if (!enabled) terom@205: return; terom@205: terom@205: // get current time terom@205: TimeMS now = CL_System::get_time(); terom@205: terom@205: // handle overflows terom@205: if (last_tick > now) terom@205: last_tick = now; terom@205: terom@205: // is the tick over? terom@205: if (last_tick + interval < now) { terom@205: // the length of this tick terom@205: TimeMS tick_length = now - last_tick; terom@205: terom@205: // trigger our signal terom@205: _sig_tick(tick_length); terom@205: terom@205: // update state terom@205: last_tick = now; terom@205: ticks += tick_length / interval; terom@300: terom@300: // if it was single-shot, disable terom@300: if (single_shot) terom@300: enabled = false; terom@205: } terom@205: } terom@205: