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