]> git.mxchange.org Git - flightgear.git/blob - Time/fg_timer.c
Minor changes to compile with rsxnt/win32.
[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 #include <sys/time.h>  /* for get/setitimer, gettimeofday, struct timeval */
30 #include <unistd.h>
31
32
33 #include "fg_timer.h"
34
35
36 unsigned long int fgSimTime;
37
38 #ifdef HAVE_ITIMER
39   static struct itimerval t, ot;
40   static void (*callbackfunc)(int multi_loop);
41
42
43 /* This routine catches the SIGALRM */
44 void fgTimerCatch() {
45     /* ignore any SIGALRM's until we come back from our EOM iteration */
46     signal(SIGALRM, SIG_IGN);
47
48     /* printf("In fgTimerCatch()\n"); */
49
50     /* -1 tells the routine to use default interval rather than something
51        dynamically calculated based on frame rate */
52     callbackfunc(-1); 
53
54     signal(SIGALRM, fgTimerCatch);
55 }
56
57
58 /* this routine initializes the interval timer to generate a SIGALRM after
59  * the specified interval (dt) */
60 void fgTimerInit(float dt, void (*f)()) {
61     int terr;
62     int isec;
63     float usec;
64
65     callbackfunc = f;
66
67     isec = (int) dt;
68     usec = 1000000* (dt - (float) isec);
69
70     t.it_interval.tv_sec = isec;
71     t.it_interval.tv_usec = usec;
72     t.it_value.tv_sec = isec;
73     t.it_value.tv_usec = usec;
74     /* printf("fgTimerInit() called\n"); */
75     fgTimerCatch();   /* set up for SIGALRM signal catch */
76     terr = setitimer( ITIMER_REAL, &t, &ot );
77     if (terr) {
78         printf("Error returned from setitimer");
79         exit(0);
80     }
81 }
82
83 #endif HAVE_ITIMER
84
85 /* This function returns the number of milleseconds since the last
86    time it was called. */
87 int fgGetTimeInterval() {
88     static struct timeval last;
89     static struct timeval current;
90     static struct timezone tz;
91     static int inited = 0;
92
93     int interval;
94
95     if ( ! inited ) {
96         inited = 1;
97         gettimeofday(&last, &tz);
98         interval = 0;
99     } else {
100         gettimeofday(&current, &tz);
101         interval = 1000000 * (current.tv_sec - last.tv_sec) + 
102             (current.tv_usec - last.tv_usec);
103         interval /= 1000;  /* convert back to milleseconds */
104         last = current;
105     }
106
107     return(interval);
108 }
109
110
111 /* $Log$
112 /* Revision 1.4  1997/06/25 15:39:49  curt
113 /* Minor changes to compile with rsxnt/win32.
114 /*
115  * Revision 1.3  1997/06/17 16:52:04  curt
116  * Timer interval stuff now uses gettimeofday() instead of ftime()
117  *
118  * Revision 1.2  1997/06/17 03:41:10  curt
119  * Nonsignal based interval timing is now working.
120  * This would be a good time to look at cleaning up the code structure a bit.
121  *
122  * Revision 1.1  1997/06/16 19:24:20  curt
123  * Initial revision.
124  *
125  */