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