author | ekku |
Mon, 08 Dec 2008 15:59:33 +0000 | |
changeset 287 | f59c8dee7f91 |
parent 282 | e0e4dfc3e528 |
child 300 | 417183866f35 |
permissions | -rw-r--r-- |
205 | 1 |
|
2 |
#include "Timer.hh" |
|
3 |
||
4 |
Timer::Timer (TimeMS interval) : |
|
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
205
diff
changeset
|
5 |
interval(interval), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
205
diff
changeset
|
6 |
ticks(0), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
205
diff
changeset
|
7 |
enabled(false), |
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
205
diff
changeset
|
8 |
last_tick(0) |
205 | 9 |
{ |
10 |
||
11 |
} |
|
12 |
||
13 |
TickCount Timer::get_ticks (void) { |
|
14 |
return ticks; |
|
15 |
} |
|
16 |
||
17 |
void Timer::start (void) { |
|
18 |
last_tick = CL_System::get_time(); |
|
19 |
||
20 |
enabled = true; |
|
21 |
} |
|
22 |
||
23 |
void Timer::keep_alive (void) { |
|
24 |
// ignore if disabled |
|
25 |
if (!enabled) |
|
26 |
return; |
|
27 |
||
28 |
// get current time |
|
29 |
TimeMS now = CL_System::get_time(); |
|
30 |
||
31 |
// handle overflows |
|
32 |
if (last_tick > now) |
|
33 |
last_tick = now; |
|
34 |
||
35 |
// is the tick over? |
|
36 |
if (last_tick + interval < now) { |
|
37 |
// the length of this tick |
|
38 |
TimeMS tick_length = now - last_tick; |
|
39 |
||
40 |
// trigger our signal |
|
41 |
_sig_tick(tick_length); |
|
42 |
||
43 |
// update state |
|
44 |
last_tick = now; |
|
45 |
ticks += tick_length / interval; |
|
46 |
} |
|
47 |
} |
|
48 |