]> git.mxchange.org Git - flightgear.git/blob - Time/fg_time.hxx
Converted "struct fg_timestamp" to "class fgTIMESTAMP" and added some
[flightgear.git] / Time / fg_time.hxx
1 //
2 // fg_time.hxx -- data structures and routines for managing time related stuff.
3 //
4 // Written by Curtis Olson, started August 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 #ifndef _FG_TIME_HXX
27 #define _FG_TIME_HXX
28
29
30 #ifndef __cplusplus                                                          
31 # error This library requires C++
32 #endif                                   
33
34
35 #ifdef HAVE_CONFIG_H
36 #  include <config.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #  include <windows.h>
41 #endif
42
43 #include <GL/glut.h>
44 #include <time.h>
45
46 #ifdef HAVE_SYS_TIMEB_H
47 #  include <sys/timeb.h> // for ftime() and struct timeb
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #  include <unistd.h>    // for gettimeofday()
51 #endif
52 #ifdef HAVE_SYS_TIME_H
53 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
54 #endif
55
56 #include <Flight/flight.hxx>
57
58
59 // Define a structure containing global time parameters
60 typedef struct {
61     // the date/time in various forms
62     // Unix "calendar" time in seconds
63     time_t cur_time;
64
65     // Break down of GMT time
66     struct tm *gmt;
67
68     // Julian date
69     double jd;
70
71     // modified Julian date
72     double mjd;
73
74     // side real time at prime meridian
75     double gst;
76
77     // local side real time
78     double lst;
79
80     // the difference between the precise sidereal time algorithm
81     // result and the course result.  
82     // course + diff has good accuracy for the short term
83     double gst_diff;
84
85     // An offset in seconds from the true time.  Allows us to adjust
86     // the effective time of day.
87     long int warp;
88
89     // How much to change the value of warp each iteration.  Allows us
90     // to make time progress faster than normal.
91     long int warp_delta; 
92
93     // Paused (0 = no, 1 = yes)
94     int pause;
95 } fgTIME;
96
97 extern fgTIME cur_time_params;
98
99
100 class fgTIMESTAMP {
101
102 private:
103
104     long seconds;
105     long millis;
106
107 public:
108
109     fgTIMESTAMP();
110     fgTIMESTAMP( const long s, const long m );
111     ~fgTIMESTAMP();
112
113     // Set time to current time
114     void stamp();
115
116     fgTIMESTAMP& operator = ( const fgTIMESTAMP& t );
117
118     friend fgTIMESTAMP operator + (const fgTIMESTAMP& t, const long& m);
119     friend long operator - (const fgTIMESTAMP& a, const fgTIMESTAMP& b);
120
121     inline long get_seconds() const { return seconds; }
122     inline long get_millis() const { return millis; }
123 };
124
125 inline fgTIMESTAMP::fgTIMESTAMP() {
126 }
127
128 inline fgTIMESTAMP::fgTIMESTAMP( const long s, const long m ) {
129     seconds = s;
130     millis = m;
131 }
132
133 inline fgTIMESTAMP::~fgTIMESTAMP() {
134 }
135
136 inline fgTIMESTAMP& fgTIMESTAMP::operator = (const fgTIMESTAMP& t)
137 {
138     seconds = t.seconds;
139     millis = t.millis;
140     return *this;
141 }
142
143 inline void fgTIMESTAMP::stamp() {
144 #if defined( WIN32 )
145     unsigned int t;
146     t = timeGetTime();
147     seconds = 0;
148     millis =  t;
149 #elif defined( HAVE_GETTIMEOFDAY )
150     struct timeval current;
151     struct timezone tz;
152     // fg_timestamp currtime;
153     gettimeofday(&current, &tz);
154     seconds = current.tv_sec;
155     millis = current.tv_usec / 1000;
156 #elif defined( HAVE_GETLOCALTIME )
157     SYSTEMTIME current;
158     GetLocalTime(&current);
159     seconds = current.wSecond;
160     millis = current.wMilliseconds;
161 #elif defined( HAVE_FTIME )
162     struct timeb current;
163     ftime(&current);
164     seconds = current.time;
165     millis = current.millitm;
166 #else
167 # error Port me
168 #endif
169 }
170
171 // difference between time stamps in milliseconds
172 inline fgTIMESTAMP operator + (const fgTIMESTAMP& t, const long& m) {
173 #ifdef WIN32
174     return fgTIMESTAMP( 0, t.millis + m );
175 #else
176     return fgTIMESTAMP( t.seconds + ( t.millis + m ) / 1000,
177                         ( t.millis + m ) % 1000 );
178 #endif
179 }
180
181 // difference between time stamps in milliseconds
182 inline long operator - (const fgTIMESTAMP& a, const fgTIMESTAMP& b)
183 {
184 #if defined( WIN32 )
185     return a.millis - b.millis;
186 #else
187     return 1000 * (a.seconds - b.seconds) + (a.millis - b.millis);
188 #endif
189 }
190
191 // Portability wrap to get current time.
192 // void timestamp(fg_timestamp *timestamp);
193
194
195 // Return duration in millis from first to last
196 // long timediff(fg_timestamp *first, fg_timestamp *last);
197
198
199 // Return new timestamp given a time stamp and an interval to add in
200 // void timesum(fg_timestamp *res, fg_timestamp *start, long millis);
201
202
203 // Update time variables such as gmt, julian date, and sidereal time
204 void fgTimeInit(fgTIME *t);
205
206
207 // Update the time dependent variables
208 void fgTimeUpdate(fgFLIGHT *f, fgTIME *t);
209
210
211 #endif // _FG_TIME_HXX
212
213
214 // $Log$
215 // Revision 1.9  1998/12/04 01:32:50  curt
216 // Converted "struct fg_timestamp" to "class fgTIMESTAMP" and added some
217 // convenience inline operators.
218 //
219 // Revision 1.8  1998/10/16 23:28:00  curt
220 // C++-ifying.
221 //
222 // Revision 1.7  1998/10/16 00:56:09  curt
223 // Converted to Point3D class.
224 //
225 // Revision 1.6  1998/07/27 18:42:22  curt
226 // Added a pause option.
227 //
228 // Revision 1.5  1998/05/22 21:14:54  curt
229 // Rewrote event.cxx in C++ as a class using STL for the internal event list
230 // and run queue this removes the arbitrary list sizes and makes things much
231 // more dynamic.  Because this is C++-classified we can now have multiple
232 // event_tables if we'd ever want them.
233 //
234 // Revision 1.4  1998/04/28 01:22:17  curt
235 // Type-ified fgTIME and fgVIEW.
236 //
237 // Revision 1.3  1998/04/25 22:06:34  curt
238 // Edited cvs log messages in source files ... bad bad bad!
239 //
240 // Revision 1.2  1998/04/25 20:24:03  curt
241 // Cleaned up initialization sequence to eliminate interdependencies
242 // between sun position, lighting, and view position.  This creates a
243 // valid single pass initialization path.
244 //
245 // Revision 1.1  1998/04/24 00:52:28  curt
246 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
247 // Fog color fixes.
248 // Separated out lighting calcs into their own file.
249 //
250 // Revision 1.20  1998/04/22 13:24:05  curt
251 // C++ - ifiing the code a bit.
252 // Starting to reorginize some of the lighting calcs to use a table lookup.
253 //
254 // Revision 1.19  1998/04/21 17:01:44  curt
255 // Fixed a problems where a pointer to a function was being passed around.  In
256 // one place this functions arguments were defined as ( void ) while in another
257 // place they were defined as ( int ).  The correct answer was ( int ).
258 //
259 // Prepairing for C++ integration.
260 //
261 // Revision 1.18  1998/04/08 23:35:40  curt
262 // Tweaks to Gnu automake/autoconf system.
263 //
264 // Revision 1.17  1998/04/03 22:12:56  curt
265 // Converting to Gnu autoconf system.
266 // Centralized time handling differences.
267 //
268 // Revision 1.16  1998/02/07 15:29:47  curt
269 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
270 // <chotchkiss@namg.us.anritsu.com>
271 //
272 // Revision 1.15  1998/01/27 00:48:06  curt
273 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
274 // system and commandline/config file processing code.
275 //
276 // Revision 1.14  1998/01/22 02:59:43  curt
277 // Changed #ifdef FILE_H to #ifdef _FILE_H
278 //
279 // Revision 1.13  1998/01/19 19:27:20  curt
280 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
281 // This should simplify things tremendously.
282 //
283 // Revision 1.12  1998/01/05 18:44:37  curt
284 // Add an option to advance/decrease time from keyboard.
285 //
286 // Revision 1.11  1997/12/19 23:35:07  curt
287 // Lot's of tweaking with sky rendering and lighting.
288 //
289 // Revision 1.10  1997/12/15 23:55:07  curt
290 // Add xgl wrappers for debugging.
291 // Generate terrain normals on the fly.
292 //
293 // Revision 1.9  1997/12/10 22:37:55  curt
294 // Prepended "fg" on the name of all global structures that didn't have it yet.
295 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
296 //
297 // Revision 1.8  1997/12/09 04:25:38  curt
298 // Working on adding a global lighting params structure.
299 //
300 // Revision 1.7  1997/11/25 19:25:41  curt
301 // Changes to integrate Durk's moon/sun code updates + clean up.
302 //
303 // Revision 1.6  1997/09/20 03:34:35  curt
304 // Still trying to get those durned stars aligned properly.
305 //
306 // Revision 1.5  1997/09/16 15:50:31  curt
307 // Working on star alignment and time issues.
308 //
309 // Revision 1.4  1997/09/13 02:00:08  curt
310 // Mostly working on stars and generating sidereal time for accurate star
311 // placement.
312 //
313 // Revision 1.3  1997/09/04 02:17:39  curt
314 // Shufflin' stuff.
315 //
316 // Revision 1.2  1997/08/27 03:30:36  curt
317 // Changed naming scheme of basic shared structures.
318 //
319 // Revision 1.1  1997/08/13 21:56:00  curt
320 // Initial revision.
321 //