]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.cxx
Fix my mailing address by replacing it with my web page.
[flightgear.git] / src / Cockpit / hud.cxx
1 // hud.cxx -- hud defines and prototypes
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - micheleamerica@geocities.com
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 #include <simgear/compiler.h>
24 #include <simgear/structure/exception.hxx>
25
26 #include STL_STRING
27 #include STL_FSTREAM
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #ifdef HAVE_WINDOWS_H
34 #  include <windows.h>
35 #endif
36
37 #ifdef __BORLANDC__
38 #  define exception c_exception
39 #endif
40
41 #include <math.h>
42
43 #include <stdlib.h>
44 #include <stdio.h>              // char related functions
45 #include <string.h>             // strcmp()
46
47 #include SG_GLU_H
48
49 #include <simgear/constants.h>
50 #include <simgear/debug/logstream.hxx>
51 #include <simgear/props/props.hxx>
52 #include <simgear/misc/sg_path.hxx>
53
54 #include <Aircraft/aircraft.hxx>
55 #include <Autopilot/xmlauto.hxx>
56 #include <GUI/gui.h>
57 #include <Main/globals.hxx>
58 #include <Main/fg_props.hxx>
59 #include <Scenery/scenery.hxx>
60
61 #if defined ( __sun__ ) || defined ( __sgi )
62 extern "C" {
63     extern void *memmove(void *, const void *, size_t);
64 }
65 #endif
66
67 #include "hud.hxx"
68
69 static char units[5];
70
71 // The following routines obtain information concerning the aircraft's
72 // current state and return it to calling instrument display routines.
73 // They should eventually be member functions of the aircraft.
74 //
75
76 deque< instr_item * > HUD_deque;
77
78 fgTextList         HUD_TextList;
79 fgLineList         HUD_LineList;
80 fgLineList         HUD_StippleLineList;
81
82 fntRenderer *HUDtext = 0;
83 float  HUD_TextSize = 0;
84 int HUD_style = 0;
85
86 float HUD_matrix[16];
87 static float hud_trans_alpha = 0.67f;
88
89
90 //$$$ begin - added, Neetha, 28 Nov 2k
91
92 static string   name;
93 static int              x;
94 static int              y;
95 static UINT    width;
96 static UINT    height;
97 static float    factor;
98 static float   span_units;
99 static float   division_units;
100 static float   minor_division = 0;
101 static UINT    screen_hole;
102 static UINT    lbl_pos;
103 static bool    working;
104 static string  loadfn;
105 static UINT     options;
106 static float   maxValue;
107 static float   minValue;
108 static float   scaling;
109 static UINT    major_divs;
110 static UINT    minor_divs;
111 static UINT    modulator;
112 static int     dp_showing = 0;
113 static string  label_format;
114 static string  prelabel;
115 static string  postlabel;
116 static int     justi;
117 static int              blinking;
118 static float   maxBankAngle;
119 static float   maxSlipAngle;
120 static UINT    gap_width;
121 static bool     latitude;
122 static bool     longitude;
123 static bool     tick_bottom;
124 static bool     tick_top;
125 static bool     tick_right;
126 static bool     tick_left;
127 static bool     cap_bottom;
128 static bool     cap_top;
129 static bool     cap_right;
130 static bool     cap_left;
131 static float   marker_off;
132 static string  type;
133 static bool    enable_pointer;
134 static string  type_pointer;
135 static bool    frl_spot;
136 static bool     target;
137 static bool    vel_vector;
138 static bool    drift;
139 static bool    alpha;
140 static bool     energy;
141 static bool     climb_dive;
142 static bool     glide;
143 static float    glide_slope_val;
144 static bool     worm_energy;
145 static bool     waypoint;
146 static string type_tick;//hud
147 static string length_tick;//hud
148 static bool label_box;//hud
149 static int digits; //suma
150 static float radius; //suma
151 static int divisions; //suma
152 static int zoom; //suma
153 static int zenith; //suma
154 static int nadir ; //suma
155 static int hat; //suma
156 static bool tsi; //suma
157 static float rad; //suma
158
159
160 static FLTFNPTR load_fn;    
161 static fgLabelJust justification;
162 static const char *pre_label_string  = 0;
163 static const char *post_label_string = 0;
164
165 int readHud( istream &input );
166 int readInstrument ( const SGPropertyNode * node);
167 static instr_item * readLadder ( const SGPropertyNode * node);
168 static instr_item * readCard ( const SGPropertyNode * node);
169 static instr_item * readLabel( const SGPropertyNode * node);
170 static instr_item * readTBI( const SGPropertyNode * node);
171 //$$$ end   - added, Neetha, 28 Nov 2k
172
173 static void drawHUD();
174 static void fgUpdateHUDVirtual();
175
176 void fgHUDalphaInit( void );
177
178 class locRECT {
179 public:
180     RECT rect;
181
182     locRECT( UINT left, UINT top, UINT right, UINT bottom);
183     RECT get_rect(void) { return rect;}
184 };
185
186 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
187 {
188     rect.left   =  left;
189     rect.top    =  top;
190     rect.right  =  right;
191     rect.bottom =  bottom;
192
193 }
194 // #define DEBUG
195
196 //========================= End of Class Implementations===================
197 // fgHUDInit
198 //
199 // Constructs a HUD object and then adds in instruments. At the present
200 // the instruments are hard coded into the routine. Ultimately these need
201 // to be defined by the aircraft's instrumentation records so that the
202 // display for a Piper Cub doesn't show the speed range of a North American
203 // mustange and the engine readouts of a B36!
204 //
205
206 #define INSTRDEFS 21
207
208 //$$$ begin - added, Neetha, 28 Nov 2k
209 static instr_item * 
210 readLadder(const SGPropertyNode * node)
211 {
212
213     instr_item *p;
214
215     name                        = node->getStringValue("name");
216     x                           = node->getIntValue("x");
217     y                           = node->getIntValue("y");
218     width                       = node->getIntValue("width");
219     height                      = node->getIntValue("height");
220     factor                      = node->getFloatValue("compression_factor");
221     span_units          = node->getFloatValue("span_units");
222     division_units      = node->getFloatValue("division_units");
223     screen_hole         = node->getIntValue("screen_hole");
224     lbl_pos                     = node->getIntValue("lbl_pos");
225     frl_spot            = node->getBoolValue("enable_frl",false);
226     target                      = node->getBoolValue("enable_target_spot",false);
227     vel_vector          = node->getBoolValue("enable_velocity_vector",false);
228     drift                       = node->getBoolValue("enable_drift_marker",false);
229     alpha                       = node->getBoolValue("enable_alpha_bracket",false);
230     energy                      = node->getBoolValue("enable_energy_marker",false);
231     climb_dive          = node->getBoolValue("enable_climb_dive_marker",false);
232     glide                       = node->getBoolValue("enable_glide_slope_marker",false);
233     glide_slope_val     = node->getFloatValue("glide_slope",-4.0);
234     worm_energy         = node->getBoolValue("enable_energy_marker",false);
235     waypoint            = node->getBoolValue("enable_waypoint_marker",false);
236     working                     = node->getBoolValue("working");
237     zenith                      = node->getIntValue("zenith");  //suma
238     nadir                       = node->getIntValue("nadir");  //suma
239     hat                         = node->getIntValue("hat");
240     // The factor assumes a base of 55 degrees per 640 pixels.
241     // Invert to convert the "compression" factor to a
242     // pixels-per-degree number.
243     if(fgGetBool("/sim/hud/enable3d", true))
244     {
245         if (HUD_style == 1)
246         {
247             factor = 1;
248             factor = (640./55.) / factor;
249         }
250     }
251
252     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
253         
254     p = (instr_item *) new HudLadder( name, x, y,
255                                       width, height, factor,
256                                       get_roll, get_pitch,
257                                       span_units, division_units, minor_division,
258                                       screen_hole, lbl_pos, frl_spot, target, vel_vector, 
259                                       drift, alpha, energy, climb_dive, 
260                                       glide, glide_slope_val, worm_energy, 
261                                       waypoint, working, zenith, nadir, hat);
262                                 
263     return p;
264                 
265 } //end readLadder
266
267 static instr_item * 
268 readCard(const SGPropertyNode * node)
269 {
270
271     instr_item *p;
272
273     name                        = node->getStringValue("name");
274     x                           = node->getIntValue("x");
275     y                           = node->getIntValue("y");
276     width                       = node->getIntValue("width");
277     height                      = node->getIntValue("height");
278     loadfn                      = node->getStringValue("loadfn");
279     options                     = node->getIntValue("options");
280     maxValue            = node->getFloatValue("maxValue");
281     minValue            = node->getFloatValue("minValue");
282     scaling                     = node->getFloatValue("disp_scaling");
283     major_divs          = node->getIntValue("major_divs");
284     minor_divs          = node->getIntValue("minor_divs");
285     modulator           = node->getIntValue("modulator");
286     span_units          = node->getFloatValue("value_span");
287     type                        = node->getStringValue("type");
288     tick_bottom     = node->getBoolValue("tick_bottom",false);
289     tick_top            = node->getBoolValue("tick_top",false);
290     tick_right          = node->getBoolValue("tick_right",false);
291     tick_left           = node->getBoolValue("tick_left",false);
292     cap_bottom          = node->getBoolValue("cap_bottom",false);
293     cap_top                     = node->getBoolValue("cap_top",false);
294     cap_right           = node->getBoolValue("cap_right",false);
295     cap_left            = node->getBoolValue("cap_left",false);
296     marker_off          = node->getFloatValue("marker_offset",0.0);
297     enable_pointer      = node->getBoolValue("enable_pointer",true);
298     type_pointer        = node->getStringValue("pointer_type");
299     type_tick           = node->getStringValue("tick_type");//hud Can be 'circle' or 'line'
300     length_tick         = node->getStringValue("tick_length");//hud For variable length
301     working                     = node->getBoolValue("working");
302     radius                      = node->getFloatValue("radius"); //suma
303     divisions           = node->getIntValue("divisions"); //suma
304     zoom                        = node->getIntValue("zoom"); //suma
305
306     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
307
308
309     if(type=="gauge") {
310         span_units = maxValue - minValue;
311     }
312
313     if (loadfn=="anzg") {
314         load_fn = get_anzg;
315     } else if (loadfn=="heading") {
316         load_fn = get_heading;
317     } else if (loadfn=="aoa") {
318         load_fn = get_aoa;
319     } else if (loadfn=="climb") {
320         load_fn = get_climb_rate;
321     } else if (loadfn=="altitude") {
322         load_fn = get_altitude;
323     } else if (loadfn=="agl") {
324         load_fn = get_agl;
325     } else if (loadfn=="speed") {
326         load_fn = get_speed;
327     } else if (loadfn=="view_direction") {
328         load_fn = get_view_direction;
329     } else if (loadfn=="aileronval") {
330         load_fn = get_aileronval;
331     } else if (loadfn=="elevatorval") {
332         load_fn = get_elevatorval;
333     } else if (loadfn=="elevatortrimval") {
334         load_fn = get_elev_trimval;
335     } else if (loadfn=="rudderval") {
336         load_fn = get_rudderval;
337     } else if (loadfn=="throttleval") {
338         load_fn = get_throttleval;
339     }
340
341
342     if ( (type == "dial") | (type == "tape") ) {
343         p = (instr_item *) new hud_card( x,
344                                          y,  
345                                          width,
346                                          height,
347                                          load_fn,
348                                          options,
349                                          maxValue, minValue,
350                                          scaling,
351                                          major_divs, minor_divs,
352                                          modulator,
353                                          dp_showing,
354                                          span_units,
355                                          type,
356                                          tick_bottom,
357                                          tick_top,
358                                          tick_right,
359                                          tick_left,
360                                          cap_bottom,
361                                          cap_top,
362                                          cap_right,
363                                          cap_left,
364                                          marker_off,
365                                          enable_pointer,
366                                          type_pointer,
367                                          type_tick,//hud
368                                          length_tick,//hud
369                                          working,
370                                          radius, //suma
371                                          divisions, //suma
372                                          zoom  //suma
373                                          );
374     } else {
375         p = (instr_item *) new  gauge_instr( x,            // x
376                                              y,  // y
377                                              width,            // width
378                                              height,            // height
379                                              load_fn, // data source
380                                              options,
381                                              scaling,
382                                              maxValue,minValue,
383                                              major_divs, minor_divs,
384                                              dp_showing,
385                                              modulator,
386                                              working);
387     }
388
389     return p;
390 }// end readCard
391
392 static instr_item *
393 readLabel(const SGPropertyNode * node)
394 {
395     instr_item *p;
396
397     int font_size = (fgGetInt("/sim/startup/xsize") > 1000) ? HUD_FONT_LARGE : HUD_FONT_SMALL;
398
399     name                = node->getStringValue("name");
400     x                   = node->getIntValue("x");
401     y                   = node->getIntValue("y");
402     width               = node->getIntValue("width");
403     height              = node->getIntValue("height");
404     loadfn              = node->getStringValue("data_source");
405     label_format        = node->getStringValue("label_format");
406     prelabel            = node->getStringValue("pre_label_string");
407     postlabel           = node->getStringValue("post_label_string");
408     scaling             = node->getFloatValue("scale_data");
409     options             = node->getIntValue("options");
410     justi               = node->getIntValue("justification");
411     blinking            = node->getIntValue("blinking");
412     latitude            = node->getBoolValue("latitude",false);
413     longitude           = node->getBoolValue("longitude",false);
414     label_box           = node->getBoolValue("label_box",false);//hud
415     working             = node->getBoolValue("working");
416     digits              = node->getIntValue("digits"); //suma
417
418
419     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
420
421
422     if ( justi == 0 ) {
423         justification = LEFT_JUST;
424     } else {
425         if ( justi == 1 ) {
426             justification = CENTER_JUST;
427         } else {
428             if ( justi == 2 ) {
429                 justification = RIGHT_JUST;
430             }
431         }
432     }
433
434     if ( prelabel == "NULL" ) {
435         pre_label_string = NULL;
436     } else {
437         if ( prelabel == "blank" ) {
438             pre_label_string = " ";
439         } else {
440             pre_label_string = prelabel.c_str();
441         }
442     }
443
444     if ( postlabel == "blank" ) {
445         post_label_string = " ";
446     } else {
447         if ( postlabel == "NULL" ) {
448             post_label_string = NULL;
449         } else {
450             if ( postlabel == "units" ) {
451                 post_label_string = units;
452             } else {
453                 post_label_string = postlabel.c_str();
454             }
455         }
456     }
457
458 #ifdef ENABLE_SP_FMDS
459     if ( loadfn== "aux1" ) {
460         load_fn = get_aux1;
461     } else if ( loadfn == "aux2" ) {
462         load_fn = get_aux2;
463     } else if ( loadfn == "aux3" ) {
464         load_fn = get_aux3;
465     } else if ( loadfn == "aux4" ) {
466         load_fn = get_aux4;
467     } else if ( loadfn == "aux5" ) {
468         load_fn = get_aux5;
469     } else if ( loadfn == "aux6" ) {
470         load_fn = get_aux6;
471     } else if ( loadfn == "aux7" ) {
472         load_fn = get_aux7;
473     } else if ( loadfn == "aux8" ) {
474         load_fn = get_aux8;
475     } else if ( loadfn == "aux9" ) {
476         load_fn = get_aux9;
477     } else if ( loadfn == "aux10" ) {
478         load_fn = get_aux10;
479     } else if ( loadfn == "aux11" ) {
480         load_fn = get_aux11;
481     } else if ( loadfn == "aux12" ) {
482         load_fn = get_aux12;
483     } else if ( loadfn == "aux13" ) {
484         load_fn = get_aux13;
485     } else if ( loadfn == "aux14" ) {
486         load_fn = get_aux14;
487     } else if ( loadfn == "aux15" ) {
488         load_fn = get_aux15;
489     } else if ( loadfn == "aux16" ) {
490         load_fn = get_aux16;
491     } else if ( loadfn == "aux17" ) {
492         load_fn = get_aux17;
493     } else if ( loadfn == "aux18" ) {
494         load_fn = get_aux18;
495     } else
496 #endif
497       if ( loadfn == "ax" ) {
498         load_fn = get_Ax;
499     } else if ( loadfn == "speed" ) {
500         load_fn = get_speed;
501     } else if ( loadfn == "mach" ) {
502         load_fn = get_mach;
503     } else if ( loadfn == "altitude" ) {
504         load_fn = get_altitude;
505     } else if ( loadfn == "agl" ) {
506         load_fn = get_agl;
507     } else if ( loadfn == "framerate" ) {
508         load_fn = get_frame_rate;
509     } else if ( loadfn == "heading" ) {
510         load_fn = get_heading;
511     } else if ( loadfn == "fov" ) {
512         load_fn = get_fov;
513     } else if ( loadfn == "vfc_tris_culled" ) {
514         load_fn = get_vfc_tris_culled;
515     } else if ( loadfn == "vfc_tris_drawn" ) {
516         load_fn = get_vfc_tris_drawn;
517     } else if ( loadfn == "aoa" ) {
518         load_fn = get_aoa;
519     } else if ( loadfn == "latitude" ) {
520         load_fn  = get_latitude;
521     } else if ( loadfn == "anzg" ) {
522         load_fn = get_anzg;
523     } else if ( loadfn == "longitude" ) {
524         load_fn   = get_longitude;
525     } else if (loadfn=="throttleval") {
526         load_fn = get_throttleval;
527     }
528
529     p = (instr_item *) new instr_label ( x,
530                                          y,
531                                          width,
532                                          height,
533                                          load_fn,
534                                          label_format.c_str(),
535                                          pre_label_string,
536                                          post_label_string,
537                                          scaling,
538                                          options,
539                                          justification,
540                                          font_size,
541                                          blinking,
542                                          latitude,
543                                          longitude,
544                                          label_box, //hud
545                                          working,
546                                          digits); //suma
547
548     return p;
549 } // end readLabel
550
551 static instr_item * 
552 readTBI(const SGPropertyNode * node)
553 {
554
555     instr_item *p;
556
557     name           = node->getStringValue("name");
558     x              = node->getIntValue("x");
559     y              = node->getIntValue("y");
560     width          = node->getIntValue("width");
561     height         = node->getIntValue("height");
562     maxBankAngle   = node->getFloatValue("maxBankAngle");
563     maxSlipAngle   = node->getFloatValue("maxSlipAngle");
564     gap_width      = node->getIntValue("gap_width");
565     working        = node->getBoolValue("working");
566     tsi                    = node->getBoolValue("tsi"); //suma
567     rad                    = node->getFloatValue("rad"); //suma
568
569     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
570
571
572     p = (instr_item *) new fgTBI_instr( x,
573                                         y,  
574                                         width,
575                                         height,
576                                         get_roll,
577                                         get_sideslip,
578                                         maxBankAngle, 
579                                         maxSlipAngle,
580                                         gap_width,
581                                         working,
582                                         tsi, //suma
583                                         rad); //suma
584
585     return p;
586 } //end readTBI
587
588 static instr_item * 
589 readRunway(const SGPropertyNode * node) {
590         name    = node->getStringValue("name");
591         x       = node->getIntValue("x");
592         y       = node->getIntValue("y");
593         width = node->getIntValue("width");
594         height = node->getIntValue("height");
595         scaling = node->getDoubleValue("scale");
596         working = node->getBoolValue("working",true);
597         runway_instr *ri = new runway_instr(x,y,width,height,scaling,working);
598         double scale = node->getDoubleValue("arrow_scale",1.0); 
599         ri->setDrawArrow((scale>0)?true:false);
600         ri->setDrawArrowAlways((scale>0)?node->getBoolValue("arrow_always"):false);
601         ri->setStippleOutline(node->getIntValue("outer_stipple",0xFFFF));
602         ri->setStippleCenterline(node->getIntValue("center_stipple",0xFFFF));
603         ri->setArrowRotationRadius(node->getDoubleValue("arrow_radius"));
604         ri->setArrowScale(scale);
605         ri->setLineScale(node->getDoubleValue("line_scale",1.0));
606         ri->setScaleDist(node->getDoubleValue("scale_dist_nm"));
607         SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
608         return (instr_item *) ri;
609 }
610
611
612 int readInstrument(const SGPropertyNode * node)
613 {
614     static const SGPropertyNode *startup_units_node
615         = fgGetNode("/sim/startup/units");
616
617     instr_item *HIptr;
618     
619     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
620         strcpy(units, " ft");
621     } else {
622         strcpy(units, " m");
623     }
624
625     const SGPropertyNode * ladder_group = node->getNode("ladders");
626
627     if (ladder_group != 0) {
628         int nLadders = ladder_group->nChildren();
629         for (int j = 0; j < nLadders; j++) {
630             
631             HIptr = readLadder(ladder_group->getChild(j));
632             HUD_deque.insert( HUD_deque.begin(), HIptr);
633                                         
634         }// for - ladders
635     }
636
637     const SGPropertyNode * card_group = node->getNode("cards");
638     if (card_group != 0) {
639         int nCards = card_group->nChildren();
640         for (int j = 0; j < nCards; j++) {
641             
642             HIptr = readCard(card_group->getChild(j));
643             HUD_deque.insert( HUD_deque.begin(), HIptr);
644
645         }//for - cards
646     }
647
648     const SGPropertyNode * label_group = node->getNode("labels");
649     if (label_group != 0) {
650         int nLabels = label_group->nChildren();
651         for (int j = 0; j < nLabels; j++) {
652
653             HIptr = readLabel(label_group->getChild(j));
654             HUD_deque.insert( HUD_deque.begin(), HIptr);
655
656         }//for - labels
657     }
658
659     const SGPropertyNode * tbi_group = node->getNode("tbis");
660     if (tbi_group != 0) {
661         int nTbis = tbi_group->nChildren();
662         for (int j = 0; j < nTbis; j++) {
663
664             HIptr = readTBI(tbi_group->getChild(j));
665             HUD_deque.insert( HUD_deque.begin(), HIptr);
666
667         }//for - tbis
668     }
669     
670     const SGPropertyNode * rwy_group = node->getNode("runways");
671     if (rwy_group != 0) {
672         int nRwy = rwy_group->nChildren();
673         for (int j = 0; j < nRwy; j++) {
674             SG_LOG( SG_COCKPIT, SG_DEBUG,
675                     "**************  Reading runway properties" );
676             HIptr = readRunway(rwy_group->getChild(j));
677             HUD_deque.insert( HUD_deque.begin(), HIptr);
678
679         }//for - runways
680     }    
681     return 0;
682 }//end readinstrument
683
684
685 int readHud( istream &input ) 
686 {
687
688     SGPropertyNode root;
689
690     try {
691         readProperties(input, &root);
692     } catch (const sg_exception &e) {
693         guiErrorMessage("Error reading HUD: ", e);
694         return 0;
695     }
696   
697         
698     SG_LOG(SG_INPUT, SG_INFO, "Read properties for  " <<
699            root.getStringValue("name"));
700
701
702     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());  // empty the HUD deque
703
704
705     SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
706
707     const SGPropertyNode * instrument_group = root.getChild("instruments");
708     int nInstruments = instrument_group->nChildren();
709
710     for (int i = 0; i < nInstruments; i++) {
711                 
712         const SGPropertyNode * node = instrument_group->getChild(i);
713
714         SGPath path( globals->get_fg_root() );
715         path.append(node->getStringValue("path"));
716
717         SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
718                << node->getName()
719                << " from "
720                << path.str());
721
722         SGPropertyNode root2;
723         try {
724             readProperties(path.str(), &root2);
725         } catch (const sg_exception &e) {
726             guiErrorMessage("Error reading HUD instrument: ", e);
727             continue;
728         } 
729         readInstrument(&root2);
730     }//for loop(i)
731
732     return 0;
733 }
734
735
736 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
737 {
738
739     HUD_style = 1;
740
741     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
742
743     string hud_path =
744         fgGetString("/sim/hud/path", "Huds/Default/default.xml");
745     SGPath path(globals->get_fg_root());
746     path.append(hud_path);
747         
748     ifstream input(path.c_str());
749     if (!input.good()) {
750         SG_LOG(SG_INPUT, SG_ALERT,
751                "Cannot read Hud configuration from " << path.str());
752     } else {
753         readHud(input);
754         input.close();
755     }
756
757     fgHUDalphaInit();
758     fgHUDReshape();
759
760     if ( HUDtext ) {
761         // this chunk of code is not necessarily thread safe if the
762         // compiler optimizer reorders these statements.  Note that
763         // "delete ptr" does not set "ptr = NULL".  We have to do that
764         // ourselves.
765         fntRenderer *tmp = HUDtext;
766         HUDtext = NULL;
767         delete tmp;
768     }
769
770 //    HUD_TextSize = fgGetInt("/sim/startup/xsize") / 60;
771     HUD_TextSize = 10;
772     HUDtext = new fntRenderer();
773     HUDtext -> setFont      ( guiFntHandle ) ;
774     HUDtext -> setPointSize ( HUD_TextSize ) ;
775     HUD_TextList.setFont( HUDtext );
776     
777     return 0;  // For now. Later we may use this for an error code.
778
779 }
780
781 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
782 {
783
784     HUD_style = 2;
785
786     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
787
788     SGPath path(globals->get_fg_root());
789     path.append("Huds/Minimal/default.xml");
790
791
792     ifstream input(path.c_str());
793     if (!input.good()) {
794         SG_LOG(SG_INPUT, SG_ALERT,
795                "Cannot read Hud configuration from " << path.str());
796     } else {
797         readHud(input);
798         input.close();
799     }
800
801     return 0;  // For now. Later we may use this for an error code.
802
803 }
804 //$$$ End - added, Neetha, 28 Nov 2k  
805
806 static int global_day_night_switch = HUD_DAY;
807
808 void HUD_masterswitch( bool incr )
809 {
810     if ( fgGetBool("/sim/hud/visibility") ) {
811         if ( global_day_night_switch == HUD_DAY ) {
812             global_day_night_switch = HUD_NIGHT;
813         } else {
814             fgSetBool("/sim/hud/visibility", false);
815         }
816     } else {
817         fgSetBool("/sim/hud/visibility", true);
818         global_day_night_switch = HUD_DAY;
819     }   
820 }
821
822 void HUD_brightkey( bool incr_bright )
823 {
824     instr_item *pHUDInstr = HUD_deque[0];
825     int brightness        = pHUDInstr->get_brightness();
826
827     if( fgGetBool("/sim/hud/visibility") ) {
828         if( incr_bright ) {
829             switch (brightness)
830                 {
831                 case HUD_BRT_LIGHT:
832                     brightness = HUD_BRT_BLACK;
833                     break;
834
835                 case HUD_BRT_MEDIUM:
836                     brightness = HUD_BRT_LIGHT;
837                     break;
838
839                 case HUD_BRT_DARK:
840                     brightness = HUD_BRT_MEDIUM;
841                     break;
842
843                 case HUD_BRT_BLACK:
844                     brightness = HUD_BRT_DARK;
845                     break;
846
847                 default:
848                     brightness = HUD_BRT_BLACK;
849                 }
850         } else {
851             switch (brightness)
852                 {
853                 case HUD_BRT_LIGHT:
854                     brightness = HUD_BRT_MEDIUM;
855                     break;
856
857                 case HUD_BRT_MEDIUM:
858                     brightness = HUD_BRT_DARK;
859                     break;
860
861                 case HUD_BRT_DARK:
862                     brightness = HUD_BRT_BLACK;
863                     break;
864
865                 case HUD_BRT_BLACK:
866                     brightness = HUD_BRT_LIGHT;
867                     break;
868
869                 default:
870                     fgSetBool("/sim/hud/visibility", false);
871                 }
872         }
873     } else {
874         fgSetBool("/sim/hud/visibility", true);
875     }
876
877     pHUDInstr->SetBrightness( brightness );
878 }
879
880
881 #define fgAP_CLAMP(val,min,max) ( (val) = (val) > (max) ? (max) : (val) < (min) ? (min) : (val) )
882
883 static puDialogBox *HUDalphaDialog;
884 static puText      *HUDalphaText;
885 static puSlider    *HUDalphaHS0;
886 //static puText      *HUDtextText;
887 //static puSlider    *HUDalphaHS1;
888 static char         SliderText[2][ 8 ];
889
890 static void alpha_adj( puObject *hs ) {
891     float val ;
892
893     hs-> getValue ( &val ) ;
894     fgAP_CLAMP ( val, 0.1, 1.0 ) ;
895     // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
896     hud_trans_alpha = val;
897     sprintf( SliderText[ 0 ], "%05.2f", hud_trans_alpha );
898     HUDalphaText -> setLabel ( SliderText[ 0 ] ) ;
899 }
900
901 void fgHUDalphaAdjust( puObject * ) {
902     fgSetBool("/sim/hud/antialiased", true);
903     FG_PUSH_PUI_DIALOG( HUDalphaDialog );
904 }
905
906 static void goAwayHUDalphaAdjust (puObject *)
907 {
908     FG_POP_PUI_DIALOG( HUDalphaDialog );
909 }
910
911 static void cancelHUDalphaAdjust (puObject *)
912 {
913     fgSetBool("/sim/hud/antialiased", false);
914     FG_POP_PUI_DIALOG( HUDalphaDialog );
915 }
916
917 // Done once at system initialization
918 void fgHUDalphaInit( void ) {
919
920     //  printf("fgHUDalphaInit\n");
921 #define HORIZONTAL  FALSE
922
923     int DialogX = 40;
924     int DialogY = 100;
925     int DialogWidth = 240;
926
927     char Label[] =  "HUD Adjuster";
928     char *s;
929
930     int labelX = (DialogWidth / 2) -
931         ( puGetDefaultLabelFont().getStringWidth( Label ) / 2);
932         
933     int nSliders = 1;
934     int slider_x = 10;
935     int slider_y = 55;
936     int slider_width = 220;
937     int slider_title_x = 15;
938     int slider_value_x = 160;
939     float slider_delta = 0.05f;
940
941     puFont HUDalphaLegendFont;
942     puFont HUDalphaLabelFont;
943     puGetDefaultFonts ( &HUDalphaLegendFont, &HUDalphaLabelFont );
944         
945     HUDalphaDialog = new puDialogBox ( DialogX, DialogY ); {
946         int horiz_slider_height = HUDalphaLabelFont.getStringHeight() +
947             HUDalphaLabelFont.getStringDescender() +
948             PUSTR_TGAP + PUSTR_BGAP + 5;
949
950         /* puFrame *
951            HUDalphaFrame = new puFrame ( 0, 0, DialogWidth,
952            85 + nSliders
953            * horiz_slider_height ); */
954
955         puText *
956             HUDalphaDialogMessage = new puText ( labelX,
957                                                  52 + nSliders
958                                                  * horiz_slider_height );
959         HUDalphaDialogMessage -> setDefaultValue ( Label );
960         HUDalphaDialogMessage -> getDefaultValue ( &s );
961         HUDalphaDialogMessage -> setLabel        ( s );
962
963         HUDalphaHS0 = new puSlider ( slider_x, slider_y,
964                                      slider_width, HORIZONTAL ) ;
965         HUDalphaHS0->     setDelta ( slider_delta ) ;
966         HUDalphaHS0->     setValue ( hud_trans_alpha ) ;
967         HUDalphaHS0->    setCBMode ( PUSLIDER_DELTA ) ;
968         HUDalphaHS0->  setCallback ( alpha_adj ) ;
969
970         puText *
971             HUDalphaTitle =      new puText ( slider_title_x, slider_y ) ;
972         HUDalphaTitle-> setDefaultValue ( "Alpha" ) ;
973         HUDalphaTitle-> getDefaultValue ( &s ) ;
974         HUDalphaTitle->        setLabel ( s ) ;
975                 
976         HUDalphaText = new puText ( slider_value_x, slider_y ) ;
977         sprintf( SliderText[ 0 ], "%05.2f", hud_trans_alpha );
978         HUDalphaText-> setLabel ( SliderText[ 0 ] ) ;
979
980         puOneShot *
981             HUDalphaOkButton =     new puOneShot ( 10, 10, 60, 45 );
982         HUDalphaOkButton->         setLegend ( gui_msg_OK );
983         HUDalphaOkButton-> makeReturnDefault ( TRUE );
984         HUDalphaOkButton->       setCallback ( goAwayHUDalphaAdjust );
985
986         puOneShot *
987             HUDalphaNoButton = new puOneShot ( 160, 10, 230, 45 );
988         HUDalphaNoButton->     setLegend ( gui_msg_CANCEL );
989         HUDalphaNoButton->   setCallback ( cancelHUDalphaAdjust );
990     }
991     FG_FINALIZE_PUI_DIALOG( HUDalphaDialog );
992
993 #undef HORIZONTAL
994 }
995
996
997 void fgHUDReshape(void) {
998 #if 0
999     if ( HUDtext ) {
1000         // this chunk of code is not necessarily thread safe if the
1001         // compiler optimizer reorders these statements.  Note that
1002         // "delete ptr" does not set "ptr = NULL".  We have to do that
1003         // ourselves.
1004         fntRenderer *tmp = HUDtext;
1005         HUDtext = NULL;
1006         delete tmp;
1007     }
1008
1009     HUD_TextSize = fgGetInt("/sim/startup/xsize") / 60;
1010     HUD_TextSize = 10;
1011     HUDtext = new fntRenderer();
1012     HUDtext -> setFont      ( guiFntHandle ) ;
1013     HUDtext -> setPointSize ( HUD_TextSize ) ;
1014     HUD_TextList.setFont( HUDtext );
1015 #endif
1016 }
1017
1018
1019 static void set_hud_color(float r, float g, float b) {
1020     fgGetBool("/sim/hud/antialiased") ?
1021         glColor4f(r,g,b,hud_trans_alpha) :
1022         glColor3f(r,g,b);
1023 }
1024
1025
1026 // fgUpdateHUD
1027 //
1028 // Performs a once around the list of calls to instruments installed in
1029 // the HUD object with requests for redraw. Kinda. It will when this is
1030 // all C++.
1031 //
1032 void fgUpdateHUD( void ) {
1033         
1034     static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
1035     if( HUD_style == 1 && enable3d_node->getBoolValue() )
1036     {
1037         fgUpdateHUDVirtual();
1038         return;
1039     }
1040     
1041     static const float normal_aspect = float(640) / float(480);
1042     // note: aspect_ratio is Y/X
1043     float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
1044     if( current_aspect > normal_aspect ) {
1045         float aspect_adjust = current_aspect / normal_aspect;
1046         float adjust = 320.0f*aspect_adjust - 320.0f;
1047         fgUpdateHUD( -adjust, 0.0f, 640.0f+adjust, 480.0f );
1048     } else {
1049         float aspect_adjust = normal_aspect / current_aspect;
1050         float adjust = 240.0f*aspect_adjust - 240.0f;
1051         fgUpdateHUD( 0.0f, -adjust, 640.0f, 480.0f+adjust );
1052     }
1053 }
1054
1055 void fgUpdateHUDVirtual()
1056 {
1057     FGViewer* view = globals->get_current_view();
1058
1059     // Standard fgfs projection, with essentially meaningless clip
1060     // planes (we'll map the whole HUD plane to z=-1)
1061     glMatrixMode(GL_PROJECTION);
1062     glPushMatrix();
1063     glLoadIdentity();
1064     gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
1065
1066     glMatrixMode(GL_MODELVIEW);
1067     glPushMatrix();
1068     glLoadIdentity();
1069   
1070     // Standard fgfs view direction computation
1071     float lookat[3];
1072     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
1073     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
1074     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
1075     if(fabs(lookat[1]) > 9999) lookat[1] = 9999; // FPU sanity
1076     gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
1077
1078     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
1079     // This is the default fgfs field of view, which the HUD files are
1080     // written to assume.
1081     float dx = 0.52056705; // tan(55/2)
1082     float dy = dx * 0.75;  // assumes 4:3 aspect ratio
1083     float m[16];
1084     m[0] = dx; m[4] =  0; m[ 8] = 0; m[12] = 0;
1085     m[1] =  0; m[5] = dy; m[ 9] = 0; m[13] = 0;
1086     m[2] =  0; m[6] =  0; m[10] = 1; m[14] = 0;
1087     m[3] =  0; m[7] =  0; m[11] = 0; m[15] = 1;
1088     glMultMatrixf(m);
1089
1090     // Convert the 640x480 "HUD standard" coordinate space to a square
1091     // about the origin in the range [-1:1] at depth of -1
1092     glScalef(1./320, 1./240, 1);
1093     glTranslatef(-320, -240, -1);
1094
1095     // Do the deed
1096     drawHUD();
1097
1098     // Clean up our mess
1099     glMatrixMode(GL_PROJECTION);
1100     glPopMatrix();
1101     glMatrixMode(GL_MODELVIEW);
1102     glPopMatrix();
1103 }
1104
1105 void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
1106                   GLfloat x_end, GLfloat y_end )
1107 {
1108     glMatrixMode(GL_PROJECTION);
1109     glPushMatrix();
1110     glLoadIdentity();
1111     gluOrtho2D(x_start, x_end, y_start, y_end);
1112
1113     glMatrixMode(GL_MODELVIEW);
1114     glPushMatrix();
1115     glLoadIdentity();
1116
1117     drawHUD();
1118
1119     glMatrixMode(GL_PROJECTION);
1120     glPopMatrix();
1121     glMatrixMode(GL_MODELVIEW);
1122     glPopMatrix();
1123 }
1124
1125 void drawHUD()
1126 {
1127     if( !HUD_deque.size() ) {  // Trust everyone, but ALWAYS cut the cards!
1128         return;
1129     }
1130
1131     HUD_TextList.erase();
1132     HUD_LineList.erase();
1133     // HUD_StippleLineList.erase();
1134   
1135     glDisable(GL_DEPTH_TEST);
1136     glDisable(GL_LIGHTING);
1137
1138     static const SGPropertyNode *antialiased_node
1139         = fgGetNode("/sim/hud/antialiased", true);
1140     static const SGPropertyNode *heading_enabled
1141         = fgGetNode("/autopilot/locks/heading", true);
1142     static const SGPropertyNode *altitude_enabled
1143         = fgGetNode("/autopilot/locks/altitude", true);
1144
1145     static char hud_hdg_text[256];
1146     static char hud_wp0_text[256];
1147     static char hud_wp1_text[256];
1148     static char hud_wp2_text[256];
1149     static char hud_alt_text[256];
1150
1151     if( antialiased_node->getBoolValue() ) {
1152         glEnable(GL_LINE_SMOOTH);
1153         //        glEnable(GL_BLEND);
1154         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1155         glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
1156         glLineWidth(2.0);
1157     } else {
1158         glLineWidth(1.0);
1159     }
1160
1161     if( global_day_night_switch == HUD_DAY) {
1162         switch (HUD_deque[0]->get_brightness())
1163             {
1164             case HUD_BRT_LIGHT:
1165                 set_hud_color (0.1f, 0.9f, 0.1f);
1166                 break;
1167
1168             case HUD_BRT_MEDIUM:
1169                 set_hud_color (0.1f, 0.7f, 0.0f);
1170                 break;
1171
1172             case HUD_BRT_DARK:
1173                 set_hud_color (0.0f, 0.6f, 0.0f);
1174                 break;
1175
1176             case HUD_BRT_BLACK:
1177                 set_hud_color( 0.0f, 0.0f, 0.0f);
1178                 break;
1179
1180             default:
1181                 set_hud_color (0.1f, 0.9f, 0.1f);
1182             }
1183     } else {
1184         if( global_day_night_switch == HUD_NIGHT) {
1185             switch (HUD_deque[0]->get_brightness())
1186                 {
1187                 case HUD_BRT_LIGHT:
1188                     set_hud_color (0.9f, 0.1f, 0.1f);
1189                     break;
1190
1191                 case HUD_BRT_MEDIUM:
1192                     set_hud_color (0.7f, 0.0f, 0.1f);
1193                     break;
1194
1195                 case HUD_BRT_DARK:
1196                     set_hud_color (0.6f, 0.0f, 0.0f);
1197                     break;
1198
1199                 case HUD_BRT_BLACK:
1200                     set_hud_color( 0.0f, 0.0f, 0.0f);
1201                     break;
1202
1203                 default:
1204                     set_hud_color (0.6f, 0.0f, 0.0f);
1205                 }
1206         } else {     // Just in case default
1207             set_hud_color (0.1f, 0.9f, 0.1f);
1208         }
1209     }
1210
1211     for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
1212
1213     HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
1214
1215
1216     int apY = 480 - 80;
1217     
1218     
1219     if (strcmp( heading_enabled->getStringValue(), "dg-heading-hold") == 0 ) {
1220         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
1221                   fgGetDouble("/autopilot/settings/heading-bug-deg") );
1222         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
1223         apY -= 15;
1224     } else if ( strcmp(heading_enabled->getStringValue(), "true-heading-hold") == 0 ) {
1225         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
1226                   fgGetDouble("/autopilot/settings/true-heading-deg") );
1227         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
1228         apY -= 15;
1229
1230         string wp0_id = fgGetString( "/autopilot/route-manager/wp[0]/id" );
1231         if ( wp0_id.length() > 0 ) {
1232             snprintf( hud_wp0_text, 256, "%5s %6.1fnm %s", wp0_id.c_str(), 
1233                       fgGetDouble( "/autopilot/route-manager/wp[0]/dist" ),
1234                       fgGetString( "/autopilot/route-manager/wp[0]/eta" ) );
1235             HUD_TextList.add( fgText( 40, apY, hud_wp0_text ) );
1236             apY -= 15;
1237         }
1238         string wp1_id = fgGetString( "/autopilot/route-manager/wp[1]/id" );
1239         if ( wp1_id.length() > 0 ) {
1240             snprintf( hud_wp1_text, 256, "%5s %6.1fnm %s", wp1_id.c_str(), 
1241                       fgGetDouble( "/autopilot/route-manager/wp[1]/dist" ),
1242                       fgGetString( "/autopilot/route-manager/wp[1]/eta" ) );
1243             HUD_TextList.add( fgText( 40, apY, hud_wp1_text ) );
1244             apY -= 15;
1245         }
1246         string wp2_id = fgGetString( "/autopilot/route-manager/wp-last/id" );
1247         if ( wp2_id.length() > 0 ) {
1248             snprintf( hud_wp2_text, 256, "%5s %6.1fnm %s", wp2_id.c_str(), 
1249                       fgGetDouble( "/autopilot/route-manager/wp-last/dist" ),
1250                       fgGetString( "/autopilot/route-manager/wp-last/eta" ) );
1251             HUD_TextList.add( fgText( 40, apY, hud_wp2_text ) );
1252             apY -= 15;
1253         }
1254     }
1255   
1256     if ( strcmp( altitude_enabled->getStringValue(), "altitude-hold" ) == 0 ) {
1257         snprintf( hud_alt_text, 256, "alt = %.0f\n",
1258                   fgGetDouble("/autopilot/settings/target-altitude-ft") );
1259         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
1260         apY -= 15;
1261     } else if ( strcmp( altitude_enabled->getStringValue(), "agl-hold" ) == 0 ){
1262         snprintf( hud_alt_text, 256, "agl = %.0f\n",
1263                   fgGetDouble("/autopilot/settings/target-agl-ft") );
1264         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
1265         apY -= 15;
1266     }
1267
1268     HUD_TextList.draw();
1269
1270     HUD_LineList.draw();
1271
1272     // glEnable(GL_LINE_STIPPLE);
1273     // glLineStipple( 1, 0x00FF );
1274     // HUD_StippleLineList.draw();
1275     // glDisable(GL_LINE_STIPPLE);
1276
1277     if( antialiased_node->getBoolValue() ) {
1278         // glDisable(GL_BLEND);
1279         glDisable(GL_LINE_SMOOTH);
1280         glLineWidth(1.0);
1281     }
1282
1283     glEnable(GL_DEPTH_TEST);
1284     glEnable(GL_LIGHTING);
1285 }
1286