2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Io/Timer.h $
15 * Include file for timer stuff
18 * Revision 1.2 2002/06/09 04:41:15 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:12 root
25 * 3 5/17/99 6:03p Dave
26 * Added new timing code. Put in code to allow camera speed zooming.
28 * 2 10/07/98 10:53a Dave
31 * 1 10/07/98 10:49a Dave
33 * 11 4/14/98 2:26p Hoffoss
34 * Removed function prototype that doesn't exist anymore.
36 * 10 9/11/97 7:12p Hoffoss
37 * Added more functionality to training sexp handling code.
39 * 9 8/05/97 10:18a Lawrance
40 * my_rand() being used temporarily instead of rand()
42 * 8 7/29/97 5:30p Lawrance
43 * move gettime() from keyboard module to timer module
45 * 7 7/16/97 4:42p Mike
46 * Make afterburner shake viewer, not ship.
47 * Shake for limited time.
48 * Add timestamp_until() function to timer library.
50 * 6 6/26/97 12:05p Allender
51 * fixed timestamp_valid macro
53 * 5 2/17/97 5:18p John
54 * Added a bunch of RCS headers to a bunch of old files that don't have
65 //==========================================================================
66 // This installs the timer services and interrupts at the rate specified by
67 // count_val. If 'function' isn't 0, the function pointed to by function will
68 // be called 'freq' times per second. Should be > 19 and anything around
69 // 2-3000 is gonna start slowing down the system. Count_val should be
70 // 1,193,180 divided by your target frequency. Use 0 for the normal 18.2 Hz
73 #define TIMER_FREQUENCY 1193180
75 extern void timer_init();
76 extern void timer_close();
77 extern void timer_set_rate(int count_val);
78 extern void timer_set_function( void * function );
80 //==========================================================================
81 // These functions return the time since the timer was initialized in
82 // some various units. The total length of reading time varies for each
83 // one. They will roll around after they read 2^32.
84 // There are milliseconds, milliseconds times 10, milliseconds times 100,
85 // and microseconds. They time out after 1000 hrs, 100 hrs, 10 hrs, and
86 // 1 hr, respectively.
88 extern fix timer_get_fixed_seconds(); // Rolls about every 9 hours...
89 extern fix timer_get_fixed_secondsX(); // Assume interrupts already disabled
90 extern fix timer_get_approx_seconds(); // Returns time since program started... accurate to 1/120th of a second
91 extern int timer_get_seconds();
92 extern int timer_get_milliseconds(); //
93 extern int timer_get_microseconds();
95 //==========================================================================
96 // Use to access the BIOS ticker... ie... i = TICKER
97 void timer_delay(fix seconds);
100 //=================================================================
101 //=================================================================
102 // T I M E S T A M P F U N C T I O N S
103 //=================================================================
104 //=================================================================
106 // NEVER USE THIS DIRECTLY!!! IF YOU REALLY NEED IT, THEN:
107 // call timestamp(0) and use TIMESTAMP_FREQUENCY to scale it.
108 extern int timestamp_ticker;
110 // You shouldn't use the output of timestamp() directly,
111 // but if you have to, use the TIMESTAMP_FREQUENCY to
112 // scale it correctly.
113 #define TIMESTAMP_FREQUENCY 1000
115 // Call this at least every 600 hours, I would
116 // say at the start of each level should do it.
117 extern void timestamp_reset();
119 // Call this once every frame with the frametime.
120 extern void timestamp_inc(float frametime);
122 // To do timing, call this with the interval you
123 // want to check. Then, pass this to timestamp_elapsed
124 // to see if delta_ms time has elapsed. If delta_ms is
125 // zero, the next call to timestamp_elapsed will always
126 // return 1. If delta_ms is less than zero, then this is
127 // considered an invalid timestamp and all calls to
128 // timestamp_elapsed will return 0.
130 // pass -1 for an invalid timestamp that will never time out
131 // pass 0 for a timestamp that is instantly timed out
132 // pass n > 0 for timestamp n milliseconds in the future.
133 int timestamp(int delta_ms );
135 // use this call to get the current counter value (which represents the time at the time
136 // this function is called). I.e. it doesn't return a count that would be in the future,
137 // but the count that is right now.
140 // gets a timestamp randomly between a and b milliseconds in
142 #define timestamp_rand(a,b) timestamp((myrand()%((b)-(a)+1))+(a))
144 // Example that makes a ship fire in 1/2 second
147 // ship->next_fire = timestamp(500);
149 // if (fire && timestamp_elapsed(ship->next_fire))
152 #define timestamp_elapsed( stamp ) ( (stamp!=0) ? (timestamp_ticker >= (stamp) ? 1 : 0) : 0 )
154 #define timestamp_valid(stamp) ((stamp==0) ? 0 : 1 )
156 // Returns millliseconds until timestamp will elapse.
157 int timestamp_until(int stamp);
159 // checks if a specified time (in milliseconds) has elapsed past the given timestamp (which
160 // should be obtained from timestamp() or timestamp(x) with a positive x)
161 int timestamp_has_time_elapsed(int stamp, int time);
164 // timing functions -------------------------------------------------------------------------------
166 // start timing frame stuff
167 void timing_frame_start();
169 // done timing the frame
170 void timing_frame_stop();
172 // get the total frame time in microseconds
173 int timing_frame_total();
175 // time an individual event
176 void timing_event_start(char *event_name);
178 // stop timing an event
179 void timing_event_stop(char *event_name);
181 // get the total time for an event in microseconds
182 int timing_event_total(char *event_name);
184 // get the percentage of total frametime for the event (0.0 to 1.0)
185 float timing_event_pct(char *event_name);
188 void timing_display(int x, int y);