2 * $Logfile: /Freespace2/code/Io/Timer.cpp $
7 * Include file for timer stuff
10 * Revision 1.5 2002/05/29 21:38:20 relnev
13 * Revision 1.4 2002/05/28 21:36:10 relnev
14 * some more timer junk.
16 * tried to fix software mode.
18 * Revision 1.3 2002/05/28 17:26:57 theoddone33
19 * Fill in some timer and palette setting stubs. Still no display
21 * Revision 1.2 2002/05/07 03:16:46 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:09 root
28 * 4 5/26/99 3:19p Dave
29 * Fixed release build errors.
31 * 3 5/17/99 6:03p Dave
32 * Added new timing code. Put in code to allow camera speed zooming.
34 * 2 10/07/98 10:53a Dave
37 * 1 10/07/98 10:49a Dave
39 * 14 4/13/98 10:16a John
40 * Switched gettime back to timer_get_milliseconds, which is now thread
43 * 13 4/13/98 10:11a John
44 * Made timer functions thread safe. Made timer_init be called in all
47 * 12 4/13/98 8:09a John
48 * Made timer_get functions thread safe.
50 * 11 12/02/97 3:56p John
51 * fixed a bug with timestamp_until, created one with it rolling over.
53 * 10 9/11/97 7:12p Hoffoss
54 * Added more functionality to training sexp handling code.
56 * 9 8/29/97 4:49p Allender
57 * added mprintf to check for wrap problems with timestamp ticker
59 * 8 8/28/97 1:38p Allender
60 * from John: changes to timer func to detect early rollever
62 * 7 7/29/97 5:30p Lawrance
63 * move gettime() from keyboard module to timer module
65 * 6 7/16/97 4:42p Mike
66 * Make afterburner shake viewer, not ship.
67 * Shake for limited time.
68 * Add timestamp_until() function to timer library.
70 * 5 2/17/97 5:18p John
71 * Added a bunch of RCS headers to a bunch of old files that don't have
85 #include "alphacolors.h"
91 static longlong Timer_last_value, Timer_base;
92 static uint Timer_freq=0;
94 static int Timer_inited = 0;
96 static CRITICAL_SECTION Timer_lock;
100 if ( Timer_inited ) {
105 DeleteCriticalSection( &Timer_lock );
112 if ( !Timer_inited ) {
114 SDL_InitSubSystem(SDL_INIT_TIMER);
117 QueryPerformanceFrequency(&tmp);
118 Assert( tmp.HighPart == 0 );
119 Timer_freq = tmp.LowPart;
121 QueryPerformanceCounter((LARGE_INTEGER *)&Timer_base);
122 QueryPerformanceCounter((LARGE_INTEGER *)&Timer_last_value);
124 InitializeCriticalSection(&Timer_lock);
133 // Fills Time_now with the ticks since program start
134 static void timer_get(LARGE_INTEGER * out)
139 EnterCriticalSection(&Timer_lock);
144 QueryPerformanceCounter((LARGE_INTEGER *)&time_tmp);
145 if ( time_tmp < Timer_last_value ) {
146 // The clock has rolled!
147 Timer_base = time_tmp;
148 mprintf(( "TIMER ROLLED!\n" ));
149 // Hack: I'm not accounting for the time before roll occured,
150 // since I'm not sure at what value this timer is going to roll at.
153 Time_now = time_tmp - Timer_base;
154 Timer_last_value = time_tmp;
156 out->QuadPart = Time_now;
158 LeaveCriticalSection(&Timer_lock);
162 fix timer_get_fixed_seconds()
168 // return (SDL_GetTicks() << 16) / 1000;
169 __extension__ long long a = SDL_GetTicks();
172 return (fix)(a / 1000);
175 LARGE_INTEGER temp_large;
178 Int3(); // Make sure you call timer_init before anything that uses timer functions!
182 timer_get(&temp_large);
184 // Timing in fixed point (16.16) seconds.
185 // Can be used for up to 1000 hours
186 _asm mov edx, temp_large.HighPart
187 _asm mov eax, temp_large.LowPart
189 _asm shld edx, eax, 16 ; Keep 32+11 bits
191 // edx:eax = number of 1.19Mhz pulses elapsed.
192 _asm mov ebx, Timer_freq
194 // Make sure we won't divide overflow. Make time wrap at about 9 hours
196 _asm sub edx, ebx ; subtract until negative...
197 _asm jns sub_again ; ...to prevent divide overflow...
198 _asm add edx, ebx ; ...then add in to get correct value.
200 //eax = fixed point seconds elapsed...
208 fix timer_get_fixed_secondsX()
210 return timer_get_fixed_seconds();
213 fix timer_get_approx_seconds()
215 return timer_get_fixed_seconds();
218 int timer_get_milliseconds()
221 return SDL_GetTicks();
224 LARGE_INTEGER temp_large;
227 Int3(); // Make sure you call timer_init before anything that uses timer functions!
231 timer_get(&temp_large);
233 temp_large.QuadPart *= (longlong)1000;
235 // Timing in milliseconds.
236 _asm mov edx, temp_large.HighPart
237 _asm mov eax, temp_large.LowPart
239 //_asm shld edx, eax, 16 ; Keep 32+11 bits
241 // edx:eax = number of 1.19Mhz pulses elapsed.
242 _asm mov ebx, Timer_freq
244 // Make sure we won't divide overflow. Make time wrap at about 9 hours
246 _asm sub edx, ebx ; subtract until negative...
247 _asm jns sub_again ; ...to prevent divide overflow...
248 _asm add edx, ebx ; ...then add in to get correct value.
250 //eax = milliseconds elapsed...
258 int timer_get_microseconds()
264 return SDL_GetTicks() * 1000;
267 LARGE_INTEGER temp_large;
270 Int3(); // Make sure you call timer_init before anything that uses timer functions!
274 timer_get(&temp_large);
276 temp_large.QuadPart *= (longlong)1000000;
278 // Timing in milliseconds.
279 _asm mov edx, temp_large.HighPart
280 _asm mov eax, temp_large.LowPart
282 //_asm shld edx, eax, 16 ; Keep 32+11 bits
284 // edx:eax = number of 1.19Mhz pulses elapsed.
285 _asm mov ebx, Timer_freq
287 // Make sure we won't divide overflow. Make time wrap at about 9 hours
289 _asm sub edx, ebx ; subtract until negative...
290 _asm jns sub_again ; ...to prevent divide overflow...
291 _asm add edx, ebx ; ...then add in to get correct value.
293 //eax = milliseconds elapsed...
302 // 1 means always return true
303 // 2 and above actually check the time
304 int timestamp_ticker = 2;
306 void timestamp_reset()
308 timestamp_ticker = 2;
311 // Restrict all time values between 0 and MAX_TIME
312 // so we don't have to use UINTs to calculate rollover.
313 // For debugging & testing, you could set this to
314 // something like 1 minute (6000).
315 #define MAX_TIME (INT_MAX/2)
317 void timestamp_inc(float frametime)
319 timestamp_ticker += (int)(frametime*TIMESTAMP_FREQUENCY);
321 if ( timestamp_ticker > MAX_TIME ) {
322 timestamp_ticker = 2; // Roll!
325 if (timestamp_ticker < 2 ) {
326 mprintf(("Whoa!!! timestamp_ticker < 2 -- resetting to 2!!!\n"));
327 timestamp_ticker = 2;
331 int timestamp(int delta_ms )
334 if (delta_ms < 0 ) return 0;
335 if (delta_ms == 0 ) return 1;
336 t2 = timestamp_ticker + delta_ms;
337 if ( t2 > MAX_TIME ) {
339 t2 = delta_ms - (MAX_TIME-timestamp_ticker);
341 if (t2 < 2 ) t2 = 2; // hack??
345 // Returns milliseconds until timestamp will elapse.
346 // Negative value gives milliseconds ago that timestamp elapsed.
347 int timestamp_until(int stamp)
350 // HACK!! This doesn't handle rollover!
351 // (Will it ever happen?)
353 return stamp - timestamp_ticker;
358 delta = stamp - timestamp_ticker;
361 if (delta > UINT_MAX/2)
362 delta = UINT_MAX - delta + 1;
363 else if (delta < - ( (int) (UINT_MAX/2)))
364 delta = UINT_MAX + delta + 1;
370 // alternate timestamp functions. The way these work is you call xtimestamp() to get the
371 // current counter value, and then call
374 return timestamp_ticker;
377 int timestamp_has_time_elapsed(int stamp, int time)
385 if (t <= timestamp_ticker)
386 return 1; // if we are unlucky enough to have it wrap on us, this will assume time has elapsed.
391 // timing functions -------------------------------------------------------------------------------
393 #define MAX_TIMING_EVENTS 15
398 typedef struct timing {
400 int microseconds_total;
406 timing Timing_events[MAX_TIMING_EVENTS];
407 int Timing_event_count = 0;
411 // lookup a timing event
412 int timing_event_lookup(char *event_name)
420 if(event_name == NULL){
424 // look through all events
425 for(idx=0; idx<MAX_TIMING_EVENTS; idx++){
426 if(!stricmp(Timing_events[idx].name, event_name)){
435 // start timing frame stuff
436 void timing_frame_start()
444 Timing_event_count = 0;
445 Timing_frame.start = timer_get_microseconds();
446 for(idx=0; idx<MAX_TIMING_EVENTS; idx++){
447 Timing_events[idx].microseconds_total = 0;
448 strcpy(Timing_events[idx].name, "");
449 Timing_events[idx].ref_count = 0;
454 // done timing the frame
455 void timing_frame_stop()
463 stop_time = timer_get_microseconds();
466 Timing_frame.microseconds_total = stop_time - Timing_frame.start;
470 // get the total frame time in microseconds
471 int timing_frame_total()
476 return Timing_frame.microseconds_total;
480 // time an individual event
481 void timing_event_start(char *event_name)
489 if(event_name == NULL){
493 // try and find the event
494 event = timing_event_lookup(event_name);
496 // if we already have one
498 Assert(Timing_events[event].ref_count == 0);
499 Timing_events[event].start = timer_get_microseconds();
500 Timing_events[event].ref_count++;
502 // if we need to add a new one
504 if(Timing_event_count < MAX_TIMING_EVENTS){
505 strcpy(Timing_events[Timing_event_count].name, event_name);
506 Timing_events[Timing_event_count].start = timer_get_microseconds();
507 Timing_events[Timing_event_count++].ref_count++;
513 // stop timing an event
514 void timing_event_stop(char *event_name)
522 if(event_name == NULL){
526 // try and find the event
527 event = timing_event_lookup(event_name);
529 // if we already have one
531 Assert(Timing_events[event].ref_count == 1);
532 Timing_events[event].microseconds_total += timer_get_microseconds() - Timing_events[event].start;
533 Timing_events[event].ref_count--;
538 // get the total time for an event in microseconds
539 int timing_event_total(char *event_name)
547 if(event_name == NULL){
551 // try and find the event
552 event = timing_event_lookup(event_name);
557 return Timing_events[event].microseconds_total;
561 // get the percentage of total frametime for the event (0.0 to 1.0)
562 float timing_event_pct(char *event_name)
570 if(event_name == NULL){
574 // try and find the event
575 event = timing_event_lookup(event_name);
580 return (float)((double)Timing_events[event].microseconds_total / (double)Timing_frame.microseconds_total);
585 void timing_display(int x, int y)
592 gr_set_color_fast(&Color_bright_blue);
595 gr_printf(x, y, "Total time %f\n", (float)timing_frame_total() / 1000000.0f);
598 // each event percentage
599 for(idx=0; idx<Timing_event_count; idx++){
600 gr_printf(x, y, "%s : %f\n", Timing_events[idx].name, timing_event_pct(Timing_events[idx].name));