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_milliseconds(); //
92 extern int timer_get_microseconds();
94 //==========================================================================
95 // Use to access the BIOS ticker... ie... i = TICKER
96 void timer_delay(fix seconds);
99 //=================================================================
100 //=================================================================
101 // T I M E S T A M P F U N C T I O N S
102 //=================================================================
103 //=================================================================
105 // NEVER USE THIS DIRECTLY!!! IF YOU REALLY NEED IT, THEN:
106 // call timestamp(0) and use TIMESTAMP_FREQUENCY to scale it.
107 extern int timestamp_ticker;
109 // You shouldn't use the output of timestamp() directly,
110 // but if you have to, use the TIMESTAMP_FREQUENCY to
111 // scale it correctly.
112 #define TIMESTAMP_FREQUENCY 1000
114 // Call this at least every 600 hours, I would
115 // say at the start of each level should do it.
116 extern void timestamp_reset();
118 // Call this once every frame with the frametime.
119 extern void timestamp_inc(float frametime);
121 // To do timing, call this with the interval you
122 // want to check. Then, pass this to timestamp_elapsed
123 // to see if delta_ms time has elapsed. If delta_ms is
124 // zero, the next call to timestamp_elapsed will always
125 // return 1. If delta_ms is less than zero, then this is
126 // considered an invalid timestamp and all calls to
127 // timestamp_elapsed will return 0.
129 // pass -1 for an invalid timestamp that will never time out
130 // pass 0 for a timestamp that is instantly timed out
131 // pass n > 0 for timestamp n milliseconds in the future.
132 int timestamp(int delta_ms );
134 // use this call to get the current counter value (which represents the time at the time
135 // this function is called). I.e. it doesn't return a count that would be in the future,
136 // but the count that is right now.
139 // gets a timestamp randomly between a and b milliseconds in
141 #define timestamp_rand(a,b) timestamp((myrand()%((b)-(a)+1))+(a))
143 // Example that makes a ship fire in 1/2 second
146 // ship->next_fire = timestamp(500);
148 // if (fire && timestamp_elapsed(ship->next_fire))
151 #define timestamp_elapsed( stamp ) ( (stamp!=0) ? (timestamp_ticker >= (stamp) ? 1 : 0) : 0 )
153 #define timestamp_valid(stamp) ((stamp==0) ? 0 : 1 )
155 // Returns millliseconds until timestamp will elapse.
156 int timestamp_until(int stamp);
158 // checks if a specified time (in milliseconds) has elapsed past the given timestamp (which
159 // should be obtained from timestamp() or timestamp(x) with a positive x)
160 int timestamp_has_time_elapsed(int stamp, int time);
163 // timing functions -------------------------------------------------------------------------------
165 // start timing frame stuff
166 void timing_frame_start();
168 // done timing the frame
169 void timing_frame_stop();
171 // get the total frame time in microseconds
172 int timing_frame_total();
174 // time an individual event
175 void timing_event_start(char *event_name);
177 // stop timing an event
178 void timing_event_stop(char *event_name);
180 // get the total time for an event in microseconds
181 int timing_event_total(char *event_name);
183 // get the percentage of total frametime for the event (0.0 to 1.0)
184 float timing_event_pct(char *event_name);
187 void timing_display(int x, int y);