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