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