]> git.mxchange.org Git - flightgear.git/blob - Time/fg_timer.c
Added MetroWorks patches from Carmen Volpe.
[flightgear.git] / Time / fg_timer.c
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 #include <signal.h>    /* for timer routines */
28 #include <stdio.h>     /* for printf() */
29
30 #ifdef USE_FTIME
31 #  include <sys/timeb.h> /* for ftime() and struct timeb */
32 #elif defined(__MWERKS__)
33 #  include <windows.h>  /* For Metrowerks environment */
34 #  include <winbase.h>  /* There is no ANSI/MSL time function that */
35                         /* contains milliseconds */
36 #else
37 #  include <sys/time.h>  /* for get/setitimer, gettimeofday, struct timeval */
38 #endif /* USE_FTIME */
39
40 #include <Time/fg_timer.h>
41
42
43 unsigned long int fgSimTime;
44
45 #ifdef USE_ITIMER
46   static struct itimerval t, ot;
47   static void (*callbackfunc)(int multi_loop);
48
49
50 /* This routine catches the SIGALRM */
51 void fgTimerCatch( void ) {
52     /* ignore any SIGALRM's until we come back from our EOM iteration */
53     signal(SIGALRM, SIG_IGN);
54
55     /* printf("In fgTimerCatch()\n"); */
56
57     /* -1 tells the routine to use default interval rather than something
58        dynamically calculated based on frame rate */
59     callbackfunc(-1); 
60
61     signal(SIGALRM, fgTimerCatch);
62 }
63
64
65 /* this routine initializes the interval timer to generate a SIGALRM after
66  * the specified interval (dt) */
67 void fgTimerInit(float dt, void (*f)()) {
68     int terr;
69     int isec;
70     float usec;
71
72     callbackfunc = f;
73
74     isec = (int) dt;
75     usec = 1000000* (dt - (float) isec);
76
77     t.it_interval.tv_sec = isec;
78     t.it_interval.tv_usec = usec;
79     t.it_value.tv_sec = isec;
80     t.it_value.tv_usec = usec;
81     /* printf("fgTimerInit() called\n"); */
82     fgTimerCatch();   /* set up for SIGALRM signal catch */
83     terr = setitimer( ITIMER_REAL, &t, &ot );
84     if (terr) {
85         printf("Error returned from setitimer");
86         exit(0);
87     }
88 }
89 #endif /* HAVE_ITIMER */
90
91
92 /* This function returns the number of milleseconds since the last
93    time it was called. */
94 int fgGetTimeInterval( void ) {
95     int interval;
96     static int inited = 0;
97
98 #ifdef USE_FTIME
99     static struct timeb last;
100     static struct timeb current;
101 #elif defined (__MWERKS__)
102     SYSTEMTIME last;
103     SYSTEMTIME current;
104 #else
105     static struct timeval last;
106     static struct timeval current;
107     static struct timezone tz;
108 #endif /* USE_FTIME */
109
110     if ( ! inited ) {
111         inited = 1;
112
113 #ifdef USE_FTIME
114         ftime(&last);
115 #elif defined (__MWERKS__)
116         GetLocalTime(&last);
117 #else
118         gettimeofday(&last, &tz);
119 #endif /* USE_FTIME */
120
121         interval = 0;
122     } else {
123
124 #ifdef USE_FTIME
125         ftime(&current);
126         interval = 1000 * (current.time - last.time) + 
127             (current.millitm - last.millitm);
128 #elif defined (__MWERKS__)
129         GetLocalTime(&current);
130         interval = 1000 * (current.wSecond - last.wSecond) + 
131             (current.wMilliseconds - last.wMilliseconds);
132 #else
133         gettimeofday(&current, &tz);
134         interval = 1000000 * (current.tv_sec - last.tv_sec) + 
135             (current.tv_usec - last.tv_usec);
136         interval /= 1000;  /* convert back to milleseconds */
137 #endif /* USE_FTIME */
138
139         last = current;
140     }
141
142     return(interval);
143 }
144
145
146 /* $Log$
147 /* Revision 1.10  1998/01/31 00:43:45  curt
148 /* Added MetroWorks patches from Carmen Volpe.
149 /*
150  * Revision 1.9  1998/01/19 19:27:21  curt
151  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
152  * This should simplify things tremendously.
153  *
154  * Revision 1.8  1998/01/19 18:40:39  curt
155  * Tons of little changes to clean up the code and to remove fatal errors
156  * when building with the c++ compiler.
157  *
158  * Revision 1.7  1997/12/30 13:06:58  curt
159  * A couple lighting tweaks ...
160  *
161  * Revision 1.6  1997/07/12 02:13:04  curt
162  * Add ftime() support for those that don't have gettimeofday()
163  *
164  * Revision 1.5  1997/06/26 19:08:38  curt
165  * Restructuring make, adding automatic "make dep" support.
166  *
167  * Revision 1.4  1997/06/25 15:39:49  curt
168  * Minor changes to compile with rsxnt/win32.
169  *
170  * Revision 1.3  1997/06/17 16:52:04  curt
171  * Timer interval stuff now uses gettimeofday() instead of ftime()
172  *
173  * Revision 1.2  1997/06/17 03:41:10  curt
174  * Nonsignal based interval timing is now working.
175  * This would be a good time to look at cleaning up the code structure a bit.
176  *
177  * Revision 1.1  1997/06/16 19:24:20  curt
178  * Initial revision.
179  *
180  */