]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
54ccd2a232706f45fe8f8fa8d7a25a1e86f718f2
[flightgear.git] / src / Cockpit / cockpit.cxx
1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/props/props.hxx>
37 #include <simgear/timing/sg_time.hxx>
38
39 #include <Include/general.hxx>
40
41 #include <Main/globals.hxx>
42 #include <Main/fg_props.hxx>
43 #include <Main/viewmgr.hxx>
44 #include <Main/viewer.hxx>
45 #include <Scenery/scenery.hxx>
46 #include <GUI/gui.h>
47
48 #include "cockpit.hxx"
49 #include "hud.hxx"
50
51 // The following routines obtain information concerntin the aircraft's
52 // current state and return it to calling instrument display routines.
53 // They should eventually be member functions of the aircraft.
54 //
55
56 float get_latitude( void )
57 {
58     return fgGetDouble("/position/latitude-deg");
59 }
60
61 float get_lat_min( void )
62 {
63     double a, d;
64
65     a = fgGetDouble("/position/latitude-deg");
66     if (a < 0.0) {
67         a = -a;
68     }
69     d = (double) ( (int) a);
70     float lat_min = (a - d) * 60.0;
71
72     return lat_min;
73 }
74
75
76 float get_longitude( void )
77 {
78     return fgGetDouble("/position/longitude-deg");
79 }
80
81
82 char*
83 get_formated_gmt_time( void )
84 {
85     static char buf[32];
86     const struct tm *p = globals->get_time_params()->getGmt();
87     sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
88          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
89          p->tm_hour, p->tm_min, p->tm_sec);
90
91     return buf;
92 }
93
94
95 float get_long_min( void )
96 {
97     double  a, d;
98     a = fgGetDouble("/position/longitude-deg");
99     if (a < 0.0) {
100         a = -a;
101     }
102     d = (double) ( (int) a);
103     float lon_min = (a - d) * 60.0;
104
105     return lon_min;
106 }
107
108 float get_throttleval( void )
109 {
110     // Hack limiting to one engine
111     return globals->get_controls()->get_throttle( 0 );
112 }
113
114 float get_aileronval( void )
115 {
116     return globals->get_controls()->get_aileron();
117 }
118
119 float get_elevatorval( void )
120 {
121     return globals->get_controls()->get_elevator();
122 }
123
124 float get_elev_trimval( void )
125 {
126     return globals->get_controls()->get_elevator_trim();
127 }
128
129 float get_rudderval( void )
130 {
131     return globals->get_controls()->get_rudder();
132 }
133
134 float get_speed( void )
135 {
136     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
137
138     float speed = fgGetDouble("/velocities/airspeed-kt")
139         * speedup_node->getIntValue();
140
141     return speed;
142 }
143
144 float get_mach(void)
145 {
146     return fgGetDouble("/velocities/mach");
147 }
148
149 float get_aoa( void )
150 {
151     return fgGetDouble("/orientation/alpha-deg");
152 }
153
154 float get_roll( void )
155 {
156     return fgGetDouble("/orientation/roll-deg") * SG_DEGREES_TO_RADIANS;
157 }
158
159 float get_pitch( void )
160 {
161     return fgGetDouble("/orientation/pitch-deg") * SG_DEGREES_TO_RADIANS;
162 }
163
164 float get_heading( void )
165 {
166     return fgGetDouble("/orientation/heading-deg");
167 }
168
169 float get_altitude( void )
170 {
171     static const SGPropertyNode *startup_units_node
172         = fgGetNode("/sim/startup/units");
173
174     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
175         return fgGetDouble("/position/altitude-ft");
176     } else {
177         return fgGetDouble("/position/altitude-ft") * SG_FEET_TO_METER;
178     }
179 }
180
181 float get_agl( void )
182 {
183     static const SGPropertyNode *startup_units_node
184         = fgGetNode("/sim/startup/units");
185
186     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
187         return fgGetDouble("/position/altitude-agl-ft");
188     } else {
189         return fgGetDouble("/position/altitude-agl-ft") * SG_FEET_TO_METER;
190     }
191 }
192
193 float get_sideslip( void )
194 {
195     return fgGetDouble("/orientation/side-slip-rad");
196 }
197
198 float get_frame_rate( void )
199 {
200     return general.get_frame_rate();
201 }
202
203 float get_fov( void )
204 {
205     return globals->get_current_view()->get_fov();
206 }
207
208 float get_vfc_ratio( void )
209 {
210     // float vfc = current_view.get_vfc_ratio();
211     // return (vfc);
212     return 0.0;
213 }
214
215 float get_vfc_tris_drawn   ( void )
216 {
217     // float rendered = current_view.get_tris_rendered();
218     // return (rendered);
219     return 0.0;
220 }
221
222 float get_vfc_tris_culled   ( void )
223 {
224     // float culled = current_view.get_tris_culled();
225     // return (culled);
226     return 0.0;
227 }
228
229 float get_climb_rate( void )
230 {
231     static const SGPropertyNode *startup_units_node
232         = fgGetNode("/sim/startup/units");
233
234     float climb_rate = fgGetDouble("/velocities/vertical-speed-fps", 0.0);
235     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
236         climb_rate *= 60.0;
237     } else {
238         climb_rate *= SG_FEET_TO_METER * 60.0;
239     }
240
241     return climb_rate;
242 }
243
244
245 float get_view_direction( void )
246 {
247     double view_off = 360.0 - globals->get_current_view()->getHeadingOffset_deg();
248     double view = SGMiscd::normalizeAngle(fgGetDouble("/orientation/heading-deg") + view_off);
249     return view;
250 }
251
252 // Added by Markus Hof on 5. Jan 2004
253 float get_dme( void )
254 {
255     static const SGPropertyNode * dme_node =
256         fgGetNode("/instrumentation/dme/indicated-distance-nm");
257
258     return dme_node->getFloatValue();
259 }
260
261 float get_Ax   ( void )
262 {
263     return fgGetDouble("/accelerations/ned/north-accel-fps_sec", 0.0);
264 }
265
266 float get_anzg   ( void )
267 {
268     return fgGetDouble("/accelerations/n-z-cg-fps_sec", 0.0);
269 }
270
271 #ifdef ENABLE_SP_FDM
272 float get_aux1 (void)
273 {
274     return fgGetDouble("/fdm-ada/ship-lat", 0.0);
275 }
276
277 float get_aux2 (void)
278 {
279     return fgGetDouble("/fdm-ada/ship-lon", 0.0);
280 }
281
282 float get_aux3 (void)
283 {
284     return fgGetDouble("/fdm-ada/ship-alt", 0.0);
285 }
286
287 float get_aux4 (void)
288 {
289     return fgGetDouble("/fdm-ada/skijump-dist", 0.0);
290 }
291
292 float get_aux5 (void)
293 {
294     return fgGetDouble("/fdm-ada/aux5", 0.0);
295 }
296
297 float get_aux6 (void)
298 {
299     return fgGetDouble("/fdm-ada/aux6", 0.0);
300 }
301
302 float get_aux7 (void)
303 {
304     return fgGetDouble("/fdm-ada/aux7", 0.0);
305 }
306
307 float get_aux8 (void)
308 {
309     return fgGetDouble("/fdm-ada/aux8", 0.0);}
310
311 float get_aux9 (void)
312 {
313     return fgGetDouble("/fdm-ada/aux9", 0.0);}
314
315 float get_aux10 (void)
316 {
317     return fgGetDouble("/fdm-ada/aux10", 0.0);
318 }
319
320 float get_aux11 (void)
321 {
322     return fgGetDouble("/fdm-ada/aux11", 0.0);
323 }
324
325 float get_aux12 (void)
326 {
327     return fgGetDouble("/fdm-ada/aux12", 0.0);
328 }
329
330 float get_aux13 (void)
331 {
332     return fgGetDouble("/fdm-ada/aux13", 0.0);
333 }
334
335 float get_aux14 (void)
336 {
337     return fgGetDouble("/fdm-ada/aux14", 0.0);
338 }
339
340 float get_aux15 (void)
341 {
342     return fgGetDouble("/fdm-ada/aux15", 0.0);
343 }
344
345 float get_aux16 (void)
346 {
347     return fgGetDouble("/fdm-ada/aux16", 0.0);
348 }
349
350 float get_aux17 (void)
351 {
352     return fgGetDouble("/fdm-ada/aux17", 0.0);
353 }
354
355 float get_aux18 (void)
356 {
357     return fgGetDouble("/fdm-ada/aux18", 0.0);
358 }
359 #endif
360
361
362 bool fgCockpitInit()
363 {
364     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
365
366     //  cockpit->code = 1;  /* It will be aircraft dependent */
367     //  cockpit->status = 0;
368
369     // If aircraft has HUD specified we will get the specs from its def
370     // file. For now we will depend upon hard coding in hud?
371
372     // We must insure that the existing instrument link is purged.
373     // This is done by deleting the links in the list.
374
375     // HI_Head is now a null pointer so we can generate a new list from the
376     // current aircraft.
377
378     fgHUDInit();
379
380     return true;
381 }
382
383 void fgCockpitUpdate( osg::State* state ) {
384
385     static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
386     static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
387     static const SGPropertyNode * hud_visibility_node
388         = fgGetNode("/sim/hud/visibility");
389
390     int iwidth   = xsize_node->getIntValue();
391     int iheight  = ysize_node->getIntValue();
392
393                                 // FIXME: inefficient
394     if ( hud_visibility_node->getBoolValue() ) {
395         // This will check the global hud linked list pointer.
396         // If there is anything to draw it will.
397         fgUpdateHUD( state );
398     }
399
400     glViewport( 0, 0, iwidth, iheight );
401 }
402
403
404
405
406
407 struct FuncTable {
408     const char *name;
409     FLTFNPTR func;
410 } fn_table[] = {
411     { "agl", get_agl },
412     { "aileronval", get_aileronval },
413     { "altitude", get_altitude },
414     { "anzg", get_anzg },
415     { "aoa", get_aoa },
416     { "ax", get_Ax },
417     { "climb", get_climb_rate },
418     { "elevatortrimval", get_elev_trimval },
419     { "elevatorval", get_elevatorval },
420     { "fov", get_fov },
421     { "framerate", get_frame_rate },
422     { "heading", get_heading },
423     { "latitude", get_latitude },
424     { "longitude", get_longitude },
425     { "mach", get_mach },
426     { "rudderval", get_rudderval },
427     { "speed", get_speed },
428     { "throttleval", get_throttleval },
429     { "view_direction", get_view_direction },
430     { "vfc_tris_culled", get_vfc_tris_culled },
431     { "vfc_tris_drawn", get_vfc_tris_drawn },
432 #ifdef ENABLE_SP_FDM
433     { "aux1", get_aux1 },
434     { "aux2", get_aux2 },
435     { "aux3", get_aux3 },
436     { "aux4", get_aux4 },
437     { "aux5", get_aux5 },
438     { "aux6", get_aux6 },
439     { "aux7", get_aux7 },
440     { "aux8", get_aux8 },
441     { "aux9", get_aux9 },
442     { "aux10", get_aux10 },
443     { "aux11", get_aux11 },
444     { "aux12", get_aux12 },
445     { "aux13", get_aux13 },
446     { "aux14", get_aux14 },
447     { "aux15", get_aux15 },
448     { "aux16", get_aux16 },
449     { "aux17", get_aux17 },
450     { "aux18", get_aux18 },
451 #endif
452     { 0, 0 },
453 };
454
455
456 FLTFNPTR get_func(const char *name)
457 {
458     for (int i = 0; fn_table[i].name; i++)
459         if (!strcmp(fn_table[i].name, name))
460             return fn_table[i].func;
461
462     return 0;
463 }
464
465