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