]>
Dogcows Code - chaz/yoink/blob - src/moof/timer.cc
c45e1b8909eb6413e79b9b14a372fca4b0da744c
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
28 void timer::init(const function
& function
,
36 if ((mode_
= mode
) != invalid
)
46 absolute_
= seconds
+ source_
->ticks();
54 detach_from_runloop();
57 void timer::invalidate()
60 absolute_
= SCALAR(0.0);
64 void timer::added_to_runloop(runloop
& runloop
)
66 detach_from_runloop();
70 void timer::detach_from_runloop()
74 runloop_
->remove_timer(*this);
80 void timer::fire(scalar t
)
82 if (function_
) function_(*this, t
);
86 if (is_equal(absolute_
, t
, 1.0)) absolute_
+= interval_
;
87 else absolute_
= interval_
+ t
;
95 return default_source().ticks();
100 #if ENABLE_CLOCK_GETTIME
102 class real_time
: public timer_source
116 int result
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
117 ASSERT(result
== 0 && "monotonic clock not available");
120 (scalar(ts
.tv_sec
- start_
.tv_sec
) +
121 scalar(ts
.tv_nsec
- start_
.tv_nsec
) *
122 SCALAR(0.000000001)) * scale_
;
127 reference_
= SCALAR(0.0);
128 clock_gettime(CLOCK_MONOTONIC
, &start_
);
131 void scale(scalar factor
)
133 reference_
= ticks();
134 clock_gettime(CLOCK_MONOTONIC
, &start_
);
142 struct timespec start_
;
147 void timer::sleep(scalar seconds
, enum mode mode
)
149 if (mode
== absolute
) seconds
-= ticks();
150 if (seconds
< SCALAR(0.0)) return;
154 ts
.tv_nsec
= (seconds
- scalar(ts
.tv_sec
)) * SCALAR(1000000000.0);
157 do ret
= nanosleep(&ts
, &ts
); while (ret
== -1 && errno
== EINTR
);
161 #else // ! ENABLE_CLOCK_GETTIME
164 class real_time
: public timer_source
177 return reference_
+ scalar(SDL_GetTicks() - start_
) * scale_
;
182 reference_
= SCALAR(0.0);
183 start_
= SDL_GetTicks();
186 void scale(scalar factor
)
188 reference_
= ticks();
189 start_
= SDL_GetTicks();
190 scale_
= factor
* SCALAR(0.001);
202 void timer::sleep(scalar seconds
, mode mode
)
204 if (mode
== absolute
) seconds
-= ticks();
205 if (seconds
< SCALAR(0.0)) return;
206 SDL_Delay(seconds
* SCALAR(1000.0));
209 #endif // ENABLE_CLOCK_GETTIME
212 timer_source
& timer::default_source()
This page took 0.038597 seconds and 4 git commands to generate.