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