]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
Set the format default to float instead of int.
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H          
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glu.h>
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <simgear/constants.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/polar3d.hxx>
41 #include <simgear/props/props.hxx>
42 #include <simgear/timing/sg_time.hxx>
43
44 #include <Aircraft/aircraft.hxx>
45 #include <Include/general.hxx>
46 #include <FDM/ADA.hxx>
47 #include <Main/globals.hxx>
48 #include <Main/fg_props.hxx>
49 #include <Main/viewmgr.hxx>
50 #include <Scenery/scenery.hxx>
51 #include <Time/fg_timer.hxx>
52 #include <GUI/gui.h>
53
54 #include "cockpit.hxx"
55 #include "hud.hxx"
56
57
58 // This is a structure that contains all data related to
59 // cockpit/panel/hud system
60
61 static pCockpit ac_cockpit;
62 // The following routines obtain information concerntin the aircraft's
63 // current state and return it to calling instrument display routines.
64 // They should eventually be member functions of the aircraft.
65 //
66
67 float get_latitude( void )
68 {
69     return current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
70 }
71
72 float get_lat_min( void )
73 {
74     double a, d;
75
76     a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;    
77     if (a < 0.0) {
78         a = -a;
79     }
80     d = (double) ( (int) a);
81     float lat_min = (a - d) * 60.0;
82
83     return lat_min;
84 }
85
86
87 float get_longitude( void )
88 {
89     return current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
90 }
91
92
93 char*
94 get_formated_gmt_time( void )
95 {
96     static char buf[32];
97     const struct tm *p = globals->get_time_params()->getGmt();
98     sprintf( buf, "%d/%d/%4d %d:%02d:%02d", 
99          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
100          p->tm_hour, p->tm_min, p->tm_sec);
101
102     return buf;
103 }
104
105
106 float get_long_min( void )
107 {
108     double  a, d;
109     a = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;   
110     if (a < 0.0) {
111         a = -a;
112     }
113     d = (double) ( (int) a);
114     float lon_min = (a - d) * 60.0; 
115
116     return lon_min;
117 }
118
119 float get_throttleval( void )
120 {
121     // Hack limiting to one engine
122     return globals->get_controls()->get_throttle( 0 );
123 }
124
125 float get_aileronval( void )
126 {
127     return globals->get_controls()->get_aileron();
128 }
129
130 float get_elevatorval( void )
131 {
132     return globals->get_controls()->get_elevator();
133 }
134
135 float get_elev_trimval( void )
136 {
137     return globals->get_controls()->get_elevator_trim();
138 }
139
140 float get_rudderval( void )
141 {
142     return globals->get_controls()->get_rudder();
143 }
144
145 float get_speed( void )
146 {
147     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
148
149     float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
150         * speedup_node->getIntValue();
151
152     return speed;
153 }
154
155 float get_mach(void)
156 {
157     return current_aircraft.fdm_state->get_Mach_number();
158 }       
159
160 float get_aoa( void )
161 {
162     return current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
163 }
164
165 float get_roll( void )
166 {
167     return current_aircraft.fdm_state->get_Phi();
168 }
169
170 float get_pitch( void )
171 {
172     return current_aircraft.fdm_state->get_Theta();
173 }
174
175 float get_heading( void )
176 {
177     return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES;
178 }
179
180 float get_altitude( void )
181 {
182     static const SGPropertyNode *startup_units_node
183         = fgGetNode("/sim/startup/units");
184
185     float altitude;
186
187     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
188         altitude = current_aircraft.fdm_state->get_Altitude();
189     } else {
190         altitude = (current_aircraft.fdm_state->get_Altitude()
191                     * SG_FEET_TO_METER);
192     }
193
194     return altitude;
195 }
196
197 float get_agl( void )
198 {
199     static const SGPropertyNode *startup_units_node
200         = fgGetNode("/sim/startup/units");
201
202     float agl;
203
204     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
205         agl = (current_aircraft.fdm_state->get_Altitude()
206                - globals->get_scenery()->get_cur_elev() * SG_METER_TO_FEET);
207     } else {
208         agl = (current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
209                - globals->get_scenery()->get_cur_elev());
210     }
211
212     return agl;
213 }
214
215 float get_sideslip( void )
216 {
217     return current_aircraft.fdm_state->get_Beta();
218 }
219
220 float get_frame_rate( void )
221 {
222     return general.get_frame_rate();
223 }
224
225 float get_fov( void )
226 {
227     return globals->get_current_view()->get_fov(); 
228 }
229
230 float get_vfc_ratio( void )
231 {
232     // float vfc = current_view.get_vfc_ratio();
233     // return (vfc);
234     return 0.0;
235 }
236
237 float get_vfc_tris_drawn   ( void )
238 {
239     // float rendered = current_view.get_tris_rendered();
240     // return (rendered);
241     return 0.0;
242 }
243
244 float get_vfc_tris_culled   ( void )
245 {
246     // float culled = current_view.get_tris_culled();
247     // return (culled);
248     return 0.0;
249 }
250
251 float get_climb_rate( void )
252 {
253     static const SGPropertyNode *startup_units_node
254         = fgGetNode("/sim/startup/units");
255
256     float climb_rate;
257     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
258         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
259     } else {
260         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
261     }
262
263     return climb_rate;
264 }
265
266
267 float get_view_direction( void )
268 {
269     double view_off = SGD_2PI - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
270     double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
271         * SGD_RADIANS_TO_DEGREES;
272     
273     if(view > 360.)
274         view -= 360.;
275     else if(view<0.)
276         view += 360.;
277     
278     return view;
279 }
280
281 // Added by Markus Hof on 5. Jan 2004
282 float get_dme( void )
283 {
284     static const SGPropertyNode * dme_node =
285         fgGetNode("/radios/dme/distance-nm");
286
287     return dme_node->getFloatValue();
288 }
289
290 // $$$ begin - added, VS Renganathan 13 Oct 2K
291 // #ifdef FIGHTER_HUD
292 float get_Vx   ( void )
293 {
294     // Curt dont comment this and return zero. - Ranga
295     // Please remove comments from get_V_..() function in flight.hxx
296     float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
297     return Vxx;
298 }
299
300 float get_Vy   ( void )
301 {
302     // Curt dont comment this and return zero. - Ranga
303     // Please remove comments from get_V_..() function in flight.hxx
304     float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
305     return Vyy;
306 }
307
308 float get_Vz   ( void )
309 {
310     // Curt dont comment this and return zero. - Ranga
311     // Please remove comments from get_V_..() function in flight.hxx
312     float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
313     return Vzz;
314 }
315
316 float get_Ax   ( void )
317 {
318     float Ax = current_aircraft.fdm_state->get_V_dot_north();
319     return Ax;
320 }
321
322 float get_Ay   ( void )
323 {
324     float Ay = current_aircraft.fdm_state->get_V_dot_east();
325     return Ay;
326 }
327
328 float get_Az   ( void )
329 {
330     float Az = current_aircraft.fdm_state->get_V_dot_down();
331     return Az;
332 }
333
334 float get_anzg   ( void )
335 {
336     float anzg = current_aircraft.fdm_state->get_N_Z_cg();
337     return anzg;
338 }
339
340 int get_iaux1 (void)
341 {
342     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
343     return fdm->get_iaux(1);
344 }
345
346 int get_iaux2 (void)
347 {
348     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
349     return fdm->get_iaux(2);
350 }
351
352 int get_iaux3 (void)
353 {
354     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
355     return fdm->get_iaux(3);
356 }
357
358 int get_iaux4 (void)
359 {
360     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
361     return fdm->get_iaux(4);
362 }
363
364 int get_iaux5 (void)
365 {
366     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
367     return fdm->get_iaux(5);
368 }
369
370 int get_iaux6 (void)
371 {
372     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
373     return fdm->get_iaux(6);
374 }
375
376 int get_iaux7 (void)
377 {
378     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
379     return fdm->get_iaux(7);
380 }
381
382 int get_iaux8 (void)
383 {
384     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
385     return fdm->get_iaux(8);
386 }
387
388 int get_iaux9 (void)
389 {
390     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
391     return fdm->get_iaux(9);
392 }
393
394 int get_iaux10 (void)
395 {
396     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
397     return fdm->get_iaux(10);
398 }
399
400 int get_iaux11 (void)
401 {
402     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
403     return fdm->get_iaux(11);
404 }
405
406 int get_iaux12 (void)
407 {
408      FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
409      return fdm->get_iaux(12);
410 }
411
412 float get_aux1 (void)
413 {
414     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
415     return fdm->get_daux(1);
416 }
417
418 float get_aux2 (void)
419 {
420     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
421     return fdm->get_daux(2);
422 }
423
424 float get_aux3 (void)
425 {
426     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
427     return fdm->get_daux(3);
428 }
429
430 float get_aux4 (void)
431 {
432     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
433     return fdm->get_daux(4);
434 }
435
436 float get_aux5 (void)
437 {
438     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
439     return fdm->get_daux(5);
440 }
441
442 float get_aux6 (void)
443 {
444     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
445     return fdm->get_daux(6);
446 }
447
448 float get_aux7 (void)
449 {
450     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
451     return fdm->get_daux(7);
452 }
453
454 float get_aux8 (void)
455 {
456     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
457     return fdm->get_daux(8);
458 }
459
460 float get_aux9 (void)
461 {
462     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
463     return fdm->get_faux(1);
464 }
465
466 float get_aux10 (void)
467 {
468     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
469     return fdm->get_faux(2);
470 }
471
472 float get_aux11 (void)
473 {
474     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
475     return fdm->get_faux(3);
476 }
477
478 float get_aux12 (void)
479 {
480     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
481     return fdm->get_faux(4);
482 }
483
484 float get_aux13 (void)
485 {
486     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
487     return fdm->get_faux(5);
488 }
489
490 float get_aux14 (void)
491 {
492     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
493     return fdm->get_faux(6);
494 }
495
496 float get_aux15 (void)
497 {
498     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
499     return fdm->get_faux(7);
500 }
501
502 float get_aux16 (void)
503 {
504     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
505     return fdm->get_faux(8);
506 }
507
508 float get_aux17 (void)
509 {
510     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
511     return fdm->get_faux(9);
512 }
513
514 float get_aux18 (void)
515 {
516     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
517     return fdm->get_faux(10);
518 }
519 // #endif
520 // $$$ end - added, VS Renganathan 13 Oct 2K
521
522
523 #ifdef NOT_USED
524 /****************************************************************************/
525 /* Convert degrees to dd mm'ss.s" (DMS-Format)                              */
526 /****************************************************************************/
527 char *dmshh_format(double degrees)
528 {
529     static char buf[16];    
530     int deg_part;
531     int min_part;
532     double sec_part;
533     
534     if (degrees < 0)
535       degrees = -degrees;
536
537     deg_part = degrees;
538     min_part = 60.0 * (degrees - deg_part);
539     sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
540
541     /* Round off hundredths */
542     if (sec_part + 0.005 >= 60.0)
543       sec_part -= 60.0, min_part += 1;
544     if (min_part >= 60)
545       min_part -= 60, deg_part += 1;
546
547     sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
548
549     return buf;
550 }
551 #endif // 0
552
553
554 /************************************************************************
555  Convert degrees to dd mm.mmm' (DMM-Format)
556  Description: Converts using a round-off factor tailored to the required
557  precision of the minutes field (three decimal places).  Round-off
558  prevents function from returning a minutes value of 60.
559
560  Input arguments: Coordinate value in decimal degrees
561
562 ************************************************************************/
563 static char *toDM(float dd)
564 {
565     static char  dm[16];
566     double tempdd;
567     double mn;
568     double sign = 1;
569     int deg;
570
571     if (dd < 0) {
572         sign = -1;
573     }
574     /* round for minutes expressed to three decimal places */
575     tempdd = fabs(dd) + (5.0E-4 / 60.0);
576     deg = (int)tempdd;
577     mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
578     deg *= (int)sign;
579     sprintf(dm, "%d*%06.3f", deg, mn);
580     return dm;
581 }
582
583
584 /************************************************************************
585  Convert degrees to dd mm'ss.s'' (DMS-Format)
586  Description: Converts using a round-off factor tailored to the required
587  precision of the seconds field (one decimal place).  Round-off
588  prevents function from returning a seconds value of 60.
589
590  Input arguments: Coordinate value in decimal degrees
591
592 ************************************************************************/
593 static char *toDMS(float dd)
594 {
595     static char  dms[16];
596     double tempdd, tempmin;
597     int deg;
598     int mn;
599     double sec;
600     double sign = 1;
601
602     if(dd < 0) {
603         sign = -1;
604     }
605     /* round up for seconds expressed to one decimal place */
606     tempdd = fabs(dd) + (0.05 / 3600.0);
607     deg = (int)tempdd;
608     tempmin =  (tempdd - (double)(deg)) * 60.0;
609     mn = (int)tempmin;
610     sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
611     deg *= (int)sign;
612     sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
613     return dms;
614 }
615
616
617 // Have to set the LatLon display type
618 //static char *(*fgLatLonFormat)(float) = toDM;
619 static char *(*fgLatLonFormat)(float);
620
621 char *coord_format_lat(float latitude)
622 {
623     static char buf[16];
624
625     sprintf(buf,"%s%c",
626 //      dmshh_format(latitude),
627 //      toDMS(latitude),
628 //      toDM(latitude),
629         fgLatLonFormat(latitude),           
630         latitude > 0 ? 'N' : 'S');
631     return buf;
632 }
633
634 char *coord_format_lon(float longitude)
635 {
636     static char buf[80];
637
638     sprintf(buf,"%s%c",
639 //      dmshh_format(longitude),
640 //      toDMS(longitude),
641 //      toDM(longitude),
642         fgLatLonFormat(longitude),
643         longitude > 0 ? 'E' : 'W');
644     return buf;
645 }
646
647 void fgLatLonFormatToggle( puObject *)
648 {
649     static int toggle = 0;
650
651     if ( toggle ) 
652         fgLatLonFormat = toDM;
653     else
654         fgLatLonFormat = toDMS;
655     
656     toggle = ~toggle;
657 }
658
659 #ifdef NOT_USED
660 char *coord_format_latlon(double latitude, double longitude)
661 {
662     static char buf[1024];
663
664     sprintf(buf,"%s%c %s%c",
665         dmshh_format(latitude),
666         latitude > 0 ? 'N' : 'S',
667         dmshh_format(longitude),
668         longitude > 0 ? 'E' : 'W');
669     return buf;
670 }
671 #endif
672
673
674 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
675 {
676     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
677
678     //  cockpit->code = 1;  /* It will be aircraft dependent */
679     //  cockpit->status = 0;
680
681     // If aircraft has HUD specified we will get the specs from its def
682     // file. For now we will depend upon hard coding in hud?
683     
684     // We must insure that the existing instrument link is purged.
685     // This is done by deleting the links in the list.
686     
687     // HI_Head is now a null pointer so we can generate a new list from the
688     // current aircraft.
689
690     fgHUDInit( cur_aircraft );
691     ac_cockpit = new fg_Cockpit();
692     
693     // Have to set the LatLon display type
694     fgLatLonFormat = toDM;
695     
696     SG_LOG( SG_COCKPIT, SG_INFO,
697         "  Code " << ac_cockpit->code() << " Status " 
698         << ac_cockpit->status() );
699
700         return true;
701 }
702
703 void fgCockpitUpdate( void ) {
704
705     SG_LOG( SG_COCKPIT, SG_DEBUG,
706             "Cockpit: code " << ac_cockpit->code() << " status " 
707             << ac_cockpit->status() );
708
709     static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
710     static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
711     static const SGPropertyNode * hud_visibility_node
712         = fgGetNode("/sim/hud/visibility");
713
714     int iwidth   = xsize_node->getIntValue();
715     int iheight  = ysize_node->getIntValue();
716     float width  = iwidth;
717     // float height = iheight;
718
719                                 // FIXME: inefficient
720     if ( hud_visibility_node->getBoolValue() ) {
721         // This will check the global hud linked list pointer.
722         // If these is anything to draw it will.
723         fgUpdateHUD();
724     }
725
726     if ( fgGetBool( "/sim/hud/draw-fps", false ) ) {
727         char buf[64];
728         float fps = get_frame_rate();
729         sprintf(buf,"%-5.1f", fps);
730
731         glMatrixMode( GL_PROJECTION );
732         glPushMatrix();
733         glLoadIdentity();
734         gluOrtho2D( 0, iwidth, 0, iheight );
735         glMatrixMode( GL_MODELVIEW );
736         glPushMatrix();
737         glLoadIdentity();
738
739         glDisable( GL_DEPTH_TEST );
740         glDisable( GL_LIGHTING );
741         
742         glColor3f( 0.9, 0.4, 0.2 );
743
744         guiFnt.drawString( buf,
745                            int(width - guiFnt.getStringWidth(buf) - 10),
746                            10 );
747         glEnable( GL_DEPTH_TEST );
748         glEnable( GL_LIGHTING );
749         glMatrixMode( GL_PROJECTION );
750         glPopMatrix();
751         glMatrixMode( GL_MODELVIEW );
752         glPopMatrix();
753     }
754     
755     glViewport( 0, 0, iwidth, iheight );
756 }
757