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