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