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