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