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