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