3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2000 Manuel Moos (manuel@moosnet.de)
8
**************************************************************************
10
This program is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; either version 2
13
of the License, or (at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
***************************************************************************
31
#include "tRecorder.h"
33
// Both implementations are stolen from Q1.
38
int microseconds; //! microseconds
39
int seconds; //! seconds
41
#define NORMALIZER 1000000
43
tTime(): microseconds(0), seconds(0){}
45
//! makes sure microseconds is between 0 and NORMALIZER-1
48
int overflow = microseconds / NORMALIZER;
49
microseconds -= overflow * NORMALIZER;
52
while ( microseconds < 0 )
54
microseconds += NORMALIZER;
59
tTime operator + ( const tTime & other )
62
ret.microseconds = other.microseconds + microseconds;
63
ret.seconds = other.seconds + seconds;
69
tTime operator - ( const tTime & other )
72
ret.microseconds = -other.microseconds + microseconds;
73
ret.seconds = -other.seconds + seconds;
83
#include <sys/timeb.h>
88
void GetTime( tTime & time )
90
struct _timeb tstruct;
91
LARGE_INTEGER mtime,frq;
93
// Check if high-resolution performance counter is supported
94
if (!QueryPerformanceFrequency(&frq))
96
// Nope, not supported, do it the old way.
98
time.microseconds = tstruct.millitm*1000;
99
time.seconds = tstruct.time;
103
QueryPerformanceCounter(&mtime);
104
time.seconds = mtime.QuadPart/frq.QuadPart;
105
time.microseconds = ( ( mtime.QuadPart - time.seconds * frq.QuadPart ) * 1000000 ) / frq.QuadPart;
113
//! returns true if a timer with more than millisecond accuracy is available
114
bool tTimerIsAccurate()
117
return QueryPerformanceFrequency(&dummy);
121
void GetTime( tTime & time )
123
struct _timeb tstruct;
126
time.microseconds = tstruct.millitm*1000;
127
time.seconds = tstruct.time;
133
static double blocktime;
135
static unsigned int sleep_rest=0;
139
unsigned int r=sleep_rest/1000;
145
double tb = tSysTimeFloat();
151
double ta = tSysTimeFloat();
165
#include <sys/time.h>
167
void GetTime( tTime & time )
172
gettimeofday(&tp, &tzp);
174
time.microseconds = tp.tv_usec;
175
time.seconds = tp.tv_sec;
180
//! returns true if a timer with more than millisecond accuracy is available
181
bool tTimerIsAccurate()
183
return true; // always on unix
188
static char const * recordingSection = "T";
190
template< class Archiver > class TimeArchiver
193
static bool Archive( tTime & time )
195
// start archive block if archiving is active
197
if ( archive.Initialize( recordingSection ) )
199
archive.Archive( time.seconds ).Archive( time.microseconds );
207
static struct tTime timeStart; // the time at the start of the program
208
static struct tTime timeRelative; // the time since the system start ( eventually from a playback )
210
void tAdvanceFrameSys( tTime & start, tTime & relative )
216
if ( start.microseconds == 0 && start.seconds == 0 )
218
relative = time - start;
221
static bool s_delayedInPlayback = false;
222
void tDelay( int usecdelay )
224
// delay a bit if we're not playing back
225
if ( ! tRecorder::IsPlayingBack() )
228
s_delayedInPlayback = true;
231
void tDelayForce( int usecdelay )
234
if ( !s_delayedInPlayback )
238
// when recording, the machine was idling around. No need to play that back.
239
// Only pretend to delay.
241
timeDelay.microseconds = usecdelay;
242
timeStart = timeStart - timeDelay;
245
s_delayedInPlayback = false;
248
void tAdvanceFrame( int usecdelay )
250
// delay a bit if we're not playing back
254
tTime timeNewRelative;
255
tAdvanceFrameSys( timeStart, timeNewRelative );
257
// try to fetch time from playback
258
// tTime timeAdvance;
259
if ( TimeArchiver< tPlaybackBlock >::Archive( timeRelative ) )
261
// timeRelative = timeRelative + timeAdvance;
263
// correct start time so transition to normal time after the recording ended is smooth
264
timeStart = timeStart + timeNewRelative - timeRelative;
268
// must never be called when a recording is running
269
tASSERT( !tRecorder::IsPlayingBack() );
271
// timeAdvance = timeRealRelative - timeRelative;
272
timeRelative = timeNewRelative;
276
TimeArchiver< tRecordingBlock >::Archive( timeRelative );
279
if ( timeRelative.microseconds == 337949 && timeRelative.seconds == 25 )
287
double tSysTimeFloat ()
290
if ( ! tRecorder::IsPlayingBack() )
293
tAdvanceFrameSys( timeStart, time );
294
time = time - timeRelative;
295
// if ( time.seconds > 5 )
297
// std::cout << "tAdvanceFrame not called often enough!\n";
299
// tAdvanceFrameSys( timeRealRelative );
304
return timeRelative.seconds + timeRelative.microseconds*0.000001;
307
static struct tTime timeRealStart; // the real time at the start of the program
308
static struct tTime timeRealRelative; // the time since the system start
310
double tRealSysTimeFloat ()
312
// get real time from real OS
313
tAdvanceFrameSys( timeRealStart, timeRealRelative );
314
return timeRealRelative.seconds + timeRealRelative.microseconds*0.000001;