205
|
1 |
|
|
2 |
#include "Timer.hh"
|
|
3 |
|
|
4 |
Timer::Timer (TimeMS interval) :
|
|
5 |
interval(interval), ticks(0), last_tick(0), enabled(false)
|
|
6 |
{
|
|
7 |
|
|
8 |
}
|
|
9 |
|
|
10 |
TickCount Timer::get_ticks (void) {
|
|
11 |
return ticks;
|
|
12 |
}
|
|
13 |
|
|
14 |
void Timer::start (void) {
|
|
15 |
last_tick = CL_System::get_time();
|
|
16 |
|
|
17 |
enabled = true;
|
|
18 |
}
|
|
19 |
|
|
20 |
void Timer::keep_alive (void) {
|
|
21 |
// ignore if disabled
|
|
22 |
if (!enabled)
|
|
23 |
return;
|
|
24 |
|
|
25 |
// get current time
|
|
26 |
TimeMS now = CL_System::get_time();
|
|
27 |
|
|
28 |
// handle overflows
|
|
29 |
if (last_tick > now)
|
|
30 |
last_tick = now;
|
|
31 |
|
|
32 |
// is the tick over?
|
|
33 |
if (last_tick + interval < now) {
|
|
34 |
// the length of this tick
|
|
35 |
TimeMS tick_length = now - last_tick;
|
|
36 |
|
|
37 |
// trigger our signal
|
|
38 |
_sig_tick(tick_length);
|
|
39 |
|
|
40 |
// update state
|
|
41 |
last_tick = now;
|
|
42 |
ticks += tick_length / interval;
|
|
43 |
}
|
|
44 |
}
|
|
45 |
|