vegastrike  0.5.1.r1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lin_time.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include "vegastrike.h"
23 #include "in_kb.h"
24 #include "vs_random.h"
25 static double firsttime;
26 VSRandom vsrandom( time( NULL ) );
27 
28 #ifdef WIN32
29 #ifndef NOMINMAX
30 #define NOMINMAX
31 #endif //tells VCC not to generate min/max macros
32 #include <windows.h>
33 static LONGLONG ttime;
34 static LONGLONG newtime = 0;
35 static LONGLONG freq;
36 static double dblnewtime;
37 #else
38 #if defined (HAVE_SDL)
39 # include <SDL/SDL.h>
40 #endif /* defined( HAVE_SDL ) */
41 static double newtime;
42 static double lasttime;
43 
44 #include <sys/time.h>
45 #include <sys/types.h>
46 #endif
47 static double elapsedtime = .1;
48 static double timecompression = 1;
49 
50 double getNewTime()
51 {
52 #ifdef _WIN32
53  return dblnewtime-firsttime;
54 #else
55  return newtime-firsttime;
56 #endif
57 }
58 
59 class NetClient;
60 
61 int timecount;
62 
64 {
65  if (a == PRESS) {
66  timecompression *= 1.5;
67  timecount++;
68  }
69 }
70 
72 {
73  if (a == PRESS) {
74  timecompression /= 1.5;
75  timecount--;
76  }
77 }
78 
80 {
81  if (a == PRESS) {
82  timecompression = 1;
83  timecount = 0;
84  }
85 }
86 
87 void pause_key( const KBData &s, KBSTATE a )
88 {
89  static bool paused = false;
90  if (a == PRESS) {
91  if (paused == false) {
92  timecompression = .0000001;
93  timecount = 0;
94  paused = true;
95  } else {
96  paused = false;
97  reset_time_compression( s, a );
98  }
99  }
100 }
101 
103 {
104  return timecompression;
105 }
106 
107 void setTimeCompression( float tc )
108 {
109  timecompression = tc;
110  timecount = 0; //to avoid any problems with time compression sounds... use getTimeCompression() instead
111 }
112 
114 {
115  static bool paused = false;
116  if (paused)
117  {
119  paused = false;
120  }
121  else
122  {
123  setTimeCompression(.0000001);
124  paused = true;
125  }
126  return paused;
127 }
128 
129 #ifdef _WIN32
130 
131 #ifndef NOMINMAX
132 #define NOMINMAX
133 #endif //tells VCC not to generate min/max macros
134 
135 #include <windows.h>
136 
137 void micro_sleep( unsigned int n )
138 {
139  Sleep( n/1000 );
140 }
141 
142 #elif defined (IRIX)
143 
144 #include <unistd.h>
145 
146 void micro_sleep( unsigned int n )
147 {
148  (void) usleep( (useconds_t) n );
149 }
150 
151 #else
152 
153 void micro_sleep( unsigned int n )
154 {
155  struct timeval tv = {
156  0, 0
157  };
158 
159  tv.tv_usec = n%1000000;
160  tv.tv_sec = n/1000000;
161  select( 0, NULL, NULL, NULL, &tv );
162 }
163 #endif
164 
165 void InitTime()
166 {
167 #ifdef WIN32
168  QueryPerformanceFrequency( (LARGE_INTEGER*) &freq );
169  QueryPerformanceCounter( (LARGE_INTEGER*) &ttime );
170 
171 #elif defined (HAVE_GETTIMEOFDAY)
172  struct timeval tv;
173  (void) gettimeofday( &tv, NULL );
174 
175  newtime = (double) tv.tv_sec+(double) tv.tv_usec*1.e-6;
176  lasttime = newtime-.001;
177 
178 #elif defined (HAVE_SDL)
179  newtime = SDL_GetTicks()*1.e-3;
180  lasttime = newtime-.001;
181 
182 #else
183 # error "We have no way to determine the time on this system."
184 #endif
185  elapsedtime = .001;
186 }
187 
189 {
190  return elapsedtime;
191 }
192 
193 double queryTime()
194 {
195 #ifdef WIN32
196  LONGLONG tmpnewtime;
197  QueryPerformanceCounter( (LARGE_INTEGER*) &tmpnewtime );
198  return ( (double) tmpnewtime )/(double) freq-firsttime;
199 #elif defined (HAVE_GETTIMEOFDAY)
200  struct timeval tv;
201  (void) gettimeofday( &tv, NULL );
202  double tmpnewtime = (double) tv.tv_sec+(double) tv.tv_usec*1.e-6;
203  return tmpnewtime-firsttime;
204 #elif defined (HAVE_SDL)
205  double tmpnewtime = SDL_GetTicks()*1.e-3;
206  return tmpnewtime-firsttime;
207 #else
208 # error "We have no way to determine the time on this system."
209  return 0.;
210 #endif
211 }
212 
213 double realTime()
214 {
215 #ifdef WIN32
216  LONGLONG tmpnewtime;
217  QueryPerformanceCounter( (LARGE_INTEGER*) &tmpnewtime );
218  return ( (double) tmpnewtime )/(double) freq;
219 #elif defined (HAVE_GETTIMEOFDAY)
220  struct timeval tv;
221  (void) gettimeofday( &tv, NULL );
222  double tmpnewtime = (double) tv.tv_sec+(double) tv.tv_usec*1.e-6;
223 #elif defined (HAVE_SDL)
224  double tmpnewtime = SDL_GetTicks()*1.e-3;
225 #else
226 # error "We have no way to determine the time on this system."
227  double tmpnewtime = 0.;
228 #endif
229 
230  static double reallyfirsttime = tmpnewtime;
231  return tmpnewtime - reallyfirsttime;
232 }
233 
235 {
236  static bool first=true;
237 #ifdef WIN32
238  QueryPerformanceCounter( (LARGE_INTEGER*) &newtime );
239  elapsedtime = ( (double) (newtime-ttime) )/freq;
240  ttime = newtime;
241  if (freq == 0)
242  dblnewtime = 0.;
243  else
244  dblnewtime = ( (double) newtime )/( (double) freq );
245  if (first)
246  firsttime = dblnewtime;
247 #elif defined (HAVE_GETTIMEOFDAY)
248  struct timeval tv;
249  (void) gettimeofday( &tv, NULL );
250  lasttime = newtime;
251  newtime = (double) tv.tv_sec+(double) tv.tv_usec*1.e-6;
252  elapsedtime = newtime-lasttime;
253  if (first)
254  firsttime = newtime;
255 #elif defined (HAVE_SDL)
256  lasttime = newtime;
257  newtime = SDL_GetTicks()*1.e-3;
258  elapsedtime = newtime-lasttime;
259  if (first)
260  firsttime = newtime;
261 #else
262 # error "We have no way to determine the time on this system."
263 #endif
264  elapsedtime *= timecompression;
265  first=false;
266 }
267 
268 void setNewTime( double newnewtime )
269 {
270  firsttime -= newnewtime-queryTime();
271  UpdateTime();
272 }
273