]> git.mxchange.org Git - flightgear.git/blob - Time/fg_timer.cxx
Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
[flightgear.git] / Time / fg_timer.cxx
1 // fg_timer.cxx -- time handling routines
2 //
3 // Written by Curtis Olson, started June 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <signal.h>    // for timer routines
30 #include <stdio.h>     // for printf()
31
32 #ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 #endif
35
36 #ifdef HAVE_SYS_TIME_H
37 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
38 #endif
39
40 #include "fg_time.hxx"
41 #include "fg_timer.hxx"
42 #include "timestamp.hxx"
43
44
45 unsigned long int fgSimTime;
46
47 #ifdef HAVE_SETITIMER
48   static struct itimerval t, ot;
49   static void (*callbackfunc)(int multi_loop);
50
51
52 // This routine catches the SIGALRM
53 void fgTimerCatch( int dummy ) {
54     int warning_avoider;
55
56     // get past a compiler warning
57     warning_avoider = dummy;
58
59     // ignore any SIGALRM's until we come back from our EOM iteration
60     signal(SIGALRM, SIG_IGN);
61
62     // printf("In fgTimerCatch()\n");
63
64     // -1 tells the routine to use default interval rather than
65     // something dynamically calculated based on frame rate
66     callbackfunc(-1); 
67
68     signal(SIGALRM, fgTimerCatch);
69 }
70
71
72 // this routine initializes the interval timer to generate a SIGALRM
73 // after the specified interval (dt)
74 void fgTimerInit(float dt, void (*f)( int )) {
75     int terr;
76     int isec;
77     int usec;
78
79     callbackfunc = f;
80
81     isec = (int) dt;
82     usec = 1000000 * ((int)dt - isec);
83
84     t.it_interval.tv_sec = isec;
85     t.it_interval.tv_usec = usec;
86     t.it_value.tv_sec = isec;
87     t.it_value.tv_usec = usec;
88     // printf("fgTimerInit() called\n");
89     fgTimerCatch(0);   // set up for SIGALRM signal catch
90     terr = setitimer( ITIMER_REAL, &t, &ot );
91     if (terr) {
92         printf("Error returned from setitimer");
93         exit(0);
94     }
95 }
96 #endif // HAVE_SETITIMER
97
98
99 // This function returns the number of microseconds since the last
100 // time it was called.
101 int fgGetTimeInterval( void ) {
102     int interval;
103     static int inited = 0;
104     static FGTimeStamp last;
105     FGTimeStamp current;
106
107     if ( ! inited ) {
108         inited = 1;
109         last.stamp();
110         interval = 0;
111     } else {
112         current.stamp();
113         interval = current - last;
114         last = current;
115     }
116
117     return(interval);
118 }
119
120
121 // $Log$
122 // Revision 1.6  1999/01/09 13:37:45  curt
123 // Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
124 //
125 // Revision 1.5  1998/12/05 14:21:32  curt
126 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
127 // to it's own file, timestamp.hxx.
128 //
129 // Revision 1.4  1998/12/04 01:32:51  curt
130 // Converted "struct fg_timestamp" to "class fgTIMESTAMP" and added some
131 // convenience inline operators.
132 //
133 // Revision 1.3  1998/04/28 01:22:18  curt
134 // Type-ified fgTIME and fgVIEW.
135 //
136 // Revision 1.2  1998/04/25 20:24:03  curt
137 // Cleaned up initialization sequence to eliminate interdependencies
138 // between sun position, lighting, and view position.  This creates a
139 // valid single pass initialization path.
140 //
141 // Revision 1.1  1998/04/24 00:52:29  curt
142 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
143 // Fog color fixes.
144 // Separated out lighting calcs into their own file.
145 //
146 // Revision 1.12  1998/04/21 17:01:44  curt
147 // Fixed a problems where a pointer to a function was being passed around.  In
148 // one place this functions arguments were defined as ( void ) while in another
149 // place they were defined as ( int ).  The correct answer was ( int ).
150 //
151 // Prepairing for C++ integration.
152 //
153 // Revision 1.11  1998/04/03 22:12:56  curt
154 // Converting to Gnu autoconf system.
155 // Centralized time handling differences.
156 //
157 // Revision 1.10  1998/01/31 00:43:45  curt
158 // Added MetroWorks patches from Carmen Volpe.
159 //
160 // Revision 1.9  1998/01/19 19:27:21  curt
161 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
162 // This should simplify things tremendously.
163 //
164 // Revision 1.8  1998/01/19 18:40:39  curt
165 // Tons of little changes to clean up the code and to remove fatal errors
166 // when building with the c++ compiler.
167 //
168 // Revision 1.7  1997/12/30 13:06:58  curt
169 // A couple lighting tweaks ...
170 //
171 // Revision 1.6  1997/07/12 02:13:04  curt
172 // Add ftime() support for those that don't have gettimeofday()
173 //
174 // Revision 1.5  1997/06/26 19:08:38  curt
175 // Restructuring make, adding automatic "make dep" support.
176 //
177 // Revision 1.4  1997/06/25 15:39:49  curt
178 // Minor changes to compile with rsxnt/win32.
179 //
180 // Revision 1.3  1997/06/17 16:52:04  curt
181 // Timer interval stuff now uses gettimeofday() instead of ftime()
182 //
183 // Revision 1.2  1997/06/17 03:41:10  curt
184 // Nonsignal based interval timing is now working.
185 // This would be a good time to look at cleaning up the code structure a bit.
186 //
187 // Revision 1.1  1997/06/16 19:24:20  curt
188 // Initial revision.
189 //
190