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