forked from adamjimenez/ChaosGroove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cpp
117 lines (90 loc) · 2.23 KB
/
timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <Windows.h>
#include "timer.hpp"
// High resolution timer code for Windows. Call start_timer first and then call
// check_timer with the required accuracy range.
struct timer_t timer;
void start_timer(void)
{
timer.high_freq = QueryPerformanceFrequency(&timer.tticks);
timer.logic_frames = 0;
if (timer.high_freq)
{
QueryPerformanceCounter(&timer.tstart);
timer.tlast = timer.tstart;
}
}
void reset_timer(void)
{
if (timer.high_freq)
{
QueryPerformanceCounter(&timer.tnow);
timer.tstart = timer.tnow;
}
}
int check_timer(int frac_sec)
{
int t;
if (timer.high_freq)
{
QueryPerformanceCounter(&timer.tnow);
t = (int) (((timer.tnow.QuadPart - timer.tstart.QuadPart) * frac_sec) / timer.tticks.QuadPart);
// Have we done 1 second since the timer was last reset?
if (t > 240)
{
// Yes, so reset again and update details.
// If we reset the timer after each check we get errors building up causing a lack
// of precision. We also reset it here to make it as fast as possible.
timer.tstart = timer.tlast;
t -= timer.logic_frames;
timer.logic_fps = timer.logic_frames;
timer.logic_frames = 0;
timer.gfx_fps = timer.gfx_frames;
timer.gfx_frames = 0;
}
timer.tlast = timer.tnow;
return t;
}
return 0;
}
//#endif
/*
#if defined(ALLEGRO_MACOSX) || defined(ALLEGRO_UNIX) || defined(ALLEGRO_LINUX)
#include <sys/time.h>
static struct timeval tstart;
static struct timeval tlast;
void start_timer(void)
{
gettimeofday(&tstart, NULL);
tlast.tv_sec = tstart.tv_sec;
tlast.tv_usec = tstart.tv_usec;
}
void reset_timer(void)
{
gettimeofday(&tstart, NULL);
}
int check_timer(int frac_sec)
{
struct timeval now;
double hi, low;
int t, ms;
rest(0);
gettimeofday(&now, NULL);
hi = (double) (now.tv_sec - tstart.tv_sec);
low = ((double) (now.tv_usec - tstart.tv_usec)) / 1.0e6;
t = (int) ((hi + low) * ((double) frac_sec));
if ( t > 240 )
{
tstart.tv_usec = tlast.tv_usec;
tstart.tv_sec = tlast.tv_sec;
t -= timer.logic_frames;
timer.logic_fps = timer.logic_frames;
timer.logic_frames = 0;
timer.gfx_fps = timer.gfx_frames;
timer.gfx_frames = 0;
}
tlast.tv_usec = now.tv_usec;
tlast.tv_sec = now.tv_sec;
return t;
}
#endif
*/