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