[go: up one dir, main page]

Menu

[r35]: / grueworld / lin_time.cpp  Maximize  Restore  History

Download this file

176 lines (149 with data), 4.1 kB

  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
/*
* Vega Strike
* Copyright (C) 2001-2002 Daniel Horn
*
* http://vegastrike.sourceforge.net/
*
* 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 <stdlib.h>
#include "lin_time.hpp"
static double firsttime;
#ifdef _WIN32
#include <windows.h>
static LONGLONG ttime;
static LONGLONG newtime = 0;
static LONGLONG freq;
static double dblnewtime;
#else
#if defined( HAVE_SDL )
# include <SDL/SDL.h>
#endif /* defined( HAVE_SDL ) */
static double newtime;
static double lasttime;
#include <sys/time.h>
#include <sys/types.h>
#endif
static double elapsedtime=.1;
static double timecompression=1;
double getNewTime() {
#ifdef _WIN32
return dblnewtime-firsttime;
#else
return newtime-firsttime;
#endif
}
class NetClient;
int timecount;
float getTimeCompression () {
return timecompression;
}
void setTimeCompression(float tc){
timecompression=tc;
timecount=0;//to avoid any problems with time compression sounds... use getTimeCompression() instead
}
#ifdef _WIN32
#include <windows.h>
void micro_sleep(unsigned int n) {
Sleep(n / 1000);
return;
}
#elif defined(IRIX)
#include <unistd.h>
void micro_sleep(unsigned int n) {
(void) usleep((useconds_t)n);
return;
}
#else
void micro_sleep(unsigned int n) {
struct timeval tv = { 0, 0 };
tv.tv_usec = n%1000000;
tv.tv_sec=n/1000000;
select(0, NULL, NULL, NULL, &tv);
return;
}
#endif
#ifndef _WIN32
#define HAVE_GETTIMEOFDAY
#endif
void InitTime () {
#ifdef _WIN32
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)&ttime);
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
(void) gettimeofday(&tv, NULL);
newtime = (double)tv.tv_sec + (double)tv.tv_usec * 1.e-6;
lasttime = newtime -.001;
#elif defined(HAVE_SDL)
newtime = SDL_GetTicks() * 1.e-3;
lasttime = newtime -.001;
#else
# error "We have no way to determine the time on this system."
#endif
elapsedtime = .001;
}
double GetElapsedTime() {
return elapsedtime;
}
double queryTime() {
#ifdef _WIN32
LONGLONG tmpnewtime;
QueryPerformanceCounter((LARGE_INTEGER*)&tmpnewtime);
return ((double)tmpnewtime)/(double)freq-firsttime;
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
(void) gettimeofday(&tv, NULL);
double tmpnewtime = (double)tv.tv_sec + (double)tv.tv_usec * 1.e-6;
return tmpnewtime-firsttime;
#elif defined(HAVE_SDL)
double tmpnewtime = SDL_GetTicks() * 1.e-3;
return tmpnewtime-firsttime;
#else
# error "We have no way to determine the time on this system."
return 0.;
#endif
}
void UpdateTime() {
#ifdef _WIN32
QueryPerformanceCounter((LARGE_INTEGER*)&newtime);
elapsedtime = ((double)(newtime-ttime))/freq;
ttime = newtime;
if( freq==0)
dblnewtime = 0.;
else
dblnewtime = ((double)newtime)/((double)freq);
static double ftime = firsttime = dblnewtime;
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
(void) gettimeofday(&tv, NULL);
lasttime = newtime;
newtime = (double)tv.tv_sec + (double)tv.tv_usec * 1.e-6;
elapsedtime = newtime-lasttime;
static double ftime = firsttime = newtime;
#elif defined(HAVE_SDL)
lasttime = newtime;
newtime = SDL_GetTicks() * 1.e-3;
elapsedtime = newtime-lasttime;
static double ftime = firsttime = newtime;
#else
# error "We have no way to determine the time on this system."
#endif
elapsedtime *=timecompression;
}
void setNewTime(double newnewtime) {
firsttime-= newnewtime-queryTime();
UpdateTime();
}