FS2_Open
Open source remastering of the Freespace 2 engine
timer.h
Go to the documentation of this file.
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef _TIMER_H
13 #define _TIMER_H
14 
15 #include "globalincs/pstypes.h"
16 
17 //==========================================================================
18 // This installs the timer services and interrupts at the rate specified by
19 // count_val. If 'function' isn't 0, the function pointed to by function will
20 // be called 'freq' times per second. Should be > 19 and anything around
21 // 2-3000 is gonna start slowing down the system. Count_val should be
22 // 1,193,180 divided by your target frequency. Use 0 for the normal 18.2 Hz
23 // interrupt rate.
24 
25 #ifdef _WIN32
26 #define TIMER_FREQUENCY 1193180
27 #endif
28 
29 extern void timer_init();
30 extern void timer_close();
31 
32 //==========================================================================
33 // These functions return the time since the timer was initialized in
34 // some various units. The total length of reading time varies for each
35 // one. They will roll around after they read 2^32.
36 // There are milliseconds, milliseconds times 10, milliseconds times 100,
37 // and microseconds. They time out after 1000 hrs, 100 hrs, 10 hrs, and
38 // 1 hr, respectively.
39 
40 extern fix timer_get_fixed_seconds(); // Rolls about every 9 hours...
41 extern fix timer_get_fixed_secondsX(); // Assume interrupts already disabled
42 extern fix timer_get_approx_seconds(); // Returns time since program started... accurate to 1/120th of a second
43 extern int timer_get_milliseconds(); //
44 extern int timer_get_microseconds();
46 extern int timer_get_seconds(); // seconds since program started... not accurate, but good for long
47  // runtimes with second-based timeouts
48 
49 
50 //=================================================================
51 //=================================================================
52 // T I M E S T A M P F U N C T I O N S
53 //=================================================================
54 //=================================================================
55 
56 // NEVER USE THIS DIRECTLY!!! IF YOU REALLY NEED IT, THEN:
57 // call timestamp(0) and use TIMESTAMP_FREQUENCY to scale it.
58 extern int timestamp_ticker;
59 
60 // You shouldn't use the output of timestamp() directly,
61 // but if you have to, use the TIMESTAMP_FREQUENCY to
62 // scale it correctly.
63 #define TIMESTAMP_FREQUENCY 1000
64 
65 // Call this at least every 600 hours, I would
66 // say at the start of each level should do it.
67 extern void timestamp_reset();
68 
69 // Call this once every frame with the frametime.
70 extern void timestamp_inc(int frametime_ms);
71 
72 // To do timing, call this with the interval you
73 // want to check. Then, pass this to timestamp_elapsed
74 // to see if delta_ms time has elapsed. If delta_ms is
75 // zero, the next call to timestamp_elapsed will always
76 // return 1. If delta_ms is less than zero, then this is
77 // considered an invalid timestamp and all calls to
78 // timestamp_elapsed will return 0.
79 // In other words:
80 // pass -1 for an invalid timestamp that will never time out
81 // pass 0 for a timestamp that is instantly timed out
82 // pass n > 0 for timestamp n milliseconds in the future.
83 int timestamp(int delta_ms );
84 
85 // use this call to get the current counter value (which represents the time at the time
86 // this function is called). I.e. it doesn't return a count that would be in the future,
87 // but the count that is right now.
88 int timestamp();
89 
90 // gets a timestamp randomly between a and b milliseconds in
91 // the future.
92 #define timestamp_rand(a,b) timestamp((myrand()%((b)-(a)+1))+(a))
93 
94 // Example that makes a ship fire in 1/2 second
95 
96 // ...
97 // ship->next_fire = timestamp(500);
98 // ...
99 // if (fire && timestamp_elapsed(ship->next_fire))
100 // fire_laser();
101 
102 #define timestamp_elapsed( stamp ) ( (stamp!=0) ? (timestamp_ticker >= (stamp) ? 1 : 0) : 0 )
103 
104 #define timestamp_valid(stamp) ((stamp==0) ? 0 : 1 )
105 
106 // Returns millliseconds until timestamp will elapse.
107 int timestamp_until(int stamp);
108 
109 // checks if a specified time (in milliseconds) has elapsed past the given timestamp (which
110 // should be obtained from timestamp() or timestamp(x) with a positive x)
111 int timestamp_has_time_elapsed(int stamp, int time);
112 
113 // safer version of timestamp
114 #define timestamp_elapsed_safe(_a, _b) ( (_a != 0) ? (((timestamp_ticker >= (_a)) || (timestamp_ticker < (_a - (_b + 100)))) ? 1 : 0) : 1 )
115 
116 
117 #endif
void timestamp_inc(int frametime_ms)
Definition: timer.cpp:212
int timer_get_milliseconds()
Definition: timer.cpp:150
uint timer_get_high_res_microseconds()
Definition: timer.cpp:170
fix timer_get_fixed_secondsX()
Definition: timer.cpp:130
void timer_init()
Definition: timer.cpp:55
char stamp[STAMP_STRING_LENGTH]
Definition: fredview.cpp:74
int timestamp_has_time_elapsed(int stamp, int time)
Definition: timer.cpp:272
fix timer_get_fixed_seconds()
Definition: timer.cpp:116
void timestamp_reset()
Definition: timer.cpp:201
int timer_get_microseconds()
Definition: timer.cpp:160
unsigned int uint
Definition: pstypes.h:64
int timer_get_seconds()
Definition: timer.cpp:140
fix timer_get_approx_seconds()
Definition: timer.cpp:135
long fix
Definition: pstypes.h:54
int timestamp_ticker
Definition: timer.cpp:199
void timer_close()
Definition: timer.cpp:44
int timestamp_until(int stamp)
Definition: timer.cpp:242
int timestamp(int delta_ms)
Definition: timer.cpp:226