21 |
|
|
22 |
#include <errno.h> |
#include <errno.h> |
23 |
|
|
|
#include "base/types.h" |
|
24 |
#include "base/timer.h" |
#include "base/timer.h" |
25 |
|
|
26 |
namespace base |
namespace base |
27 |
{ |
{ |
28 |
timer::timer() : Slice(100), Lasttime(0), Frames_missed(0) |
// ctor |
29 |
|
timer::timer() : Slice(50), Lasttime(0), FramesMissed(0) |
30 |
{ |
{ |
31 |
gettimeofday(&initial_time, NULL); |
gettimeofday (&InitialTime, NULL); |
32 |
} |
} |
33 |
|
|
34 |
void timer::set_slice(unsigned long int sl) |
// set duration of one game cycle in ms |
35 |
|
void timer::set_slice(u_int32 sl) |
36 |
{ |
{ |
37 |
Slice = sl; |
Slice = sl; |
38 |
} |
} |
39 |
|
|
40 |
unsigned long int timer::current_time() const |
// return time passed since creation of timer |
41 |
|
u_int32 timer::current_time() const |
42 |
{ |
{ |
43 |
struct timeval tv; |
struct timeval tv; |
44 |
gettimeofday(&tv, NULL); |
gettimeofday(&tv, NULL); |
45 |
return convert_timeval(tv); |
return convert_timeval(tv); |
46 |
} |
} |
47 |
|
|
48 |
void timer::sleep (unsigned long int msecs) const |
// suspend application for given time |
49 |
|
void timer::sleep (u_int32 msecs) const |
50 |
{ |
{ |
51 |
unsigned char err; |
unsigned char err; |
52 |
struct timespec req, rem; |
struct timespec req, rem; |
60 |
} while (err && (errno == EINTR)); |
} while (err && (errno == EINTR)); |
61 |
} |
} |
62 |
|
|
63 |
|
// wait until current game cycle is over |
64 |
void timer::update () |
void timer::update () |
65 |
{ |
{ |
66 |
register u_int16 delay = current_time () - Lasttime; |
register u_int16 delay = current_time () - Lasttime; |
69 |
if (Slice > delay) { |
if (Slice > delay) { |
70 |
sleep (Slice - delay); |
sleep (Slice - delay); |
71 |
delay = current_time () - Lasttime; |
delay = current_time () - Lasttime; |
72 |
Frames_missed = 0; |
FramesMissed = 0; |
73 |
} |
} |
74 |
// see whether calculating the current frame took too long |
// see whether calculating the current frame took too long |
75 |
else Frames_missed = delay / Slice; |
else FramesMissed = delay / Slice; |
76 |
|
|
77 |
Lasttime += delay; |
Lasttime += delay; |
78 |
} |
} |