]> icculus.org git repositories - taylor/freespace2.git/blob - include/timer.h
fix issue with looping audio streams
[taylor/freespace2.git] / include / timer.h
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Io/Timer.h $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Include file for timer stuff
16  *
17  * $Log$
18  * Revision 1.2  2002/06/09 04:41:15  relnev
19  * added copyright header
20  *
21  * Revision 1.1.1.1  2002/05/03 03:28:12  root
22  * Initial import.
23  *
24  * 
25  * 3     5/17/99 6:03p Dave
26  * Added new timing code. Put in code to allow camera speed zooming.
27  * 
28  * 2     10/07/98 10:53a Dave
29  * Initial checkin.
30  * 
31  * 1     10/07/98 10:49a Dave
32  * 
33  * 11    4/14/98 2:26p Hoffoss
34  * Removed function prototype that doesn't exist anymore.
35  * 
36  * 10    9/11/97 7:12p Hoffoss
37  * Added more functionality to training sexp handling code.
38  * 
39  * 9     8/05/97 10:18a Lawrance
40  * my_rand() being used temporarily instead of rand()
41  * 
42  * 8     7/29/97 5:30p Lawrance
43  * move gettime() from keyboard module to timer module
44  * 
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.
49  * 
50  * 6     6/26/97 12:05p Allender
51  * fixed timestamp_valid macro
52  * 
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
55  * them.
56  *
57  * $NoKeywords: $
58  */
59
60 #ifndef _TIMER_H
61 #define _TIMER_H
62
63 #include "pstypes.h"
64
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
71 // interrupt rate.
72
73 #define TIMER_FREQUENCY 1193180
74
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 );
79
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.
87
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();
94
95 //==========================================================================
96 // Use to access the BIOS ticker... ie...   i = TICKER
97 void timer_delay(fix seconds);
98
99
100 //=================================================================
101 //=================================================================
102 //               T I M E S T A M P   F U N C T I O N S
103 //=================================================================
104 //=================================================================
105
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;    
109
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
114
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();
118
119 // Call this once every frame with the frametime.
120 extern void timestamp_inc(float frametime);
121
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.
129 // In other words:
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 );
134
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.
138 int timestamp();
139
140 // gets a timestamp randomly between a and b milliseconds in
141 // the future.
142 #define timestamp_rand(a,b) timestamp((myrand()%((b)-(a)+1))+(a))
143
144 // Example that makes a ship fire in 1/2 second
145
146 // ...
147 // ship->next_fire = timestamp(500);
148 // ...
149 // if (fire && timestamp_elapsed(ship->next_fire))
150 //   fire_laser();
151
152 #define timestamp_elapsed( stamp ) ( (stamp!=0) ? (timestamp_ticker >= (stamp) ? 1 : 0) : 0 )
153
154 #define timestamp_valid(stamp) ((stamp==0) ? 0 : 1 )
155
156 //      Returns millliseconds until timestamp will elapse.
157 int timestamp_until(int stamp);
158
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);
162
163
164 // timing functions -------------------------------------------------------------------------------
165
166 // start timing frame stuff
167 void timing_frame_start();
168
169 // done timing the frame
170 void timing_frame_stop();
171
172 // get the total frame time in microseconds
173 int timing_frame_total();
174
175 // time an individual event
176 void timing_event_start(char *event_name);
177
178 // stop timing an event
179 void timing_event_stop(char *event_name);
180
181 // get the total time for an event in microseconds
182 int timing_event_total(char *event_name);
183
184 // get the percentage of total frametime for the event (0.0 to 1.0)
185 float timing_event_pct(char *event_name);
186
187 // display timing 
188 void timing_display(int x, int y);
189
190 #endif
191