1 /**************************************************************************
2 * fg_timer.c -- time handling routines
4 * Written by Curtis Olson, started June 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
31 #include <signal.h> /* for timer routines */
32 #include <stdio.h> /* for printf() */
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h> /* for get/setitimer, gettimeofday, struct timeval */
38 #include "fg_time.hxx"
39 #include "fg_timer.hxx"
42 unsigned long int fgSimTime;
45 static struct itimerval t, ot;
46 static void (*callbackfunc)(int multi_loop);
49 /* This routine catches the SIGALRM */
50 void fgTimerCatch( int dummy ) {
53 // get past a compiler warning
54 warning_avoider = dummy;
56 /* ignore any SIGALRM's until we come back from our EOM iteration */
57 signal(SIGALRM, SIG_IGN);
59 /* printf("In fgTimerCatch()\n"); */
61 /* -1 tells the routine to use default interval rather than something
62 dynamically calculated based on frame rate */
65 signal(SIGALRM, fgTimerCatch);
69 /* this routine initializes the interval timer to generate a SIGALRM after
70 * the specified interval (dt) */
71 void fgTimerInit(float dt, void (*f)( int )) {
79 usec = 1000000 * ((int)dt - isec);
81 t.it_interval.tv_sec = isec;
82 t.it_interval.tv_usec = usec;
83 t.it_value.tv_sec = isec;
84 t.it_value.tv_usec = usec;
85 /* printf("fgTimerInit() called\n"); */
86 fgTimerCatch(0); /* set up for SIGALRM signal catch */
87 terr = setitimer( ITIMER_REAL, &t, &ot );
89 printf("Error returned from setitimer");
93 #endif /* HAVE_SETITIMER */
96 /* This function returns the number of milleseconds since the last
97 time it was called. */
98 int fgGetTimeInterval( void ) {
100 static int inited = 0;
101 static fg_timestamp last;
102 fg_timestamp current;
110 interval = timediff(&last, ¤t);
111 last.seconds = current.seconds;
112 last.millis = current.millis;
120 /* Revision 1.2 1998/04/25 20:24:03 curt
121 /* Cleaned up initialization sequence to eliminate interdependencies
122 /* between sun position, lighting, and view position. This creates a
123 /* valid single pass initialization path.
125 * Revision 1.1 1998/04/24 00:52:29 curt
126 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
128 * Separated out lighting calcs into their own file.
130 * Revision 1.12 1998/04/21 17:01:44 curt
131 * Fixed a problems where a pointer to a function was being passed around. In
132 * one place this functions arguments were defined as ( void ) while in another
133 * place they were defined as ( int ). The correct answer was ( int ).
135 * Prepairing for C++ integration.
137 * Revision 1.11 1998/04/03 22:12:56 curt
138 * Converting to Gnu autoconf system.
139 * Centralized time handling differences.
141 * Revision 1.10 1998/01/31 00:43:45 curt
142 * Added MetroWorks patches from Carmen Volpe.
144 * Revision 1.9 1998/01/19 19:27:21 curt
145 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
146 * This should simplify things tremendously.
148 * Revision 1.8 1998/01/19 18:40:39 curt
149 * Tons of little changes to clean up the code and to remove fatal errors
150 * when building with the c++ compiler.
152 * Revision 1.7 1997/12/30 13:06:58 curt
153 * A couple lighting tweaks ...
155 * Revision 1.6 1997/07/12 02:13:04 curt
156 * Add ftime() support for those that don't have gettimeofday()
158 * Revision 1.5 1997/06/26 19:08:38 curt
159 * Restructuring make, adding automatic "make dep" support.
161 * Revision 1.4 1997/06/25 15:39:49 curt
162 * Minor changes to compile with rsxnt/win32.
164 * Revision 1.3 1997/06/17 16:52:04 curt
165 * Timer interval stuff now uses gettimeofday() instead of ftime()
167 * Revision 1.2 1997/06/17 03:41:10 curt
168 * Nonsignal based interval timing is now working.
169 * This would be a good time to look at cleaning up the code structure a bit.
171 * Revision 1.1 1997/06/16 19:24:20 curt