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