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