]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.cxx
5ff98efa4853d20aee75fee0862b17e1cc740d76
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #include <simgear/compiler.h>
24 #include <simgear/structure/exception.hxx>
25
26 #include STL_STRING
27 #include STL_FSTREAM
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #ifdef HAVE_WINDOWS_H
34 #  include <windows.h>
35 #endif
36
37 #ifdef __BORLANDC__
38 #  define exception c_exception
39 #endif
40
41 #include <math.h>
42
43 #include <stdlib.h>
44 #include <stdio.h>              // char related functions
45 #include <string.h>             // strcmp()
46
47 #include SG_GLU_H
48
49 #include <simgear/constants.h>
50 #include <simgear/debug/logstream.hxx>
51 #include <simgear/props/props.hxx>
52 #include <simgear/misc/sg_path.hxx>
53
54 #include <Aircraft/aircraft.hxx>
55 #include <Autopilot/xmlauto.hxx>
56 #include <GUI/new_gui.hxx>
57 #include <Main/globals.hxx>
58 #include <Main/fg_props.hxx>
59 #include <Scenery/scenery.hxx>
60
61 #if defined (__sun) || defined ( __sgi )
62 extern "C" {
63     extern void *memmove(void *, const void *, size_t);
64 }
65 #endif
66
67 #include "hud.hxx"
68
69
70 static HUD_Properties *HUD;
71
72 static char units[5];
73
74 // The following routines obtain information concerning the aircraft's
75 // current state and return it to calling instrument display routines.
76 // They should eventually be member functions of the aircraft.
77 //
78
79 deque< instr_item * > HUD_deque;
80
81 fgTextList         HUD_TextList;
82 fgLineList         HUD_LineList;
83 fgLineList         HUD_StippleLineList;
84
85 fntRenderer *HUDtext = 0;
86 fntTexFont *HUD_Font = 0;
87 float HUD_TextSize = 0;
88 int HUD_style = 0;
89
90 float HUD_matrix[16];
91
92
93 //$$$ begin - added, Neetha, 28 Nov 2k
94
95 static string   name;
96 static int              x;
97 static int              y;
98 static UINT    width;
99 static UINT    height;
100 static float    factor;
101 static float   span_units;
102 static float   division_units;
103 static float   minor_division = 0;
104 static UINT    screen_hole;
105 static UINT    lbl_pos;
106 static bool    working;
107 static string  loadfn;
108 static UINT     options;
109 static float   maxValue;
110 static float   minValue;
111 static float   scaling;
112 static UINT    major_divs;
113 static UINT    minor_divs;
114 static UINT    modulator;
115 static int     dp_showing = 0;
116 static string  label_format;
117 static string  prelabel;
118 static string  postlabel;
119 static int     justi;
120 static int              blinking;
121 static float   maxBankAngle;
122 static float   maxSlipAngle;
123 static UINT    gap_width;
124 static bool     latitude;
125 static bool     longitude;
126 static bool     tick_bottom;
127 static bool     tick_top;
128 static bool     tick_right;
129 static bool     tick_left;
130 static bool     cap_bottom;
131 static bool     cap_top;
132 static bool     cap_right;
133 static bool     cap_left;
134 static float   marker_off;
135 static string  type;
136 static bool    enable_pointer;
137 static string  type_pointer;
138 static bool    frl_spot;
139 static bool     target;
140 static bool    vel_vector;
141 static bool    drift;
142 static bool    alpha;
143 static bool     energy;
144 static bool     climb_dive;
145 static bool     glide;
146 static float    glide_slope_val;
147 static bool     worm_energy;
148 static bool     waypoint;
149 static string type_tick;//hud
150 static string length_tick;//hud
151 static bool label_box;//hud
152 static int digits; //suma
153 static float radius; //suma
154 static int divisions; //suma
155 static int zoom; //suma
156 static int zenith; //suma
157 static int nadir ; //suma
158 static int hat; //suma
159 static bool tsi; //suma
160 static float rad; //suma
161
162
163 static FLTFNPTR load_fn;    
164 static fgLabelJust justification;
165 static const char *pre_label_string  = 0;
166 static const char *post_label_string = 0;
167
168 int readHud( istream &input );
169 int readInstrument ( const SGPropertyNode * node);
170 static instr_item * readLadder ( const SGPropertyNode * node);
171 static instr_item * readCard ( const SGPropertyNode * node);
172 static instr_item * readLabel( const SGPropertyNode * node);
173 static instr_item * readTBI( const SGPropertyNode * node);
174 //$$$ end   - added, Neetha, 28 Nov 2k
175
176 static void drawHUD();
177 static void fgUpdateHUDVirtual();
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 #ifdef ENABLE_SP_FMDS
460     if ( loadfn== "aux1" ) {
461         load_fn = get_aux1;
462     } else if ( loadfn == "aux2" ) {
463         load_fn = get_aux2;
464     } else if ( loadfn == "aux3" ) {
465         load_fn = get_aux3;
466     } else if ( loadfn == "aux4" ) {
467         load_fn = get_aux4;
468     } else if ( loadfn == "aux5" ) {
469         load_fn = get_aux5;
470     } else if ( loadfn == "aux6" ) {
471         load_fn = get_aux6;
472     } else if ( loadfn == "aux7" ) {
473         load_fn = get_aux7;
474     } else if ( loadfn == "aux8" ) {
475         load_fn = get_aux8;
476     } else if ( loadfn == "aux9" ) {
477         load_fn = get_aux9;
478     } else if ( loadfn == "aux10" ) {
479         load_fn = get_aux10;
480     } else if ( loadfn == "aux11" ) {
481         load_fn = get_aux11;
482     } else if ( loadfn == "aux12" ) {
483         load_fn = get_aux12;
484     } else if ( loadfn == "aux13" ) {
485         load_fn = get_aux13;
486     } else if ( loadfn == "aux14" ) {
487         load_fn = get_aux14;
488     } else if ( loadfn == "aux15" ) {
489         load_fn = get_aux15;
490     } else if ( loadfn == "aux16" ) {
491         load_fn = get_aux16;
492     } else if ( loadfn == "aux17" ) {
493         load_fn = get_aux17;
494     } else if ( loadfn == "aux18" ) {
495         load_fn = get_aux18;
496     } else
497 #endif
498       if ( loadfn == "ax" ) {
499         load_fn = get_Ax;
500     } else if ( loadfn == "speed" ) {
501         load_fn = get_speed;
502     } else if ( loadfn == "mach" ) {
503         load_fn = get_mach;
504     } else if ( loadfn == "altitude" ) {
505         load_fn = get_altitude;
506     } else if ( loadfn == "agl" ) {
507         load_fn = get_agl;
508     } else if ( loadfn == "framerate" ) {
509         load_fn = get_frame_rate;
510     } else if ( loadfn == "heading" ) {
511         load_fn = get_heading;
512     } else if ( loadfn == "fov" ) {
513         load_fn = get_fov;
514     } else if ( loadfn == "vfc_tris_culled" ) {
515         load_fn = get_vfc_tris_culled;
516     } else if ( loadfn == "vfc_tris_drawn" ) {
517         load_fn = get_vfc_tris_drawn;
518     } else if ( loadfn == "aoa" ) {
519         load_fn = get_aoa;
520     } else if ( loadfn == "latitude" ) {
521         load_fn  = get_latitude;
522     } else if ( loadfn == "anzg" ) {
523         load_fn = get_anzg;
524     } else if ( loadfn == "longitude" ) {
525         load_fn   = get_longitude;
526     } else if (loadfn=="throttleval") {
527         load_fn = get_throttleval;
528     }
529
530     p = (instr_item *) new instr_label ( x,
531                                          y,
532                                          width,
533                                          height,
534                                          load_fn,
535                                          label_format.c_str(),
536                                          pre_label_string,
537                                          post_label_string,
538                                          scaling,
539                                          options,
540                                          justification,
541                                          font_size,
542                                          blinking,
543                                          latitude,
544                                          longitude,
545                                          label_box, //hud
546                                          working,
547                                          digits); //suma
548
549     return p;
550 } // end readLabel
551
552 static instr_item * 
553 readTBI(const SGPropertyNode * node)
554 {
555
556     instr_item *p;
557
558     name           = node->getStringValue("name");
559     x              = node->getIntValue("x");
560     y              = node->getIntValue("y");
561     width          = node->getIntValue("width");
562     height         = node->getIntValue("height");
563     maxBankAngle   = node->getFloatValue("maxBankAngle");
564     maxSlipAngle   = node->getFloatValue("maxSlipAngle");
565     gap_width      = node->getIntValue("gap_width");
566     working        = node->getBoolValue("working");
567     tsi                    = node->getBoolValue("tsi"); //suma
568     rad                    = node->getFloatValue("rad"); //suma
569
570     SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
571
572
573     p = (instr_item *) new fgTBI_instr( x,
574                                         y,  
575                                         width,
576                                         height,
577                                         get_roll,
578                                         get_sideslip,
579                                         maxBankAngle, 
580                                         maxSlipAngle,
581                                         gap_width,
582                                         working,
583                                         tsi, //suma
584                                         rad); //suma
585
586     return p;
587 } //end readTBI
588
589 static instr_item * 
590 readRunway(const SGPropertyNode * node) {
591         name    = node->getStringValue("name");
592         x       = node->getIntValue("x");
593         y       = node->getIntValue("y");
594         width = node->getIntValue("width");
595         height = node->getIntValue("height");
596         scaling = node->getDoubleValue("scale");
597         working = node->getBoolValue("working",true);
598         runway_instr *ri = new runway_instr(x,y,width,height,scaling,working);
599         double scale = node->getDoubleValue("arrow_scale",1.0); 
600         ri->setDrawArrow((scale>0)?true:false);
601         ri->setDrawArrowAlways((scale>0)?node->getBoolValue("arrow_always"):false);
602         ri->setStippleOutline(node->getIntValue("outer_stipple",0xFFFF));
603         ri->setStippleCenterline(node->getIntValue("center_stipple",0xFFFF));
604         ri->setArrowRotationRadius(node->getDoubleValue("arrow_radius"));
605         ri->setArrowScale(scale);
606         ri->setLineScale(node->getDoubleValue("line_scale",1.0));
607         ri->setScaleDist(node->getDoubleValue("scale_dist_nm"));
608         SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
609         return (instr_item *) ri;
610 }
611
612
613 int readInstrument(const SGPropertyNode * node)
614 {
615     static const SGPropertyNode *startup_units_node
616         = fgGetNode("/sim/startup/units");
617
618     instr_item *HIptr;
619     
620     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
621         strcpy(units, " ft");
622     } else {
623         strcpy(units, " m");
624     }
625
626     const SGPropertyNode * ladder_group = node->getNode("ladders");
627
628     if (ladder_group != 0) {
629         int nLadders = ladder_group->nChildren();
630         for (int j = 0; j < nLadders; j++) {
631             
632             HIptr = readLadder(ladder_group->getChild(j));
633             HUD_deque.insert( HUD_deque.begin(), HIptr);
634                                         
635         }// for - ladders
636     }
637
638     const SGPropertyNode * card_group = node->getNode("cards");
639     if (card_group != 0) {
640         int nCards = card_group->nChildren();
641         for (int j = 0; j < nCards; j++) {
642             
643             HIptr = readCard(card_group->getChild(j));
644             HUD_deque.insert( HUD_deque.begin(), HIptr);
645
646         }//for - cards
647     }
648
649     const SGPropertyNode * label_group = node->getNode("labels");
650     if (label_group != 0) {
651         int nLabels = label_group->nChildren();
652         for (int j = 0; j < nLabels; j++) {
653
654             HIptr = readLabel(label_group->getChild(j));
655             HUD_deque.insert( HUD_deque.begin(), HIptr);
656
657         }//for - labels
658     }
659
660     const SGPropertyNode * tbi_group = node->getNode("tbis");
661     if (tbi_group != 0) {
662         int nTbis = tbi_group->nChildren();
663         for (int j = 0; j < nTbis; j++) {
664
665             HIptr = readTBI(tbi_group->getChild(j));
666             HUD_deque.insert( HUD_deque.begin(), HIptr);
667
668         }//for - tbis
669     }
670     
671     const SGPropertyNode * rwy_group = node->getNode("runways");
672     if (rwy_group != 0) {
673         int nRwy = rwy_group->nChildren();
674         for (int j = 0; j < nRwy; j++) {
675             SG_LOG( SG_COCKPIT, SG_DEBUG,
676                     "**************  Reading runway properties" );
677             HIptr = readRunway(rwy_group->getChild(j));
678             HUD_deque.insert( HUD_deque.begin(), HIptr);
679
680         }//for - runways
681     }    
682     return 0;
683 }//end readinstrument
684
685
686 int readHud( istream &input ) 
687 {
688
689     SGPropertyNode root;
690
691     try {
692         readProperties(input, &root);
693     } catch (const sg_exception &e) {
694         guiErrorMessage("Error reading HUD: ", e);
695         return 0;
696     }
697   
698         
699     SG_LOG(SG_INPUT, SG_INFO, "Read properties for  " <<
700            root.getStringValue("name"));
701
702
703     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());  // empty the HUD deque
704
705
706     SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
707
708     const SGPropertyNode * instrument_group = root.getChild("instruments");
709     int nInstruments = instrument_group->nChildren();
710
711     for (int i = 0; i < nInstruments; i++) {
712                 
713         const SGPropertyNode * node = instrument_group->getChild(i);
714
715         SGPath path( globals->get_fg_root() );
716         path.append(node->getStringValue("path"));
717
718         SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
719                << node->getName()
720                << " from "
721                << path.str());
722
723         SGPropertyNode root2;
724         try {
725             readProperties(path.str(), &root2);
726         } catch (const sg_exception &e) {
727             guiErrorMessage("Error reading HUD instrument: ", e);
728             continue;
729         } 
730         readInstrument(&root2);
731     }//for loop(i)
732
733     return 0;
734 }
735
736
737 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
738 {
739
740     HUD_style = 1;
741
742     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
743
744     string hud_path =
745         fgGetString("/sim/hud/path", "Huds/Default/default.xml");
746     SGPath path(globals->get_fg_root());
747     path.append(hud_path);
748         
749     ifstream input(path.c_str());
750     if (!input.good()) {
751         SG_LOG(SG_INPUT, SG_ALERT,
752                "Cannot read Hud configuration from " << path.str());
753     } else {
754         readHud(input);
755         input.close();
756     }
757
758     fgHUDReshape();
759
760     if ( HUDtext ) {
761         // this chunk of code is not necessarily thread safe if the
762         // compiler optimizer reorders these statements.  Note that
763         // "delete ptr" does not set "ptr = NULL".  We have to do that
764         // ourselves.
765         fntRenderer *tmp = HUDtext;
766         HUDtext = NULL;
767         delete tmp;
768     }
769
770     FGFontCache *fc = globals->get_fontcache();
771     HUD_Font = fc->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
772     if (!HUD_Font)
773         throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
774
775     HUD_TextSize = fgGetFloat("/sim/hud/font/size", 10);
776
777     HUDtext = new fntRenderer();
778     HUDtext->setFont(HUD_Font);
779     HUDtext->setPointSize(HUD_TextSize);
780     HUD_TextList.setFont( HUDtext );
781
782     HUD = new HUD_Properties;
783     return 0;  // For now. Later we may use this for an error code.
784
785 }
786
787 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
788 {
789
790     HUD_style = 2;
791
792     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
793
794     SGPath path(globals->get_fg_root());
795     path.append("Huds/Minimal/default.xml");
796
797
798     ifstream input(path.c_str());
799     if (!input.good()) {
800         SG_LOG(SG_INPUT, SG_ALERT,
801                "Cannot read Hud configuration from " << path.str());
802     } else {
803         readHud(input);
804         input.close();
805     }
806
807     return 0;  // For now. Later we may use this for an error code.
808
809 }
810 //$$$ End - added, Neetha, 28 Nov 2k  
811
812
813 void fgHUDReshape(void) {
814 #if 0
815     if ( HUDtext ) {
816         // this chunk of code is not necessarily thread safe if the
817         // compiler optimizer reorders these statements.  Note that
818         // "delete ptr" does not set "ptr = NULL".  We have to do that
819         // ourselves.
820         fntRenderer *tmp = HUDtext;
821         HUDtext = NULL;
822         delete tmp;
823     }
824
825     HUD_TextSize = fgGetInt("/sim/startup/xsize") / 60;
826     HUD_TextSize = 10;
827     HUDtext = new fntRenderer();
828     HUDtext -> setFont      ( guiFntHandle ) ;
829     HUDtext -> setPointSize ( HUD_TextSize ) ;
830     HUD_TextList.setFont( HUDtext );
831 #endif
832 }
833
834
835 // fgUpdateHUD
836 //
837 // Performs a once around the list of calls to instruments installed in
838 // the HUD object with requests for redraw. Kinda. It will when this is
839 // all C++.
840 //
841 void fgUpdateHUD( void ) {
842         
843     static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
844     if( HUD_style == 1 && enable3d_node->getBoolValue() )
845     {
846         fgUpdateHUDVirtual();
847         return;
848     }
849     
850     static const float normal_aspect = float(640) / float(480);
851     // note: aspect_ratio is Y/X
852     float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
853     if( current_aspect > normal_aspect ) {
854         float aspect_adjust = current_aspect / normal_aspect;
855         float adjust = 320.0f*aspect_adjust - 320.0f;
856         fgUpdateHUD( -adjust, 0.0f, 640.0f+adjust, 480.0f );
857     } else {
858         float aspect_adjust = normal_aspect / current_aspect;
859         float adjust = 240.0f*aspect_adjust - 240.0f;
860         fgUpdateHUD( 0.0f, -adjust, 640.0f, 480.0f+adjust );
861     }
862 }
863
864 void fgUpdateHUDVirtual()
865 {
866     FGViewer* view = globals->get_current_view();
867
868     // Standard fgfs projection, with essentially meaningless clip
869     // planes (we'll map the whole HUD plane to z=-1)
870     glMatrixMode(GL_PROJECTION);
871     glPushMatrix();
872     glLoadIdentity();
873     gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
874
875     glMatrixMode(GL_MODELVIEW);
876     glPushMatrix();
877     glLoadIdentity();
878   
879     // Standard fgfs view direction computation
880     float lookat[3];
881     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
882     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
883     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
884     if(fabs(lookat[1]) > 9999) lookat[1] = 9999; // FPU sanity
885     gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
886
887     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
888     // This is the default fgfs field of view, which the HUD files are
889     // written to assume.
890     float dx = 0.52056705; // tan(55/2)
891     float dy = dx * 0.75;  // assumes 4:3 aspect ratio
892     float m[16];
893     m[0] = dx; m[4] =  0; m[ 8] = 0; m[12] = 0;
894     m[1] =  0; m[5] = dy; m[ 9] = 0; m[13] = 0;
895     m[2] =  0; m[6] =  0; m[10] = 1; m[14] = 0;
896     m[3] =  0; m[7] =  0; m[11] = 0; m[15] = 1;
897     glMultMatrixf(m);
898
899     // Convert the 640x480 "HUD standard" coordinate space to a square
900     // about the origin in the range [-1:1] at depth of -1
901     glScalef(1./320, 1./240, 1);
902     glTranslatef(-320, -240, -1);
903
904     // Do the deed
905     drawHUD();
906
907     // Clean up our mess
908     glMatrixMode(GL_PROJECTION);
909     glPopMatrix();
910     glMatrixMode(GL_MODELVIEW);
911     glPopMatrix();
912 }
913
914 void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
915                   GLfloat x_end, GLfloat y_end )
916 {
917     glMatrixMode(GL_PROJECTION);
918     glPushMatrix();
919     glLoadIdentity();
920     gluOrtho2D(x_start, x_end, y_start, y_end);
921
922     glMatrixMode(GL_MODELVIEW);
923     glPushMatrix();
924     glLoadIdentity();
925
926     drawHUD();
927
928     glMatrixMode(GL_PROJECTION);
929     glPopMatrix();
930     glMatrixMode(GL_MODELVIEW);
931     glPopMatrix();
932 }
933
934 void drawHUD()
935 {
936     if( !HUD_deque.size() ) {  // Trust everyone, but ALWAYS cut the cards!
937         return;
938     }
939
940     HUD_TextList.erase();
941     HUD_LineList.erase();
942     // HUD_StippleLineList.erase();
943   
944     glDisable(GL_DEPTH_TEST);
945     glDisable(GL_LIGHTING);
946
947     static const SGPropertyNode *heading_enabled
948         = fgGetNode("/autopilot/locks/heading", true);
949     static const SGPropertyNode *altitude_enabled
950         = fgGetNode("/autopilot/locks/altitude", true);
951
952     static char hud_hdg_text[256];
953     static char hud_wp0_text[256];
954     static char hud_wp1_text[256];
955     static char hud_wp2_text[256];
956     static char hud_alt_text[256];
957
958     if (HUD->isAntialiased()) {
959         glEnable(GL_LINE_SMOOTH);
960         //        glEnable(GL_BLEND);
961         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
962         glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
963         glLineWidth(2.0);
964     } else {
965         glLineWidth(1.0);
966     }
967
968     HUD->setColor();
969     for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
970
971     HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
972
973
974     int apY = 480 - 80;
975     
976     
977     if (strcmp( heading_enabled->getStringValue(), "dg-heading-hold") == 0 ) {
978         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
979                   fgGetDouble("/autopilot/settings/heading-bug-deg") );
980         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
981         apY -= 15;
982     } else if ( strcmp(heading_enabled->getStringValue(), "true-heading-hold") == 0 ) {
983         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
984                   fgGetDouble("/autopilot/settings/true-heading-deg") );
985         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
986         apY -= 15;
987
988         string wp0_id = fgGetString( "/autopilot/route-manager/wp[0]/id" );
989         if ( wp0_id.length() > 0 ) {
990             snprintf( hud_wp0_text, 256, "%5s %6.1fnm %s", wp0_id.c_str(), 
991                       fgGetDouble( "/autopilot/route-manager/wp[0]/dist" ),
992                       fgGetString( "/autopilot/route-manager/wp[0]/eta" ) );
993             HUD_TextList.add( fgText( 40, apY, hud_wp0_text ) );
994             apY -= 15;
995         }
996         string wp1_id = fgGetString( "/autopilot/route-manager/wp[1]/id" );
997         if ( wp1_id.length() > 0 ) {
998             snprintf( hud_wp1_text, 256, "%5s %6.1fnm %s", wp1_id.c_str(), 
999                       fgGetDouble( "/autopilot/route-manager/wp[1]/dist" ),
1000                       fgGetString( "/autopilot/route-manager/wp[1]/eta" ) );
1001             HUD_TextList.add( fgText( 40, apY, hud_wp1_text ) );
1002             apY -= 15;
1003         }
1004         string wp2_id = fgGetString( "/autopilot/route-manager/wp-last/id" );
1005         if ( wp2_id.length() > 0 ) {
1006             snprintf( hud_wp2_text, 256, "%5s %6.1fnm %s", wp2_id.c_str(), 
1007                       fgGetDouble( "/autopilot/route-manager/wp-last/dist" ),
1008                       fgGetString( "/autopilot/route-manager/wp-last/eta" ) );
1009             HUD_TextList.add( fgText( 40, apY, hud_wp2_text ) );
1010             apY -= 15;
1011         }
1012     }
1013   
1014     if ( strcmp( altitude_enabled->getStringValue(), "altitude-hold" ) == 0 ) {
1015         snprintf( hud_alt_text, 256, "alt = %.0f\n",
1016                   fgGetDouble("/autopilot/settings/target-altitude-ft") );
1017         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
1018         apY -= 15;
1019     } else if ( strcmp( altitude_enabled->getStringValue(), "agl-hold" ) == 0 ){
1020         snprintf( hud_alt_text, 256, "agl = %.0f\n",
1021                   fgGetDouble("/autopilot/settings/target-agl-ft") );
1022         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
1023         apY -= 15;
1024     }
1025
1026     HUD_TextList.draw();
1027
1028     HUD_LineList.draw();
1029
1030     // glEnable(GL_LINE_STIPPLE);
1031     // glLineStipple( 1, 0x00FF );
1032     // HUD_StippleLineList.draw();
1033     // glDisable(GL_LINE_STIPPLE);
1034
1035     if (HUD->isAntialiased()) {
1036         // glDisable(GL_BLEND);
1037         glDisable(GL_LINE_SMOOTH);
1038         glLineWidth(1.0);
1039     }
1040
1041     glEnable(GL_DEPTH_TEST);
1042     glEnable(GL_LIGHTING);
1043 }
1044
1045
1046
1047
1048
1049 // HUD property listener class
1050 //
1051 HUD_Properties::HUD_Properties()
1052 {
1053     _colors = fgGetNode("/sim/hud/colors", true)->getChildren("color");
1054     _which = fgGetNode("/sim/hud/current-color", true);
1055     _brightness = fgGetNode("/sim/hud/brightness", true);
1056     _alpha = fgGetNode("/sim/hud/alpha", true);
1057     _visibility = fgGetNode("/sim/hud/visibility", true);
1058     _antialiasing = fgGetNode("/sim/hud/antialiased", true);
1059
1060     _which->addChangeListener(this);
1061     _brightness->addChangeListener(this);
1062     _alpha->addChangeListener(this);
1063     _visibility->addChangeListener(this);
1064     _antialiasing->addChangeListener(this, true);
1065 }
1066
1067
1068 void HUD_Properties::valueChanged(SGPropertyNode *n)
1069 {
1070     _visible = _visibility->getBoolValue();
1071     _antialiased = _antialiasing->getBoolValue();
1072     float brt = _brightness->getFloatValue();
1073     int w = _which->getIntValue();
1074     if (w < 0 || w >= int(_colors.size()))
1075         _visible = false;
1076     else {
1077         _r = clamp(brt * _colors[w]->getFloatValue("red", 1.0));
1078         _g = clamp(brt * _colors[w]->getFloatValue("green", 1.0));
1079         _b = clamp(brt * _colors[w]->getFloatValue("blue", 1.0));
1080     }
1081     _a = _alpha->getFloatValue();
1082 }
1083
1084
1085 void HUD_Properties::setColor() const
1086 {
1087     if (_antialiased)
1088         glColor4f(_r, _g, _b, _a);
1089     else
1090         glColor3f(_r, _g, _b);
1091 }
1092