2 * $Logfile: /Freespace2/code/Io/Timer.cpp $
7 * Include file for timer stuff
10 * Revision 1.4 2002/05/28 21:36:10 relnev
11 * some more timer junk.
13 * tried to fix software mode.
15 * Revision 1.3 2002/05/28 17:26:57 theoddone33
16 * Fill in some timer and palette setting stubs. Still no display
18 * Revision 1.2 2002/05/07 03:16:46 theoddone33
19 * The Great Newline Fix
21 * Revision 1.1.1.1 2002/05/03 03:28:09 root
25 * 4 5/26/99 3:19p Dave
26 * Fixed release build errors.
28 * 3 5/17/99 6:03p Dave
29 * Added new timing code. Put in code to allow camera speed zooming.
31 * 2 10/07/98 10:53a Dave
34 * 1 10/07/98 10:49a Dave
36 * 14 4/13/98 10:16a John
37 * Switched gettime back to timer_get_milliseconds, which is now thread
40 * 13 4/13/98 10:11a John
41 * Made timer functions thread safe. Made timer_init be called in all
44 * 12 4/13/98 8:09a John
45 * Made timer_get functions thread safe.
47 * 11 12/02/97 3:56p John
48 * fixed a bug with timestamp_until, created one with it rolling over.
50 * 10 9/11/97 7:12p Hoffoss
51 * Added more functionality to training sexp handling code.
53 * 9 8/29/97 4:49p Allender
54 * added mprintf to check for wrap problems with timestamp ticker
56 * 8 8/28/97 1:38p Allender
57 * from John: changes to timer func to detect early rollever
59 * 7 7/29/97 5:30p Lawrance
60 * move gettime() from keyboard module to timer module
62 * 6 7/16/97 4:42p Mike
63 * Make afterburner shake viewer, not ship.
64 * Shake for limited time.
65 * Add timestamp_until() function to timer library.
67 * 5 2/17/97 5:18p John
68 * Added a bunch of RCS headers to a bunch of old files that don't have
82 #include "alphacolors.h"
88 static longlong Timer_last_value, Timer_base;
89 static uint Timer_freq=0;
91 static int Timer_inited = 0;
93 static CRITICAL_SECTION Timer_lock;
102 DeleteCriticalSection( &Timer_lock );
109 if ( !Timer_inited ) {
111 SDL_InitSubSystem(SDL_INIT_TIMER);
114 QueryPerformanceFrequency(&tmp);
115 Assert( tmp.HighPart == 0 );
116 Timer_freq = tmp.LowPart;
118 QueryPerformanceCounter((LARGE_INTEGER *)&Timer_base);
119 QueryPerformanceCounter((LARGE_INTEGER *)&Timer_last_value);
121 InitializeCriticalSection(&Timer_lock);
130 // Fills Time_now with the ticks since program start
131 static void timer_get(LARGE_INTEGER * out)
136 EnterCriticalSection(&Timer_lock);
141 QueryPerformanceCounter((LARGE_INTEGER *)&time_tmp);
142 if ( time_tmp < Timer_last_value ) {
143 // The clock has rolled!
144 Timer_base = time_tmp;
145 mprintf(( "TIMER ROLLED!\n" ));
146 // Hack: I'm not accounting for the time before roll occured,
147 // since I'm not sure at what value this timer is going to roll at.
150 Time_now = time_tmp - Timer_base;
151 Timer_last_value = time_tmp;
153 out->QuadPart = Time_now;
155 LeaveCriticalSection(&Timer_lock);
159 fix timer_get_fixed_seconds()
165 return (SDL_GetTicks() << 16) / 1000;
168 LARGE_INTEGER temp_large;
171 Int3(); // Make sure you call timer_init before anything that uses timer functions!
175 timer_get(&temp_large);
177 // Timing in fixed point (16.16) seconds.
178 // Can be used for up to 1000 hours
179 _asm mov edx, temp_large.HighPart
180 _asm mov eax, temp_large.LowPart
182 _asm shld edx, eax, 16 ; Keep 32+11 bits
184 // edx:eax = number of 1.19Mhz pulses elapsed.
185 _asm mov ebx, Timer_freq
187 // Make sure we won't divide overflow. Make time wrap at about 9 hours
189 _asm sub edx, ebx ; subtract until negative...
190 _asm jns sub_again ; ...to prevent divide overflow...
191 _asm add edx, ebx ; ...then add in to get correct value.
193 //eax = fixed point seconds elapsed...
201 fix timer_get_fixed_secondsX()
203 return timer_get_fixed_seconds();
206 fix timer_get_approx_seconds()
208 return timer_get_fixed_seconds();
211 int timer_get_milliseconds()
214 return SDL_GetTicks();
217 LARGE_INTEGER temp_large;
220 Int3(); // Make sure you call timer_init before anything that uses timer functions!
224 timer_get(&temp_large);
226 temp_large.QuadPart *= (longlong)1000;
228 // Timing in milliseconds.
229 _asm mov edx, temp_large.HighPart
230 _asm mov eax, temp_large.LowPart
232 //_asm shld edx, eax, 16 ; Keep 32+11 bits
234 // edx:eax = number of 1.19Mhz pulses elapsed.
235 _asm mov ebx, Timer_freq
237 // Make sure we won't divide overflow. Make time wrap at about 9 hours
239 _asm sub edx, ebx ; subtract until negative...
240 _asm jns sub_again ; ...to prevent divide overflow...
241 _asm add edx, ebx ; ...then add in to get correct value.
243 //eax = milliseconds elapsed...
251 int timer_get_microseconds()
257 return SDL_GetTicks() * 1000;
260 LARGE_INTEGER temp_large;
263 Int3(); // Make sure you call timer_init before anything that uses timer functions!
267 timer_get(&temp_large);
269 temp_large.QuadPart *= (longlong)1000000;
271 // Timing in milliseconds.
272 _asm mov edx, temp_large.HighPart
273 _asm mov eax, temp_large.LowPart
275 //_asm shld edx, eax, 16 ; Keep 32+11 bits
277 // edx:eax = number of 1.19Mhz pulses elapsed.
278 _asm mov ebx, Timer_freq
280 // Make sure we won't divide overflow. Make time wrap at about 9 hours
282 _asm sub edx, ebx ; subtract until negative...
283 _asm jns sub_again ; ...to prevent divide overflow...
284 _asm add edx, ebx ; ...then add in to get correct value.
286 //eax = milliseconds elapsed...
295 // 1 means always return true
296 // 2 and above actually check the time
297 int timestamp_ticker = 2;
299 void timestamp_reset()
301 timestamp_ticker = 2;
304 // Restrict all time values between 0 and MAX_TIME
305 // so we don't have to use UINTs to calculate rollover.
306 // For debugging & testing, you could set this to
307 // something like 1 minute (6000).
308 #define MAX_TIME (INT_MAX/2)
310 void timestamp_inc(float frametime)
312 timestamp_ticker += (int)(frametime*TIMESTAMP_FREQUENCY);
314 if ( timestamp_ticker > MAX_TIME ) {
315 timestamp_ticker = 2; // Roll!
318 if (timestamp_ticker < 2 ) {
319 mprintf(("Whoa!!! timestamp_ticker < 2 -- resetting to 2!!!\n"));
320 timestamp_ticker = 2;
324 int timestamp(int delta_ms )
327 if (delta_ms < 0 ) return 0;
328 if (delta_ms == 0 ) return 1;
329 t2 = timestamp_ticker + delta_ms;
330 if ( t2 > MAX_TIME ) {
332 t2 = delta_ms - (MAX_TIME-timestamp_ticker);
334 if (t2 < 2 ) t2 = 2; // hack??
338 // Returns milliseconds until timestamp will elapse.
339 // Negative value gives milliseconds ago that timestamp elapsed.
340 int timestamp_until(int stamp)
343 // HACK!! This doesn't handle rollover!
344 // (Will it ever happen?)
346 return stamp - timestamp_ticker;
351 delta = stamp - timestamp_ticker;
354 if (delta > UINT_MAX/2)
355 delta = UINT_MAX - delta + 1;
356 else if (delta < - ( (int) (UINT_MAX/2)))
357 delta = UINT_MAX + delta + 1;
363 // alternate timestamp functions. The way these work is you call xtimestamp() to get the
364 // current counter value, and then call
367 return timestamp_ticker;
370 int timestamp_has_time_elapsed(int stamp, int time)
378 if (t <= timestamp_ticker)
379 return 1; // if we are unlucky enough to have it wrap on us, this will assume time has elapsed.
384 // timing functions -------------------------------------------------------------------------------
386 #define MAX_TIMING_EVENTS 15
391 typedef struct timing {
393 int microseconds_total;
399 timing Timing_events[MAX_TIMING_EVENTS];
400 int Timing_event_count = 0;
404 // lookup a timing event
405 int timing_event_lookup(char *event_name)
413 if(event_name == NULL){
417 // look through all events
418 for(idx=0; idx<MAX_TIMING_EVENTS; idx++){
419 if(!stricmp(Timing_events[idx].name, event_name)){
428 // start timing frame stuff
429 void timing_frame_start()
437 Timing_event_count = 0;
438 Timing_frame.start = timer_get_microseconds();
439 for(idx=0; idx<MAX_TIMING_EVENTS; idx++){
440 Timing_events[idx].microseconds_total = 0;
441 strcpy(Timing_events[idx].name, "");
442 Timing_events[idx].ref_count = 0;
447 // done timing the frame
448 void timing_frame_stop()
456 stop_time = timer_get_microseconds();
459 Timing_frame.microseconds_total = stop_time - Timing_frame.start;
463 // get the total frame time in microseconds
464 int timing_frame_total()
469 return Timing_frame.microseconds_total;
473 // time an individual event
474 void timing_event_start(char *event_name)
482 if(event_name == NULL){
486 // try and find the event
487 event = timing_event_lookup(event_name);
489 // if we already have one
491 Assert(Timing_events[event].ref_count == 0);
492 Timing_events[event].start = timer_get_microseconds();
493 Timing_events[event].ref_count++;
495 // if we need to add a new one
497 if(Timing_event_count < MAX_TIMING_EVENTS){
498 strcpy(Timing_events[Timing_event_count].name, event_name);
499 Timing_events[Timing_event_count].start = timer_get_microseconds();
500 Timing_events[Timing_event_count++].ref_count++;
506 // stop timing an event
507 void timing_event_stop(char *event_name)
515 if(event_name == NULL){
519 // try and find the event
520 event = timing_event_lookup(event_name);
522 // if we already have one
524 Assert(Timing_events[event].ref_count == 1);
525 Timing_events[event].microseconds_total += timer_get_microseconds() - Timing_events[event].start;
526 Timing_events[event].ref_count--;
531 // get the total time for an event in microseconds
532 int timing_event_total(char *event_name)
540 if(event_name == NULL){
544 // try and find the event
545 event = timing_event_lookup(event_name);
550 return Timing_events[event].microseconds_total;
554 // get the percentage of total frametime for the event (0.0 to 1.0)
555 float timing_event_pct(char *event_name)
563 if(event_name == NULL){
567 // try and find the event
568 event = timing_event_lookup(event_name);
573 return (float)((double)Timing_events[event].microseconds_total / (double)Timing_frame.microseconds_total);
578 void timing_display(int x, int y)
585 gr_set_color_fast(&Color_bright_blue);
588 gr_printf(x, y, "Total time %f\n", (float)timing_frame_total() / 1000000.0f);
591 // each event percentage
592 for(idx=0; idx<Timing_event_count; idx++){
593 gr_printf(x, y, "%s : %f\n", Timing_events[idx].name, timing_event_pct(Timing_events[idx].name));