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