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