author | nireco |
Sat, 31 Jan 2009 15:21:57 +0200 | |
changeset 445 | 94dfb47036ef |
parent 423 | 947ab54de4b7 |
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; |
|
300 | 21 |
single_shot = false; |
22 |
} |
|
23 |
||
24 |
void Timer::fire_once (void) { |
|
25 |
last_tick = CL_System::get_time(); |
|
26 |
||
27 |
enabled = true; |
|
28 |
single_shot = true; |
|
205 | 29 |
} |
30 |
||
31 |
void Timer::keep_alive (void) { |
|
32 |
// ignore if disabled |
|
33 |
if (!enabled) |
|
34 |
return; |
|
35 |
||
36 |
// get current time |
|
37 |
TimeMS now = CL_System::get_time(); |
|
38 |
||
39 |
// handle overflows |
|
40 |
if (last_tick > now) |
|
41 |
last_tick = now; |
|
42 |
||
43 |
// is the tick over? |
|
44 |
if (last_tick + interval < now) { |
|
45 |
// the length of this tick |
|
46 |
TimeMS tick_length = now - last_tick; |
|
47 |
||
48 |
// trigger our signal |
|
49 |
_sig_tick(tick_length); |
|
50 |
||
51 |
// update state |
|
52 |
last_tick = now; |
|
423
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
300
diff
changeset
|
53 |
|
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
300
diff
changeset
|
54 |
// XXX: handle overflow |
947ab54de4b7
fix worst compilation errors when using the debian lenny gcc/libc...
Tero Marttila <terom@fixme.fi>
parents:
300
diff
changeset
|
55 |
ticks += (TickCount) tick_length / interval; |
300 | 56 |
|
57 |
// if it was single-shot, disable |
|
58 |
if (single_shot) |
|
59 |
enabled = false; |
|
205 | 60 |
} |
61 |
} |
|
62 |