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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* PARSEC - Frame Timing
*
* $Author: uberlinuxguy $ - $Date: 2004/09/15 12:25:42 $
*
* Orginally written by:
* Copyright (c) Markus Hadwiger <msh@parsec.org> 1998-2000
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#ifndef SYSTEM_TARGET_WINDOWS
// C library includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// time functions
#include <sys/time.h>
#include <unistd.h>
// compilation flags/debug support
#include "debug.h"
// general definitions
#include "general.h"
#include "objstruc.h"
// global externals
#include "globals.h"
// subsystem headers
#ifdef PARSEC_CLIENT
#include "aud_defs.h"
#include "inp_defs.h"
#endif //PARSEC_CLIENT
#include "sys_defs.h"
// local module header
#include "sl_timer.h"
// global variables for frame timing ------------------------------------------
//
volatile int FrameRate;
volatile int FrameCounter;
volatile int RefTimeCount;
volatile int RefFrameCount;
// static timer vars ----------------------------------------------------------
//
static refframe_t sl_timfreqsec;
static hprec_t sl_timfreqfac;
static int sl_timsecbase;
static int sl_frmsecbase;
// reference frame count pausing ----------------------------------------------
//
static int refframe_count_paused = FALSE;
static refframe_t refframe_count_pauseval = REFFRAME_INVALID;
static refframe_t refframe_count_pauseofs = 0;
// base frequency of gettimeofday() is 1MHz -----------------------------------
//
#define BASE_FREQ 1000000.0
// init frame timing ----------------------------------------------------------
//
int SYSs_InitFrameTimer()
{
int static init_done = FALSE;
if ( init_done ) {
// get high-precision time
struct timeval time_timeval;
struct timezone time_timezone;
gettimeofday( &time_timeval, &time_timezone );
// calc refframe count for old frequency
time_t secframes = ( time_timeval.tv_sec - sl_timsecbase ) * sl_timfreqsec;
refframe_t refframecount = refframe_t( time_timeval.tv_usec * sl_timfreqfac + secframes );
// calc correction factor for new frequency
sl_timfreqsec = FRAME_MEASURE_TIMEBASE;
sl_timfreqfac = FRAME_MEASURE_TIMEBASE / BASE_FREQ;
// calc offset to ensure monotonically increasing refframecount
// even though we now use the new frequency
secframes = ( time_timeval.tv_sec - sl_timsecbase ) * sl_timfreqsec;
RefFrameCount = refframe_t( time_timeval.tv_usec * sl_timfreqfac + secframes );
refframe_count_pauseofs += RefFrameCount - refframecount;
} else {
// calculate factor to adjust to our time quantum
sl_timfreqsec = FRAME_MEASURE_TIMEBASE;
sl_timfreqfac = FRAME_MEASURE_TIMEBASE / BASE_FREQ;
// set initial count
SYSs_InitRefFrameCount();
init_done = TRUE;
}
return TRUE;
}
// deinit frame timing --------------------------------------------------------
//
int SYSs_KillFrameTimer()
{
// not needed
return 1;
}
// init reference frame counting ----------------------------------------------
//
void SYSs_InitRefFrameCount()
{
// secbase for frame rate measurement not set
sl_frmsecbase = -1;
// get high-precision time
struct timeval time_timeval;
struct timezone time_timezone;
gettimeofday( &time_timeval, &time_timezone );
// set base in seconds
sl_timsecbase = (int) time_timeval.tv_sec;
// calc number of elapsed reference frames
int secframes = int(( time_timeval.tv_sec - sl_timsecbase ) * sl_timfreqsec );
RefFrameCount = (int)( time_timeval.tv_usec * sl_timfreqfac ) + secframes;
// init refframe count pausing/offset
refframe_count_paused = FALSE;
refframe_count_pauseval = REFFRAME_INVALID;
refframe_count_pauseofs = 0;
}
// get count of current reference frame ---------------------------------------
//
refframe_t SYSs_GetRefFrameCount()
{
// get high-precision time
struct timeval time_timeval;
struct timezone time_timezone;
gettimeofday( &time_timeval, &time_timezone );
// calc number of elapsed reference frames
int secframes = int(( time_timeval.tv_sec - sl_timsecbase ) * sl_timfreqsec );
RefFrameCount = (int)( time_timeval.tv_usec * sl_timfreqfac ) + secframes;
return RefFrameCount - refframe_count_pauseofs;
}
// pause reference frame counting ---------------------------------------------
//
void SYSs_PauseRefFrameCount()
{
if ( !refframe_count_paused ) {
SYSs_GetRefFrameCount();
refframe_count_pauseval = RefFrameCount;
refframe_count_paused = TRUE;
}
}
// resume reference frame counting --------------------------------------------
//
void SYSs_ResumeRefFrameCount()
{
if ( refframe_count_paused ) {
SYSs_GetRefFrameCount();
refframe_count_pauseofs += RefFrameCount - refframe_count_pauseval;
refframe_count_pauseval = REFFRAME_INVALID;
refframe_count_paused = FALSE;
}
}
// wait for specified number of reference frames ------------------------------
//
void SYSs_Wait( refframe_t refframes )
{
int basecount = SYSs_GetRefFrameCount();
while ( SYSs_GetRefFrameCount() - basecount < refframes ) {
SYSs_Yield();
}
}
// use yield function to calculate frame rate ---------------------------------
//
int SYSs_Yield()
{
#ifdef PARSEC_CLIENT
// yield to sound driver
AUDs_MaintainSound();
// must be called to retrieve key strokes from buffer
INPs_Collect();
#endif // PARSEC_CLIENT
// get high-precision time
struct timeval time_timeval;
struct timezone time_timezone;
gettimeofday( &time_timeval, &time_timezone );
// set base if not done yet
if ( sl_frmsecbase == -1 ) {
sl_frmsecbase = time_timeval.tv_sec;
}
// flush framecounter every second
if ( ( time_timeval.tv_sec - sl_frmsecbase ) > 0 ) {
sl_frmsecbase = time_timeval.tv_sec;
FrameRate = FrameCounter;
FrameCounter = 0;
}
return 1;
}
#ifdef PARSEC_SERVER
REGISTER_MODULE( SL_TIMER )
{
SYSs_InitFrameTimer();
}
#endif
#endif // !SYSTEM_TARGET_WINDOWS
|