]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.cxx
Update from JSBSim
[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/misc/exception.hxx>
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #ifdef HAVE_WINDOWS_H
31 #  include <windows.h>
32 #endif
33
34 #ifdef __BORLANDC__
35 #  define exception c_exception
36 #endif
37 #include <math.h>
38
39 #include <GL/glut.h>
40 #include <stdlib.h>
41 #include STL_STRING
42 #include STL_FSTREAM
43
44 #include <simgear/constants.h>
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/misc/props.hxx>
47 //#include <simgear/math/fg_random.h>
48 //#include <simgear/math/polar3d.hxx>
49
50 #include <Aircraft/aircraft.hxx>
51 #include <Autopilot/newauto.hxx>
52 #include <GUI/gui.h>
53 #include <Main/globals.hxx>
54 #include <Main/fg_props.hxx>
55 #ifdef FG_NETWORK_OLK
56 #include <NetworkOLK/network.h>
57 #endif
58 #include <Scenery/scenery.hxx>
59 //#include <Time/fg_timer.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 void fgHUDalphaInit( void );
174
175 class locRECT {
176 public:
177     RECT rect;
178
179     locRECT( UINT left, UINT top, UINT right, UINT bottom);
180     RECT get_rect(void) { return rect;}
181 };
182
183 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
184 {
185     rect.left   =  left;
186     rect.top    =  top;
187     rect.right  =  right;
188     rect.bottom =  bottom;
189
190 }
191 // #define DEBUG
192
193 #ifdef OLD_CODE
194 void drawOneLine( UINT x1, UINT y1, UINT x2, UINT y2)
195 {
196     glBegin(GL_LINES);
197     glVertex2f(x1, y1);
198     glVertex2f(x2, y2);
199     glEnd();
200 }
201
202 void drawOneLine( RECT &rect)
203 {
204     glBegin(GL_LINES);
205     glVertex2f(rect.left, rect.top);
206     glVertex2f(rect.right, rect.bottom);
207     glEnd();
208 }
209
210 //
211 // The following code deals with painting the "instrument" on the display
212 //
213 /* textString - Bitmap font string */
214
215 void textString( int x, int y, char *msg, void *font,int digit) //suma
216 {
217
218     if(*msg) {
219         //      puDrawString (  NULL, msg, x, y );
220         glRasterPos2f(x, y);
221         while (*msg) {
222             glutBitmapCharacter(font, *msg);
223             msg++;
224         }
225     }
226 }
227
228
229 /* strokeString - Stroke font string */
230 void strokeString(int x, int y, char *msg, void *font, float theta)
231 {
232     int xx;
233     int yy;
234     int c;
235     float sintheta,costheta;
236     
237
238     if(*msg) {
239         glPushMatrix();
240         glRotatef(theta * SGD_RADIANS_TO_DEGREES, 0.0, 0.0, 1.0);
241         sintheta = sin(theta);
242         costheta = cos(theta);
243         xx = (int)(x * costheta + y * sintheta);
244         yy = (int)(y * costheta - x * sintheta);
245         glTranslatef( xx, yy, 0);
246         glScalef(.1, .1, 0.0);
247         while( (c=*msg++) ) {
248             glutStrokeCharacter(font, c);
249         }
250         glPopMatrix();
251     }
252 }
253
254 int getStringWidth ( char *str )
255 {
256     if ( HUDtext && str ) {
257         float r, l ;
258         guiFntHandle->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
259         return FloatToInt( r - l );
260     }
261     return 0 ;
262 }
263 #endif // OLD_CODE
264
265 //========================= End of Class Implementations===================
266 // fgHUDInit
267 //
268 // Constructs a HUD object and then adds in instruments. At the present
269 // the instruments are hard coded into the routine. Ultimately these need
270 // to be defined by the aircraft's instrumentation records so that the
271 // display for a Piper Cub doesn't show the speed range of a North American
272 // mustange and the engine readouts of a B36!
273 //
274
275 #define INSTRDEFS 21
276
277 //$$$ begin - added, Neetha, 28 Nov 2k
278 static instr_item * 
279 readLadder(const SGPropertyNode * node)
280 {
281
282     instr_item *p;
283
284     name                        = node->getStringValue("name");
285     x                           = node->getIntValue("x");
286     y                           = node->getIntValue("y");
287     width                       = node->getIntValue("width");
288     height                      = node->getIntValue("height");
289     factor                      = node->getFloatValue("compression_factor");
290     span_units          = node->getFloatValue("span_units");
291     division_units      = node->getFloatValue("division_units");
292     screen_hole         = node->getIntValue("screen_hole");
293     lbl_pos                     = node->getIntValue("lbl_pos");
294     frl_spot            = node->getBoolValue("enable_frl",false);
295     target                      = node->getBoolValue("enable_target_spot",false);
296     vel_vector          = node->getBoolValue("enable_velocity_vector",false);
297     drift                       = node->getBoolValue("enable_drift_marker",false);
298     alpha                       = node->getBoolValue("enable_alpha_bracket",false);
299     energy                      = node->getBoolValue("enable_energy_marker",false);
300     climb_dive          = node->getBoolValue("enable_climb_dive_marker",false);
301     glide                       = node->getBoolValue("enable_glide_slope_marker",false);
302     glide_slope_val     = node->getFloatValue("glide_slope",-4.0);
303     worm_energy         = node->getBoolValue("enable_energy_marker",false);
304     waypoint            = node->getBoolValue("enable_waypoint_marker",false);
305     working                     = node->getBoolValue("working");
306     zenith                      = node->getIntValue("zenith");  //suma
307     nadir                       = node->getIntValue("nadir");  //suma
308     hat                         = node->getIntValue("hat");
309
310
311     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
312         
313                                 
314     p = (instr_item *) new HudLadder( name, x, y,
315                                       width, height, factor,
316                                       get_roll, get_pitch,
317                                       span_units, division_units, minor_division,
318                                       screen_hole, lbl_pos, frl_spot, target, vel_vector, 
319                                       drift, alpha, energy, climb_dive, 
320                                       glide, glide_slope_val, worm_energy, 
321                                       waypoint, working, zenith, nadir, hat);
322                                 
323     return p;
324                 
325 } //end readLadder
326
327 static instr_item * 
328 readCard(const SGPropertyNode * node)
329 {
330
331     instr_item *p;
332
333     name                        = node->getStringValue("name");
334     x                           = node->getIntValue("x");
335     y                           = node->getIntValue("y");
336     width                       = node->getIntValue("width");
337     height                      = node->getIntValue("height");
338     loadfn                      = node->getStringValue("loadfn");
339     options                     = node->getIntValue("options");
340     maxValue            = node->getFloatValue("maxValue");
341     minValue            = node->getFloatValue("minValue");
342     scaling                     = node->getFloatValue("disp_scaling");
343     major_divs          = node->getIntValue("major_divs");
344     minor_divs          = node->getIntValue("minor_divs");
345     modulator           = node->getIntValue("modulator");
346     span_units          = node->getFloatValue("value_span");
347     type                        = node->getStringValue("type");
348     tick_bottom     = node->getBoolValue("tick_bottom",false);
349     tick_top            = node->getBoolValue("tick_top",false);
350     tick_right          = node->getBoolValue("tick_right",false);
351     tick_left           = node->getBoolValue("tick_left",false);
352     cap_bottom          = node->getBoolValue("cap_bottom",false);
353     cap_top                     = node->getBoolValue("cap_top",false);
354     cap_right           = node->getBoolValue("cap_right",false);
355     cap_left            = node->getBoolValue("cap_left",false);
356     marker_off          = node->getFloatValue("marker_offset",0.0);
357     enable_pointer      = node->getBoolValue("enable_pointer",true);
358     type_pointer        = node->getStringValue("pointer_type");
359     type_tick           = node->getStringValue("tick_type");//hud Can be 'circle' or 'line'
360     length_tick         = node->getStringValue("tick_length");//hud For variable length
361     working                     = node->getBoolValue("working");
362     radius                      = node->getFloatValue("radius"); //suma
363     divisions           = node->getIntValue("divisions"); //suma
364     zoom                        = node->getIntValue("zoom"); //suma
365
366     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
367
368
369     if(type=="gauge") {
370         span_units = maxValue - minValue;
371     }
372
373     if (loadfn=="anzg") {
374         load_fn = get_anzg;
375     } else if (loadfn=="heading") {
376         load_fn = get_heading;
377     } else if (loadfn=="aoa") {
378         load_fn = get_aoa;
379     } else if (loadfn=="climb") {
380         load_fn = get_climb_rate;
381     } else if (loadfn=="altitude") {
382         load_fn = get_altitude;
383     } else if (loadfn=="agl") {
384         load_fn = get_agl;
385     } else if (loadfn=="speed") {
386         load_fn = get_speed;
387     } else if (loadfn=="view_direction") {
388         load_fn = get_view_direction;
389     } else if (loadfn=="aileronval") {
390         load_fn = get_aileronval;
391     } else if (loadfn=="elevatorval") {
392         load_fn = get_elevatorval;
393     } else if (loadfn=="rudderval") {
394         load_fn = get_rudderval;
395     } else if (loadfn=="throttleval") {
396         load_fn = get_throttleval;
397     }
398
399
400     if ( (type == "dial") | (type == "tape") ) {
401         p = (instr_item *) new hud_card( x,
402                                          y,  
403                                          width,
404                                          height,
405                                          load_fn,
406                                          options,
407                                          maxValue, minValue,
408                                          scaling,
409                                          major_divs, minor_divs,
410                                          modulator,
411                                          dp_showing,
412                                          span_units,
413                                          type,
414                                          tick_bottom,
415                                          tick_top,
416                                          tick_right,
417                                          tick_left,
418                                          cap_bottom,
419                                          cap_top,
420                                          cap_right,
421                                          cap_left,
422                                          marker_off,
423                                          enable_pointer,
424                                          type_pointer,
425                                          type_tick,//hud
426                                          length_tick,//hud
427                                          working,
428                                          radius, //suma
429                                          divisions, //suma
430                                          zoom  //suma
431                                          );
432     } else {
433         p = (instr_item *) new  gauge_instr( x,            // x
434                                              y,  // y
435                                              width,            // width
436                                              height,            // height
437                                              load_fn, // data source
438                                              options,
439                                              scaling,
440                                              maxValue,minValue,
441                                              major_divs, minor_divs,
442                                              dp_showing,
443                                              modulator,
444                                              working);
445     }
446
447     return p;
448 }// end readCard
449
450 static instr_item *
451 readLabel(const SGPropertyNode * node)
452 {
453     instr_item *p;
454
455     int font_size = (fgGetInt("/sim/startup/xsize") > 1000) ? LARGE : SMALL;
456
457     name                = node->getStringValue("name");
458     x                   = node->getIntValue("x");
459     y                   = node->getIntValue("y");
460     width               = node->getIntValue("width");
461     height              = node->getIntValue("height");
462     loadfn              = node->getStringValue("data_source");
463     label_format        = node->getStringValue("label_format");
464     prelabel            = node->getStringValue("pre_label_string");
465     postlabel           = node->getStringValue("post_label_string");
466     scaling             = node->getFloatValue("scale_data");
467     options             = node->getIntValue("options");
468     justi               = node->getIntValue("justification");
469     blinking            = node->getIntValue("blinking");
470     latitude            = node->getBoolValue("latitude",false);
471     longitude           = node->getBoolValue("longitude",false);
472     label_box           = node->getBoolValue("label_box",false);//hud
473     working             = node->getBoolValue("working");
474     digits              = node->getIntValue("digits"); //suma
475
476
477     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
478
479
480     if ( justi == 0 ) {
481         justification = LEFT_JUST;
482     } else {
483         if ( justi == 1 ) {
484             justification = CENTER_JUST;
485         } else {
486             if ( justi == 2 ) {
487                 justification = RIGHT_JUST;
488             }
489         }
490     }
491
492     if ( prelabel == "NULL" ) {
493         pre_label_string = NULL;
494     } else {
495         if ( prelabel == "blank" ) {
496             pre_label_string = " ";
497         } else {
498             pre_label_string = prelabel.c_str();
499         }
500     }
501
502     if ( postlabel == "blank" ) {
503         post_label_string = " ";
504     } else {
505         if ( postlabel == "NULL" ) {
506             post_label_string = NULL;
507         } else {
508             if ( postlabel == "units" ) {
509                 post_label_string = units;
510             } else {
511                 post_label_string = postlabel.c_str();
512             }
513         }
514     }
515
516     if ( loadfn== "aux1" ) {
517         load_fn = get_aux1;
518     } else if ( loadfn == "aux2" ) {
519         load_fn = get_aux2;
520     } else if ( loadfn == "aux3" ) {
521         load_fn = get_aux3;
522     } else if ( loadfn == "aux4" ) {
523         load_fn = get_aux4;
524     } else if ( loadfn == "aux5" ) {
525         load_fn = get_aux5;
526     } else if ( loadfn == "aux6" ) {
527         load_fn = get_aux6;
528     } else if ( loadfn == "aux7" ) {
529         load_fn = get_aux7;
530     } else if ( loadfn == "aux8" ) {
531         load_fn = get_aux8;
532     } else if ( loadfn == "aux9" ) {
533         load_fn = get_aux9;
534     } else if ( loadfn == "aux10" ) {
535         load_fn = get_aux10;
536     } else if ( loadfn == "aux11" ) {
537         load_fn = get_aux11;
538     } else if ( loadfn == "aux12" ) {
539         load_fn = get_aux12;
540     } else if ( loadfn == "aux13" ) {
541         load_fn = get_aux13;
542     } else if ( loadfn == "aux14" ) {
543         load_fn = get_aux14;
544     } else if ( loadfn == "aux15" ) {
545         load_fn = get_aux15;
546     } else if ( loadfn == "aux16" ) {
547         load_fn = get_aux16;
548     } else if ( loadfn == "aux17" ) {
549         load_fn = get_aux17;
550     } else if ( loadfn == "aux18" ) {
551         load_fn = get_aux18;
552     } else if ( loadfn == "ax" ) {
553         load_fn = get_Ax;
554     } else if ( loadfn == "speed" ) {
555         load_fn = get_speed;
556     } else if ( loadfn == "mach" ) {
557         load_fn = get_mach;
558     } else if ( loadfn == "altitude" ) {
559         load_fn = get_altitude;
560     } else if ( loadfn == "agl" ) {
561         load_fn = get_agl;
562     } else if ( loadfn == "framerate" ) {
563         load_fn = get_frame_rate;
564     } else if ( loadfn == "heading" ) {
565         load_fn = get_heading;
566     } else if ( loadfn == "fov" ) {
567         load_fn = get_fov;
568     } else if ( loadfn == "vfc_tris_culled" ) {
569         load_fn = get_vfc_tris_culled;
570     } else if ( loadfn == "vfc_tris_drawn" ) {
571         load_fn = get_vfc_tris_drawn;
572     } else if ( loadfn == "aoa" ) {
573         load_fn = get_aoa;
574     } else if ( loadfn == "latitude" ) {
575         load_fn  = get_latitude;
576     } else if ( loadfn == "anzg" ) {
577         load_fn = get_anzg;
578     } else if ( loadfn == "longitude" ) {
579         load_fn   = get_longitude;
580     } else if (loadfn=="throttleval") {
581         load_fn = get_throttleval;
582     }
583
584     p = (instr_item *) new instr_label ( x,
585                                          y,
586                                          width,
587                                          height,
588                                          load_fn,
589                                          label_format.c_str(),
590                                          pre_label_string,
591                                          post_label_string,
592                                          scaling,
593                                          options,
594                                          justification,
595                                          font_size,
596                                          blinking,
597                                          latitude,
598                                          longitude,
599                                          label_box, //hud
600                                          working,
601                                          digits); //suma
602
603     return p;
604 } // end readLabel
605
606 static instr_item * 
607 readTBI(const SGPropertyNode * node)
608 {
609
610     instr_item *p;
611
612     name           = node->getStringValue("name");
613     x              = node->getIntValue("x");
614     y              = node->getIntValue("y");
615     width          = node->getIntValue("width");
616     height         = node->getIntValue("height");
617     maxBankAngle   = node->getFloatValue("maxBankAngle");
618     maxSlipAngle   = node->getFloatValue("maxSlipAngle");
619     gap_width      = node->getIntValue("gap_width");
620     working        = node->getBoolValue("working");
621     tsi                    = node->getBoolValue("tsi"); //suma
622     rad                    = node->getFloatValue("rad"); //suma
623
624     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
625
626
627     p = (instr_item *) new fgTBI_instr( x,
628                                         y,  
629                                         width,
630                                         height,
631                                         get_roll,
632                                         get_sideslip,
633                                         maxBankAngle, 
634                                         maxSlipAngle,
635                                         gap_width,
636                                         working,
637                                         tsi, //suma
638                                         rad); //suma
639
640     return p;
641 } //end readTBI
642
643
644 int readInstrument(const SGPropertyNode * node)
645 {
646
647     instr_item *HIptr;
648     
649     if ( fgGetString("/sim/startup/units") == "feet" ) {
650         strcpy(units, " ft");
651     } else {
652         strcpy(units, " m");
653     }
654
655     const SGPropertyNode * ladder_group = node->getNode("ladders");
656
657     if (ladder_group != 0) {
658         int nLadders = ladder_group->nChildren();
659         for (int j = 0; j < nLadders; j++) {
660             
661             HIptr = readLadder(ladder_group->getChild(j));
662             HUD_deque.insert( HUD_deque.begin(), HIptr);
663                                         
664         }// for - ladders
665     }
666
667     const SGPropertyNode * card_group = node->getNode("cards");
668     if (card_group != 0) {
669         int nCards = card_group->nChildren();
670         for (int j = 0; j < nCards; j++) {
671             
672             HIptr = readCard(card_group->getChild(j));
673             HUD_deque.insert( HUD_deque.begin(), HIptr);
674
675         }//for - cards
676     }
677
678     const SGPropertyNode * label_group = node->getNode("labels");
679     if (label_group != 0) {
680         int nLabels = label_group->nChildren();
681         for (int j = 0; j < nLabels; j++) {
682
683             HIptr = readLabel(label_group->getChild(j));
684             HUD_deque.insert( HUD_deque.begin(), HIptr);
685
686         }//for - labels
687     }
688
689     const SGPropertyNode * tbi_group = node->getNode("tbis");
690     if (tbi_group != 0) {
691         int nTbis = tbi_group->nChildren();
692         for (int j = 0; j < nTbis; j++) {
693
694             HIptr = readTBI(tbi_group->getChild(j));
695             HUD_deque.insert( HUD_deque.begin(), HIptr);
696
697         }//for - tbis
698     }
699     return 0;
700 }//end readinstrument
701
702
703 int readHud( istream &input ) 
704 {
705
706     SGPropertyNode root;
707
708     try {
709         readProperties(input, &root);
710     } catch (const sg_exception &e) {
711         guiErrorMessage("Error reading HUD: ", e);
712         return 0;
713     }
714   
715         
716     SG_LOG(SG_INPUT, SG_INFO, "Read properties for  " <<
717            root.getStringValue("name"));
718
719
720     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());  // empty the HUD deque
721
722
723     SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
724
725     const SGPropertyNode * instrument_group = root.getChild("instruments");
726     int nInstruments = instrument_group->nChildren();
727
728     for (int i = 0; i < nInstruments; i++) {
729                 
730         const SGPropertyNode * node = instrument_group->getChild(i);
731
732         SGPath path( globals->get_fg_root() );
733         path.append(node->getStringValue("path"));
734
735         SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
736                << node->getName()
737                << " from "
738                << path.str());
739
740
741         SGPropertyNode root2;
742         try {
743             readProperties(path.str(), &root2);
744         } catch (const sg_exception &e) {
745             guiErrorMessage("Error reading HUD instrument: ", e);
746             continue;
747         } 
748         readInstrument(&root2);
749     }//for loop(i)
750
751     return 0;
752 }
753
754
755 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
756 {
757
758
759     HUD_style = 1;
760
761     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
762
763     string hud_path =
764         fgGetString("/sim/hud/path", "Huds/Default/default.xml");
765     SGPath path(globals->get_fg_root());
766     path.append(hud_path);
767         
768     ifstream input(path.c_str());
769     if (!input.good()) {
770         SG_LOG(SG_INPUT, SG_ALERT,
771                "Cannot read Hud configuration from " << path.str());
772     } else {
773         readHud(input);
774         input.close();
775     }
776
777     fgHUDalphaInit();
778     fgHUDReshape();
779
780     return 0;  // For now. Later we may use this for an error code.
781
782 }
783
784 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
785 {
786
787     HUD_style = 2;
788
789     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
790
791     SGPath path(globals->get_fg_root());
792     path.append("Huds/Minimal/default.xml");
793
794
795     ifstream input(path.c_str());
796     if (!input.good()) {
797         SG_LOG(SG_INPUT, SG_ALERT,
798                "Cannot read Hud configuration from " << path.str());
799     } else {
800         readHud(input);
801         input.close();
802     }
803
804     return 0;  // For now. Later we may use this for an error code.
805
806 }
807 //$$$ End - added, Neetha, 28 Nov 2k  
808
809 static int global_day_night_switch = DAY;
810
811 void HUD_masterswitch( bool incr )
812 {
813     if ( fgGetBool("/sim/hud/visibility") ) {
814         if ( global_day_night_switch == DAY ) {
815             global_day_night_switch = NIGHT;
816         } else {
817             fgSetBool("/sim/hud/visibility", false);
818         }
819     } else {
820         fgSetBool("/sim/hud/visibility", true);
821         global_day_night_switch = DAY;
822     }   
823 }
824
825 void HUD_brightkey( bool incr_bright )
826 {
827     instr_item *pHUDInstr = HUD_deque[0];
828     int brightness        = pHUDInstr->get_brightness();
829
830     if( fgGetBool("/sim/hud/visibility") ) {
831         if( incr_bright ) {
832             switch (brightness)
833                 {
834                 case BRT_LIGHT:
835                     brightness = BRT_BLACK;
836                     break;
837
838                 case BRT_MEDIUM:
839                     brightness = BRT_LIGHT;
840                     break;
841
842                 case BRT_DARK:
843                     brightness = BRT_MEDIUM;
844                     break;
845
846                 case BRT_BLACK:
847                     brightness = BRT_DARK;
848                     break;
849
850                 default:
851                     brightness = BRT_BLACK;
852                 }
853         } else {
854             switch (brightness)
855                 {
856                 case BRT_LIGHT:
857                     brightness = BRT_MEDIUM;
858                     break;
859
860                 case BRT_MEDIUM:
861                     brightness = BRT_DARK;
862                     break;
863
864                 case BRT_DARK:
865                     brightness = BRT_BLACK;
866                     break;
867
868                 case BRT_BLACK:
869                     brightness = BRT_LIGHT;
870                     break;
871
872                 default:
873                     fgSetBool("/sim/hud/visibility", false);
874                 }
875         }
876     } else {
877         fgSetBool("/sim/hud/visibility", true);
878     }
879
880     pHUDInstr->SetBrightness( brightness );
881 }
882
883
884 #define fgAP_CLAMP(val,min,max) ( (val) = (val) > (max) ? (max) : (val) < (min) ? (min) : (val) )
885
886 static puDialogBox *HUDalphaDialog;
887 static puText      *HUDalphaText;
888 static puSlider    *HUDalphaHS0;
889 //static puText      *HUDtextText;
890 //static puSlider    *HUDalphaHS1;
891 static char         SliderText[2][ 8 ];
892
893 static void alpha_adj( puObject *hs ) {
894     float val ;
895
896     hs-> getValue ( &val ) ;
897     fgAP_CLAMP ( val, 0.1, 1.0 ) ;
898     // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
899     hud_trans_alpha = val;
900     sprintf( SliderText[ 0 ], "%05.2f", hud_trans_alpha );
901     HUDalphaText -> setLabel ( SliderText[ 0 ] ) ;
902 }
903
904 void fgHUDalphaAdjust( puObject * ) {
905     fgSetBool("/sim/hud/antialiased", true);
906     FG_PUSH_PUI_DIALOG( HUDalphaDialog );
907 }
908
909 static void goAwayHUDalphaAdjust (puObject *)
910 {
911     FG_POP_PUI_DIALOG( HUDalphaDialog );
912 }
913
914 static void cancelHUDalphaAdjust (puObject *)
915 {
916     fgSetBool("/sim/hud/antialiased", false);
917     FG_POP_PUI_DIALOG( HUDalphaDialog );
918 }
919
920 // Done once at system initialization
921 void fgHUDalphaInit( void ) {
922
923     //  printf("fgHUDalphaInit\n");
924 #define HORIZONTAL  FALSE
925
926     int DialogX = 40;
927     int DialogY = 100;
928     int DialogWidth = 240;
929
930     char Label[] =  "HUD Adjuster";
931     char *s;
932
933     int labelX = (DialogWidth / 2) -
934         ( puGetDefaultLabelFont().getStringWidth( Label ) / 2);
935         
936     int nSliders = 1;
937     int slider_x = 10;
938     int slider_y = 55;
939     int slider_width = 220;
940     int slider_title_x = 15;
941     int slider_value_x = 160;
942     float slider_delta = 0.05f;
943
944     puFont HUDalphaLegendFont;
945     puFont HUDalphaLabelFont;
946     puGetDefaultFonts ( &HUDalphaLegendFont, &HUDalphaLabelFont );
947         
948     HUDalphaDialog = new puDialogBox ( DialogX, DialogY ); {
949         int horiz_slider_height = HUDalphaLabelFont.getStringHeight() +
950             HUDalphaLabelFont.getStringDescender() +
951             PUSTR_TGAP + PUSTR_BGAP + 5;
952
953         /* puFrame *
954            HUDalphaFrame = new puFrame ( 0, 0, DialogWidth,
955            85 + nSliders
956            * horiz_slider_height ); */
957
958         puText *
959             HUDalphaDialogMessage = new puText ( labelX,
960                                                  52 + nSliders
961                                                  * horiz_slider_height );
962         HUDalphaDialogMessage -> setDefaultValue ( Label );
963         HUDalphaDialogMessage -> getDefaultValue ( &s );
964         HUDalphaDialogMessage -> setLabel        ( s );
965
966         HUDalphaHS0 = new puSlider ( slider_x, slider_y,
967                                      slider_width, HORIZONTAL ) ;
968         HUDalphaHS0->     setDelta ( slider_delta ) ;
969         HUDalphaHS0->     setValue ( hud_trans_alpha ) ;
970         HUDalphaHS0->    setCBMode ( PUSLIDER_DELTA ) ;
971         HUDalphaHS0->  setCallback ( alpha_adj ) ;
972
973         puText *
974             HUDalphaTitle =      new puText ( slider_title_x, slider_y ) ;
975         HUDalphaTitle-> setDefaultValue ( "Alpha" ) ;
976         HUDalphaTitle-> getDefaultValue ( &s ) ;
977         HUDalphaTitle->        setLabel ( s ) ;
978                 
979         HUDalphaText = new puText ( slider_value_x, slider_y ) ;
980         sprintf( SliderText[ 0 ], "%05.2f", hud_trans_alpha );
981         HUDalphaText-> setLabel ( SliderText[ 0 ] ) ;
982
983         puOneShot *
984             HUDalphaOkButton =     new puOneShot ( 10, 10, 60, 45 );
985         HUDalphaOkButton->         setLegend ( gui_msg_OK );
986         HUDalphaOkButton-> makeReturnDefault ( TRUE );
987         HUDalphaOkButton->       setCallback ( goAwayHUDalphaAdjust );
988
989         puOneShot *
990             HUDalphaNoButton = new puOneShot ( 160, 10, 230, 45 );
991         HUDalphaNoButton->     setLegend ( gui_msg_CANCEL );
992         HUDalphaNoButton->   setCallback ( cancelHUDalphaAdjust );
993     }
994     FG_FINALIZE_PUI_DIALOG( HUDalphaDialog );
995
996 #undef HORIZONTAL
997 }
998
999
1000 void fgHUDReshape(void) {
1001     if ( HUDtext ) {
1002         // this chunk of code is not necessarily thread safe if the
1003         // compiler optimizer reorders these statements.  Note that
1004         // "delete ptr" does not set "ptr = NULL".  We have to do that
1005         // ourselves.
1006         fntRenderer *tmp = HUDtext;
1007         HUDtext = NULL;
1008         delete tmp;
1009     }
1010
1011     HUD_TextSize = fgGetInt("/sim/startup/xsize") / 60;
1012     HUD_TextSize = 10;
1013     HUDtext = new fntRenderer();
1014     HUDtext -> setFont      ( guiFntHandle ) ;
1015     HUDtext -> setPointSize ( HUD_TextSize ) ;
1016     HUD_TextList.setFont( HUDtext );
1017 }
1018
1019
1020 static void set_hud_color(float r, float g, float b) {
1021     fgGetBool("/sim/hud/antialiased") ?
1022         glColor4f(r,g,b,hud_trans_alpha) :
1023         glColor3f(r,g,b);
1024 }
1025
1026
1027 // fgUpdateHUD
1028 //
1029 // Performs a once around the list of calls to instruments installed in
1030 // the HUD object with requests for redraw. Kinda. It will when this is
1031 // all C++.
1032 //
1033 void fgUpdateHUD( void ) {
1034         
1035     static const float normal_aspect = float(640) / float(480);
1036     // note: aspect_ratio is Y/X
1037     float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
1038     if( current_aspect > normal_aspect ) {
1039         float aspect_adjust = current_aspect / normal_aspect;
1040         float adjust = 320.0f*aspect_adjust - 320.0f;
1041         fgUpdateHUD( -adjust, 0.0f, 640.0f+adjust, 480.0f );
1042     } else {
1043         float aspect_adjust = normal_aspect / current_aspect;
1044         float adjust = 240.0f*aspect_adjust - 240.0f;
1045         fgUpdateHUD( 0.0f, -adjust, 640.0f, 480.0f+adjust );
1046     }
1047 }
1048
1049 void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
1050                   GLfloat x_end, GLfloat y_end )
1051 {
1052     int brightness;
1053     // int day_night_sw = current_aircraft.controls->day_night_switch;
1054     int day_night_sw = global_day_night_switch;
1055     int hud_displays = HUD_deque.size();
1056     instr_item *pHUDInstr;
1057     // float line_width;
1058
1059     if( !hud_displays ) {  // Trust everyone, but ALWAYS cut the cards!
1060         return;
1061     }
1062
1063     HUD_TextList.erase();
1064     HUD_LineList.erase();
1065     // HUD_StippleLineList.erase();
1066   
1067     pHUDInstr = HUD_deque[0];
1068     brightness = pHUDInstr->get_brightness();
1069     // brightness = HUD_deque.at(0)->get_brightness();
1070
1071     glMatrixMode(GL_PROJECTION);
1072     glPushMatrix();
1073
1074     glLoadIdentity();
1075     gluOrtho2D(x_start, x_end, y_start, y_end);
1076     glMatrixMode(GL_MODELVIEW);
1077     glPushMatrix();
1078     glLoadIdentity();
1079
1080     glDisable(GL_DEPTH_TEST);
1081     glDisable(GL_LIGHTING);
1082
1083     static const SGPropertyNode * antialiased_node
1084         = fgGetNode("/sim/hud/antialiased");
1085
1086     if( antialiased_node->getBoolValue() ) {
1087         glEnable(GL_LINE_SMOOTH);
1088         //        glEnable(GL_BLEND);
1089         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1090         glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
1091         glLineWidth(1.5);
1092     } else {
1093         glLineWidth(1.0);
1094     }
1095
1096     if( day_night_sw == DAY) {
1097         switch (brightness)
1098             {
1099             case BRT_LIGHT:
1100                 set_hud_color (0.1f, 0.9f, 0.1f);
1101                 break;
1102
1103             case BRT_MEDIUM:
1104                 set_hud_color (0.1f, 0.7f, 0.0f);
1105                 break;
1106
1107             case BRT_DARK:
1108                 set_hud_color (0.0f, 0.6f, 0.0f);
1109                 break;
1110
1111             case BRT_BLACK:
1112                 set_hud_color( 0.0f, 0.0f, 0.0f);
1113                 break;
1114
1115             default:
1116                 set_hud_color (0.1f, 0.9f, 0.1f);
1117             }
1118     } else {
1119         if( day_night_sw == NIGHT) {
1120             switch (brightness)
1121                 {
1122                 case BRT_LIGHT:
1123                     set_hud_color (0.9f, 0.1f, 0.1f);
1124                     break;
1125
1126                 case BRT_MEDIUM:
1127                     set_hud_color (0.7f, 0.0f, 0.1f);
1128                     break;
1129
1130                 case BRT_DARK:
1131                     set_hud_color (0.6f, 0.0f, 0.0f);
1132                     break;
1133
1134                 case BRT_BLACK:
1135                     set_hud_color( 0.0f, 0.0f, 0.0f);
1136                     break;
1137
1138                 default:
1139                     set_hud_color (0.6f, 0.0f, 0.0f);
1140                 }
1141         } else {     // Just in case default
1142             set_hud_color (0.1f, 0.9f, 0.1f);
1143         }
1144     }
1145
1146     deque < instr_item * > :: iterator current = HUD_deque.begin();
1147     deque < instr_item * > :: iterator last = HUD_deque.end();
1148
1149     for ( ; current != last; ++current ) {
1150         pHUDInstr = *current;
1151
1152         if( pHUDInstr->enabled()) {
1153             //  fgPrintf( SG_COCKPIT, SG_DEBUG, "HUD Code %d  Status %d\n",
1154             //            hud->code, hud->status );
1155             pHUDInstr->draw();
1156                   
1157         }
1158     }
1159
1160     char *gmt_str = get_formated_gmt_time();
1161     HUD_TextList.add( fgText(40, 10, gmt_str, 0) );
1162
1163 #ifdef FG_NETWORK_OLK
1164     if ( net_hud_display ) {
1165         net_hud_update();
1166     }
1167 #endif
1168
1169
1170     // temporary
1171     // extern bool fgAPAltitudeEnabled( void );
1172     // extern bool fgAPHeadingEnabled( void );
1173     // extern bool fgAPWayPointEnabled( void );
1174     // extern char *fgAPget_TargetDistanceStr( void );
1175     // extern char *fgAPget_TargetHeadingStr( void );
1176     // extern char *fgAPget_TargetAltitudeStr( void );
1177     // extern char *fgAPget_TargetLatLonStr( void );
1178
1179     int apY = 480 - 80;
1180     // char scratch[128];
1181     // HUD_TextList.add( fgText( "AUTOPILOT", 20, apY) );
1182     // apY -= 15;
1183     if( current_autopilot->get_HeadingEnabled() ) {
1184         HUD_TextList.add( fgText( 40, apY, 
1185                                   current_autopilot->get_TargetHeadingStr()) );
1186         apY -= 15;
1187     }
1188     if( current_autopilot->get_AltitudeEnabled() ) {
1189         HUD_TextList.add( fgText( 40, apY, 
1190                                   current_autopilot->get_TargetAltitudeStr()) );
1191         apY -= 15;
1192     }
1193     if( current_autopilot->get_HeadingMode() == 
1194         FGAutopilot::FG_HEADING_WAYPOINT )
1195     {
1196         char *wpstr;
1197         wpstr = current_autopilot->get_TargetWP1Str();
1198         if ( strlen( wpstr ) ) {
1199             HUD_TextList.add( fgText( 40, apY, wpstr ) );
1200             apY -= 15;
1201         }
1202         wpstr = current_autopilot->get_TargetWP2Str();
1203         if ( strlen( wpstr ) ) {
1204             HUD_TextList.add( fgText( 40, apY, wpstr ) );
1205             apY -= 15;
1206         }
1207         wpstr = current_autopilot->get_TargetWP3Str();
1208         if ( strlen( wpstr ) ) {
1209             HUD_TextList.add( fgText( 40, apY, wpstr ) );
1210                 apY -= 15;
1211         }
1212     }
1213   
1214     HUD_TextList.draw();
1215
1216     HUD_LineList.draw();
1217
1218     // glEnable(GL_LINE_STIPPLE);
1219     // glLineStipple( 1, 0x00FF );
1220     // HUD_StippleLineList.draw();
1221     // glDisable(GL_LINE_STIPPLE);
1222
1223     if( antialiased_node->getBoolValue() ) {
1224         // glDisable(GL_BLEND);
1225         glDisable(GL_LINE_SMOOTH);
1226         glLineWidth(1.0);
1227     }
1228
1229     glEnable(GL_DEPTH_TEST);
1230     glEnable(GL_LIGHTING);
1231     glMatrixMode(GL_PROJECTION);
1232     glPopMatrix();
1233     glMatrixMode(GL_MODELVIEW);
1234     glPopMatrix();
1235 }
1236