]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.cxx
Add a function declaration
[flightgear.git] / src / Cockpit / hud.cxx
1 // hud.cxx -- hud defines and prototypes
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - micheleamerica@geocities.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #include <simgear/compiler.h>
24 #include <simgear/misc/exception.hxx>
25
26 #include STL_STRING
27 #include STL_FSTREAM
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #ifdef HAVE_WINDOWS_H
34 #  include <windows.h>
35 #endif
36
37 #ifdef __BORLANDC__
38 #  define exception c_exception
39 #endif
40
41 #include <math.h>
42
43 #include <GL/glut.h>
44 #include <stdlib.h>
45 #include <stdio.h>              // char related functions
46 #include <string.h>             // strcmp()
47
48 #include <simgear/constants.h>
49 #include <simgear/debug/logstream.hxx>
50 #include <simgear/misc/props.hxx>
51 #include <simgear/misc/sg_path.hxx>
52 //#include <simgear/math/fg_random.h>
53 //#include <simgear/math/polar3d.hxx>
54
55 #include <Aircraft/aircraft.hxx>
56 #include <Autopilot/newauto.hxx>
57 #include <GUI/gui.h>
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
60 #ifdef FG_NETWORK_OLK
61 #include <NetworkOLK/network.h>
62 #endif
63 #include <Scenery/scenery.hxx>
64 //#include <Time/fg_timer.hxx>
65
66 #if defined ( __sun__ ) || defined ( __sgi )
67 extern "C" {
68     extern void *memmove(void *, const void *, size_t);
69 }
70 #endif
71
72 #include "hud.hxx"
73
74 static char units[5];
75
76 // The following routines obtain information concerning the aircraft's
77 // current state and return it to calling instrument display routines.
78 // They should eventually be member functions of the aircraft.
79 //
80
81 deque< instr_item * > HUD_deque;
82
83 fgTextList         HUD_TextList;
84 fgLineList         HUD_LineList;
85 fgLineList         HUD_StippleLineList;
86
87 fntRenderer *HUDtext = 0;
88 float  HUD_TextSize = 0;
89 int HUD_style = 0;
90
91 float HUD_matrix[16];
92 static float hud_trans_alpha = 0.67f;
93
94
95 //$$$ begin - added, Neetha, 28 Nov 2k
96
97 static string   name;
98 static int              x;
99 static int              y;
100 static UINT    width;
101 static UINT    height;
102 static float    factor;
103 static float   span_units;
104 static float   division_units;
105 static float   minor_division = 0;
106 static UINT    screen_hole;
107 static UINT    lbl_pos;
108 static bool    working;
109 static string  loadfn;
110 static UINT     options;
111 static float   maxValue;
112 static float   minValue;
113 static float   scaling;
114 static UINT    major_divs;
115 static UINT    minor_divs;
116 static UINT    modulator;
117 static int     dp_showing = 0;
118 static string  label_format;
119 static string  prelabel;
120 static string  postlabel;
121 static int     justi;
122 static int              blinking;
123 static float   maxBankAngle;
124 static float   maxSlipAngle;
125 static UINT    gap_width;
126 static bool     latitude;
127 static bool     longitude;
128 static bool     tick_bottom;
129 static bool     tick_top;
130 static bool     tick_right;
131 static bool     tick_left;
132 static bool     cap_bottom;
133 static bool     cap_top;
134 static bool     cap_right;
135 static bool     cap_left;
136 static float   marker_off;
137 static string  type;
138 static bool    enable_pointer;
139 static string  type_pointer;
140 static bool    frl_spot;
141 static bool     target;
142 static bool    vel_vector;
143 static bool    drift;
144 static bool    alpha;
145 static bool     energy;
146 static bool     climb_dive;
147 static bool     glide;
148 static float    glide_slope_val;
149 static bool     worm_energy;
150 static bool     waypoint;
151 static string type_tick;//hud
152 static string length_tick;//hud
153 static bool label_box;//hud
154 static int digits; //suma
155 static float radius; //suma
156 static int divisions; //suma
157 static int zoom; //suma
158 static int zenith; //suma
159 static int nadir ; //suma
160 static int hat; //suma
161 static bool tsi; //suma
162 static float rad; //suma
163
164
165 static FLTFNPTR load_fn;    
166 static fgLabelJust justification;
167 static const char *pre_label_string  = 0;
168 static const char *post_label_string = 0;
169
170 int readHud( istream &input );
171 int readInstrument ( const SGPropertyNode * node);
172 static instr_item * readLadder ( const SGPropertyNode * node);
173 static instr_item * readCard ( const SGPropertyNode * node);
174 static instr_item * readLabel( const SGPropertyNode * node);
175 static instr_item * readTBI( const SGPropertyNode * node);
176 //$$$ end   - added, Neetha, 28 Nov 2k
177
178 static void drawHUD();
179 static void fgUpdateHUDVirtual();
180
181 void fgHUDalphaInit( void );
182
183 class locRECT {
184 public:
185     RECT rect;
186
187     locRECT( UINT left, UINT top, UINT right, UINT bottom);
188     RECT get_rect(void) { return rect;}
189 };
190
191 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
192 {
193     rect.left   =  left;
194     rect.top    =  top;
195     rect.right  =  right;
196     rect.bottom =  bottom;
197
198 }
199 // #define DEBUG
200
201 //========================= End of Class Implementations===================
202 // fgHUDInit
203 //
204 // Constructs a HUD object and then adds in instruments. At the present
205 // the instruments are hard coded into the routine. Ultimately these need
206 // to be defined by the aircraft's instrumentation records so that the
207 // display for a Piper Cub doesn't show the speed range of a North American
208 // mustange and the engine readouts of a B36!
209 //
210
211 #define INSTRDEFS 21
212
213 //$$$ begin - added, Neetha, 28 Nov 2k
214 static instr_item * 
215 readLadder(const SGPropertyNode * node)
216 {
217
218     instr_item *p;
219
220     name                        = node->getStringValue("name");
221     x                           = node->getIntValue("x");
222     y                           = node->getIntValue("y");
223     width                       = node->getIntValue("width");
224     height                      = node->getIntValue("height");
225     factor                      = node->getFloatValue("compression_factor");
226     span_units          = node->getFloatValue("span_units");
227     division_units      = node->getFloatValue("division_units");
228     screen_hole         = node->getIntValue("screen_hole");
229     lbl_pos                     = node->getIntValue("lbl_pos");
230     frl_spot            = node->getBoolValue("enable_frl",false);
231     target                      = node->getBoolValue("enable_target_spot",false);
232     vel_vector          = node->getBoolValue("enable_velocity_vector",false);
233     drift                       = node->getBoolValue("enable_drift_marker",false);
234     alpha                       = node->getBoolValue("enable_alpha_bracket",false);
235     energy                      = node->getBoolValue("enable_energy_marker",false);
236     climb_dive          = node->getBoolValue("enable_climb_dive_marker",false);
237     glide                       = node->getBoolValue("enable_glide_slope_marker",false);
238     glide_slope_val     = node->getFloatValue("glide_slope",-4.0);
239     worm_energy         = node->getBoolValue("enable_energy_marker",false);
240     waypoint            = node->getBoolValue("enable_waypoint_marker",false);
241     working                     = node->getBoolValue("working");
242     zenith                      = node->getIntValue("zenith");  //suma
243     nadir                       = node->getIntValue("nadir");  //suma
244     hat                         = node->getIntValue("hat");
245     // The factor assumes a base of 55 degrees per 640 pixels.
246     // Invert to convert the "compression" factor to a
247     // pixels-per-degree number.
248     if( HUD_style == 1)
249     {
250 //        if(factor == 0)
251             factor = 1;
252         factor = (640./55.) / factor;
253     }
254
255     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
256         
257     p = (instr_item *) new HudLadder( name, x, y,
258                                       width, height, factor,
259                                       get_roll, get_pitch,
260                                       span_units, division_units, minor_division,
261                                       screen_hole, lbl_pos, frl_spot, target, vel_vector, 
262                                       drift, alpha, energy, climb_dive, 
263                                       glide, glide_slope_val, worm_energy, 
264                                       waypoint, working, zenith, nadir, hat);
265                                 
266     return p;
267                 
268 } //end readLadder
269
270 static instr_item * 
271 readCard(const SGPropertyNode * node)
272 {
273
274     instr_item *p;
275
276     name                        = node->getStringValue("name");
277     x                           = node->getIntValue("x");
278     y                           = node->getIntValue("y");
279     width                       = node->getIntValue("width");
280     height                      = node->getIntValue("height");
281     loadfn                      = node->getStringValue("loadfn");
282     options                     = node->getIntValue("options");
283     maxValue            = node->getFloatValue("maxValue");
284     minValue            = node->getFloatValue("minValue");
285     scaling                     = node->getFloatValue("disp_scaling");
286     major_divs          = node->getIntValue("major_divs");
287     minor_divs          = node->getIntValue("minor_divs");
288     modulator           = node->getIntValue("modulator");
289     span_units          = node->getFloatValue("value_span");
290     type                        = node->getStringValue("type");
291     tick_bottom     = node->getBoolValue("tick_bottom",false);
292     tick_top            = node->getBoolValue("tick_top",false);
293     tick_right          = node->getBoolValue("tick_right",false);
294     tick_left           = node->getBoolValue("tick_left",false);
295     cap_bottom          = node->getBoolValue("cap_bottom",false);
296     cap_top                     = node->getBoolValue("cap_top",false);
297     cap_right           = node->getBoolValue("cap_right",false);
298     cap_left            = node->getBoolValue("cap_left",false);
299     marker_off          = node->getFloatValue("marker_offset",0.0);
300     enable_pointer      = node->getBoolValue("enable_pointer",true);
301     type_pointer        = node->getStringValue("pointer_type");
302     type_tick           = node->getStringValue("tick_type");//hud Can be 'circle' or 'line'
303     length_tick         = node->getStringValue("tick_length");//hud For variable length
304     working                     = node->getBoolValue("working");
305     radius                      = node->getFloatValue("radius"); //suma
306     divisions           = node->getIntValue("divisions"); //suma
307     zoom                        = node->getIntValue("zoom"); //suma
308
309     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
310
311
312     if(type=="gauge") {
313         span_units = maxValue - minValue;
314     }
315
316     if (loadfn=="anzg") {
317         load_fn = get_anzg;
318     } else if (loadfn=="heading") {
319         load_fn = get_heading;
320     } else if (loadfn=="aoa") {
321         load_fn = get_aoa;
322     } else if (loadfn=="climb") {
323         load_fn = get_climb_rate;
324     } else if (loadfn=="altitude") {
325         load_fn = get_altitude;
326     } else if (loadfn=="agl") {
327         load_fn = get_agl;
328     } else if (loadfn=="speed") {
329         load_fn = get_speed;
330     } else if (loadfn=="view_direction") {
331         load_fn = get_view_direction;
332     } else if (loadfn=="aileronval") {
333         load_fn = get_aileronval;
334     } else if (loadfn=="elevatorval") {
335         load_fn = get_elevatorval;
336     } else if (loadfn=="elevatortrimval") {
337         load_fn = get_elev_trimval;
338     } else if (loadfn=="rudderval") {
339         load_fn = get_rudderval;
340     } else if (loadfn=="throttleval") {
341         load_fn = get_throttleval;
342     }
343
344
345     if ( (type == "dial") | (type == "tape") ) {
346         p = (instr_item *) new hud_card( x,
347                                          y,  
348                                          width,
349                                          height,
350                                          load_fn,
351                                          options,
352                                          maxValue, minValue,
353                                          scaling,
354                                          major_divs, minor_divs,
355                                          modulator,
356                                          dp_showing,
357                                          span_units,
358                                          type,
359                                          tick_bottom,
360                                          tick_top,
361                                          tick_right,
362                                          tick_left,
363                                          cap_bottom,
364                                          cap_top,
365                                          cap_right,
366                                          cap_left,
367                                          marker_off,
368                                          enable_pointer,
369                                          type_pointer,
370                                          type_tick,//hud
371                                          length_tick,//hud
372                                          working,
373                                          radius, //suma
374                                          divisions, //suma
375                                          zoom  //suma
376                                          );
377     } else {
378         p = (instr_item *) new  gauge_instr( x,            // x
379                                              y,  // y
380                                              width,            // width
381                                              height,            // height
382                                              load_fn, // data source
383                                              options,
384                                              scaling,
385                                              maxValue,minValue,
386                                              major_divs, minor_divs,
387                                              dp_showing,
388                                              modulator,
389                                              working);
390     }
391
392     return p;
393 }// end readCard
394
395 static instr_item *
396 readLabel(const SGPropertyNode * node)
397 {
398     instr_item *p;
399
400     int font_size = (fgGetInt("/sim/startup/xsize") > 1000) ? HUD_FONT_LARGE : HUD_FONT_SMALL;
401
402     name                = node->getStringValue("name");
403     x                   = node->getIntValue("x");
404     y                   = node->getIntValue("y");
405     width               = node->getIntValue("width");
406     height              = node->getIntValue("height");
407     loadfn              = node->getStringValue("data_source");
408     label_format        = node->getStringValue("label_format");
409     prelabel            = node->getStringValue("pre_label_string");
410     postlabel           = node->getStringValue("post_label_string");
411     scaling             = node->getFloatValue("scale_data");
412     options             = node->getIntValue("options");
413     justi               = node->getIntValue("justification");
414     blinking            = node->getIntValue("blinking");
415     latitude            = node->getBoolValue("latitude",false);
416     longitude           = node->getBoolValue("longitude",false);
417     label_box           = node->getBoolValue("label_box",false);//hud
418     working             = node->getBoolValue("working");
419     digits              = node->getIntValue("digits"); //suma
420
421
422     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
423
424
425     if ( justi == 0 ) {
426         justification = LEFT_JUST;
427     } else {
428         if ( justi == 1 ) {
429             justification = CENTER_JUST;
430         } else {
431             if ( justi == 2 ) {
432                 justification = RIGHT_JUST;
433             }
434         }
435     }
436
437     if ( prelabel == "NULL" ) {
438         pre_label_string = NULL;
439     } else {
440         if ( prelabel == "blank" ) {
441             pre_label_string = " ";
442         } else {
443             pre_label_string = prelabel.c_str();
444         }
445     }
446
447     if ( postlabel == "blank" ) {
448         post_label_string = " ";
449     } else {
450         if ( postlabel == "NULL" ) {
451             post_label_string = NULL;
452         } else {
453             if ( postlabel == "units" ) {
454                 post_label_string = units;
455             } else {
456                 post_label_string = postlabel.c_str();
457             }
458         }
459     }
460
461     if ( loadfn== "aux1" ) {
462         load_fn = get_aux1;
463     } else if ( loadfn == "aux2" ) {
464         load_fn = get_aux2;
465     } else if ( loadfn == "aux3" ) {
466         load_fn = get_aux3;
467     } else if ( loadfn == "aux4" ) {
468         load_fn = get_aux4;
469     } else if ( loadfn == "aux5" ) {
470         load_fn = get_aux5;
471     } else if ( loadfn == "aux6" ) {
472         load_fn = get_aux6;
473     } else if ( loadfn == "aux7" ) {
474         load_fn = get_aux7;
475     } else if ( loadfn == "aux8" ) {
476         load_fn = get_aux8;
477     } else if ( loadfn == "aux9" ) {
478         load_fn = get_aux9;
479     } else if ( loadfn == "aux10" ) {
480         load_fn = get_aux10;
481     } else if ( loadfn == "aux11" ) {
482         load_fn = get_aux11;
483     } else if ( loadfn == "aux12" ) {
484         load_fn = get_aux12;
485     } else if ( loadfn == "aux13" ) {
486         load_fn = get_aux13;
487     } else if ( loadfn == "aux14" ) {
488         load_fn = get_aux14;
489     } else if ( loadfn == "aux15" ) {
490         load_fn = get_aux15;
491     } else if ( loadfn == "aux16" ) {
492         load_fn = get_aux16;
493     } else if ( loadfn == "aux17" ) {
494         load_fn = get_aux17;
495     } else if ( loadfn == "aux18" ) {
496         load_fn = get_aux18;
497     } else 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     if( HUD_style == 1)
1000     {
1001         fgUpdateHUDVirtual();
1002         return;
1003     }
1004     
1005     static const float normal_aspect = float(640) / float(480);
1006     // note: aspect_ratio is Y/X
1007     float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
1008     if( current_aspect > normal_aspect ) {
1009         float aspect_adjust = current_aspect / normal_aspect;
1010         float adjust = 320.0f*aspect_adjust - 320.0f;
1011         fgUpdateHUD( -adjust, 0.0f, 640.0f+adjust, 480.0f );
1012     } else {
1013         float aspect_adjust = normal_aspect / current_aspect;
1014         float adjust = 240.0f*aspect_adjust - 240.0f;
1015         fgUpdateHUD( 0.0f, -adjust, 640.0f, 480.0f+adjust );
1016     }
1017 }
1018
1019 void fgUpdateHUDVirtual()
1020 {
1021     FGViewer* view = globals->get_current_view();
1022
1023     // Standard fgfs projection, with essentially meaningless clip
1024     // planes (we'll map the whole HUD plane to z=-1)
1025     glMatrixMode(GL_PROJECTION);
1026     glPushMatrix();
1027     glLoadIdentity();
1028     gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
1029
1030     glMatrixMode(GL_MODELVIEW);
1031     glPushMatrix();
1032     glLoadIdentity();
1033   
1034     // Standard fgfs view direction computation
1035     float lookat[3];
1036     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
1037     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
1038     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
1039     if(fabs(lookat[1]) > 9999) lookat[1] = 9999; // FPU sanity
1040     gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
1041
1042     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
1043     // This is the default fgfs field of view, which the HUD files are
1044     // written to assume.
1045     float dx = 0.52056705; // tan(55/2)
1046     float dy = dx * 0.75;  // assumes 4:3 aspect ratio
1047     float m[16];
1048     m[0] = dx; m[4] =  0; m[ 8] = 0; m[12] = 0;
1049     m[1] =  0; m[5] = dy; m[ 9] = 0; m[13] = 0;
1050     m[2] =  0; m[6] =  0; m[10] = 1; m[14] = 0;
1051     m[3] =  0; m[7] =  0; m[11] = 0; m[15] = 1;
1052     glMultMatrixf(m);
1053
1054     // Convert the 640x480 "HUD standard" coordinate space to a square
1055     // about the origin in the range [-1:1] at depth of -1
1056     glScalef(1./320, 1./240, 1);
1057     glTranslatef(-320, -240, -1);
1058
1059     // Do the deed
1060     drawHUD();
1061
1062     // Clean up our mess
1063     glMatrixMode(GL_PROJECTION);
1064     glPopMatrix();
1065     glMatrixMode(GL_MODELVIEW);
1066     glPopMatrix();
1067 }
1068
1069 void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
1070                   GLfloat x_end, GLfloat y_end )
1071 {
1072     glMatrixMode(GL_PROJECTION);
1073     glPushMatrix();
1074     glLoadIdentity();
1075     gluOrtho2D(x_start, x_end, y_start, y_end);
1076
1077     glMatrixMode(GL_MODELVIEW);
1078     glPushMatrix();
1079     glLoadIdentity();
1080
1081     drawHUD();
1082
1083     glMatrixMode(GL_PROJECTION);
1084     glPopMatrix();
1085     glMatrixMode(GL_MODELVIEW);
1086     glPopMatrix();
1087 }
1088
1089 void drawHUD()
1090 {
1091     if( !HUD_deque.size() ) {  // Trust everyone, but ALWAYS cut the cards!
1092         return;
1093     }
1094
1095     HUD_TextList.erase();
1096     HUD_LineList.erase();
1097     // HUD_StippleLineList.erase();
1098   
1099     glDisable(GL_DEPTH_TEST);
1100     glDisable(GL_LIGHTING);
1101
1102     static const SGPropertyNode * antialiased_node
1103         = fgGetNode("/sim/hud/antialiased");
1104
1105     if( antialiased_node->getBoolValue() ) {
1106         glEnable(GL_LINE_SMOOTH);
1107         //        glEnable(GL_BLEND);
1108         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1109         glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
1110         glLineWidth(2.0);
1111     } else {
1112         glLineWidth(1.0);
1113     }
1114
1115     if( global_day_night_switch == HUD_DAY) {
1116         switch (HUD_deque[0]->get_brightness())
1117             {
1118             case HUD_BRT_LIGHT:
1119                 set_hud_color (0.1f, 0.9f, 0.1f);
1120                 break;
1121
1122             case HUD_BRT_MEDIUM:
1123                 set_hud_color (0.1f, 0.7f, 0.0f);
1124                 break;
1125
1126             case HUD_BRT_DARK:
1127                 set_hud_color (0.0f, 0.6f, 0.0f);
1128                 break;
1129
1130             case HUD_BRT_BLACK:
1131                 set_hud_color( 0.0f, 0.0f, 0.0f);
1132                 break;
1133
1134             default:
1135                 set_hud_color (0.1f, 0.9f, 0.1f);
1136             }
1137     } else {
1138         if( global_day_night_switch == HUD_NIGHT) {
1139             switch (HUD_deque[0]->get_brightness())
1140                 {
1141                 case HUD_BRT_LIGHT:
1142                     set_hud_color (0.9f, 0.1f, 0.1f);
1143                     break;
1144
1145                 case HUD_BRT_MEDIUM:
1146                     set_hud_color (0.7f, 0.0f, 0.1f);
1147                     break;
1148
1149                 case HUD_BRT_DARK:
1150                     set_hud_color (0.6f, 0.0f, 0.0f);
1151                     break;
1152
1153                 case HUD_BRT_BLACK:
1154                     set_hud_color( 0.0f, 0.0f, 0.0f);
1155                     break;
1156
1157                 default:
1158                     set_hud_color (0.6f, 0.0f, 0.0f);
1159                 }
1160         } else {     // Just in case default
1161             set_hud_color (0.1f, 0.9f, 0.1f);
1162         }
1163     }
1164
1165     for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
1166
1167     HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
1168
1169 #ifdef FG_NETWORK_OLK
1170     if ( net_hud_display ) {
1171         net_hud_update();
1172     }
1173 #endif
1174
1175
1176     int apY = 480 - 80;
1177     // char scratch[128];
1178     // HUD_TextList.add( fgText( "AUTOPILOT", 20, apY) );
1179     // apY -= 15;
1180     FGAutopilot *AP = globals->get_autopilot();
1181     
1182     if( AP->get_HeadingEnabled() ) {
1183         HUD_TextList.add( fgText( 40, apY, AP->get_TargetHeadingStr()) );
1184         apY -= 15;
1185     }
1186     if( AP->get_AltitudeEnabled() ) {
1187         HUD_TextList.add( fgText( 40, apY, AP->get_TargetAltitudeStr()) );
1188         apY -= 15;
1189     }
1190     if( AP->get_HeadingMode() == FGAutopilot::FG_HEADING_WAYPOINT )
1191     {
1192         if ( strlen( AP->get_TargetWP1Str() ) ) {
1193             HUD_TextList.add( fgText( 40, apY, AP->get_TargetWP1Str() ) );
1194             apY -= 15;
1195         }
1196         if ( strlen( AP->get_TargetWP2Str() ) ) {
1197             HUD_TextList.add( fgText( 40, apY, AP->get_TargetWP2Str() ) );
1198             apY -= 15;
1199         }
1200         if ( strlen( AP->get_TargetWP3Str() ) ) {
1201             HUD_TextList.add( fgText( 40, apY, AP->get_TargetWP3Str() ) );
1202                 apY -= 15;
1203         }
1204     }
1205   
1206     HUD_TextList.draw();
1207
1208     HUD_LineList.draw();
1209
1210     // glEnable(GL_LINE_STIPPLE);
1211     // glLineStipple( 1, 0x00FF );
1212     // HUD_StippleLineList.draw();
1213     // glDisable(GL_LINE_STIPPLE);
1214
1215     if( antialiased_node->getBoolValue() ) {
1216         // glDisable(GL_BLEND);
1217         glDisable(GL_LINE_SMOOTH);
1218         glLineWidth(1.0);
1219     }
1220
1221     glEnable(GL_DEPTH_TEST);
1222     glEnable(GL_LIGHTING);
1223 }
1224