]> git.mxchange.org Git - flightgear.git/blob - Time/fg_timer.cxx
Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
[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_SYS_TIME_H
35 #  include <sys/time.h>  /* for get/setitimer, gettimeofday, struct timeval */
36 #endif
37
38 #include "fg_time.hxx"
39 #include "fg_timer.hxx"
40
41
42 unsigned long int fgSimTime;
43
44 #ifdef HAVE_SETITIMER
45   static struct itimerval t, ot;
46   static void (*callbackfunc)(int multi_loop);
47
48
49 /* This routine catches the SIGALRM */
50 void fgTimerCatch( int dummy ) {
51     /* ignore any SIGALRM's until we come back from our EOM iteration */
52     signal(SIGALRM, SIG_IGN);
53
54     /* printf("In fgTimerCatch()\n"); */
55
56     /* -1 tells the routine to use default interval rather than something
57        dynamically calculated based on frame rate */
58     callbackfunc(-1); 
59
60     signal(SIGALRM, fgTimerCatch);
61 }
62
63
64 /* this routine initializes the interval timer to generate a SIGALRM after
65  * the specified interval (dt) */
66 void fgTimerInit(float dt, void (*f)( int )) {
67     int terr;
68     int isec;
69     int usec;
70
71     callbackfunc = f;
72
73     isec = (int) dt;
74     usec = 1000000 * ((int)dt - isec);
75
76     t.it_interval.tv_sec = isec;
77     t.it_interval.tv_usec = usec;
78     t.it_value.tv_sec = isec;
79     t.it_value.tv_usec = usec;
80     /* printf("fgTimerInit() called\n"); */
81     fgTimerCatch(0);   /* set up for SIGALRM signal catch */
82     terr = setitimer( ITIMER_REAL, &t, &ot );
83     if (terr) {
84         printf("Error returned from setitimer");
85         exit(0);
86     }
87 }
88 #endif /* HAVE_SETITIMER */
89
90
91 /* This function returns the number of milleseconds since the last
92    time it was called. */
93 int fgGetTimeInterval( void ) {
94     int interval;
95     static int inited = 0;
96     static fg_timestamp last;
97     fg_timestamp current;
98
99     if ( ! inited ) {
100         inited = 1;
101         timestamp(&last);
102         interval = 0;
103     } else {
104         timestamp(&current);
105         interval = timediff(&last, &current);
106         last.seconds = current.seconds;
107         last.millis = current.millis;
108     }
109
110     return(interval);
111 }
112
113
114 /* $Log$
115 /* Revision 1.1  1998/04/24 00:52:29  curt
116 /* Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
117 /* Fog color fixes.
118 /* Separated out lighting calcs into their own file.
119 /*
120  * Revision 1.12  1998/04/21 17:01:44  curt
121  * Fixed a problems where a pointer to a function was being passed around.  In
122  * one place this functions arguments were defined as ( void ) while in another
123  * place they were defined as ( int ).  The correct answer was ( int ).
124  *
125  * Prepairing for C++ integration.
126  *
127  * Revision 1.11  1998/04/03 22:12:56  curt
128  * Converting to Gnu autoconf system.
129  * Centralized time handling differences.
130  *
131  * Revision 1.10  1998/01/31 00:43:45  curt
132  * Added MetroWorks patches from Carmen Volpe.
133  *
134  * Revision 1.9  1998/01/19 19:27:21  curt
135  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
136  * This should simplify things tremendously.
137  *
138  * Revision 1.8  1998/01/19 18:40:39  curt
139  * Tons of little changes to clean up the code and to remove fatal errors
140  * when building with the c++ compiler.
141  *
142  * Revision 1.7  1997/12/30 13:06:58  curt
143  * A couple lighting tweaks ...
144  *
145  * Revision 1.6  1997/07/12 02:13:04  curt
146  * Add ftime() support for those that don't have gettimeofday()
147  *
148  * Revision 1.5  1997/06/26 19:08:38  curt
149  * Restructuring make, adding automatic "make dep" support.
150  *
151  * Revision 1.4  1997/06/25 15:39:49  curt
152  * Minor changes to compile with rsxnt/win32.
153  *
154  * Revision 1.3  1997/06/17 16:52:04  curt
155  * Timer interval stuff now uses gettimeofday() instead of ftime()
156  *
157  * Revision 1.2  1997/06/17 03:41:10  curt
158  * Nonsignal based interval timing is now working.
159  * This would be a good time to look at cleaning up the code structure a bit.
160  *
161  * Revision 1.1  1997/06/16 19:24:20  curt
162  * Initial revision.
163  *
164  */