1 // hud.hxx -- hud defines and prototypes (initial draft)
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
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.
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.
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.
28 # error This library requires C++
44 # include <values.h> // for MAXINT
47 #include <vector> // STL vector
48 #include <deque> // STL double ended queue
50 #include <simgear/constants.h>
51 #include <simgear/math/mat3.h>
53 #include <fg_typedefs.h>
54 #include <Aircraft/aircraft.hxx>
55 #include <FDM/flight.hxx>
56 #include <Controls/controls.hxx>
61 FG_USING_NAMESPACE(std);
70 int top, bottom, left, right;
74 // View mode definitions
76 enum VIEW_MODES{ HUD_VIEW, PANEL_VIEW, CHASE_VIEW, TOWER_VIEW };
78 // DAY, NIGHT and brightness levels need to be visible where dialogs and
79 // controls can be used to set intensity and appropriate color. This will
81 // Hud general constants
98 enum fgLabelJust{ LEFT_JUST, CENTER_JUST, RIGHT_JUST } ;
103 #define UPPER_CENTER 3
104 #define UPPER_RIGHT 4
105 #define CENTER_RIGHT 5
106 #define LOWER_RIGHT 6
107 #define LOWER_CENTER 7
109 #define CENTER_LEFT 9
110 #define SOLID_LINES 10
111 #define DASHED_LINES 11
112 #define DASHED_NEG_LINES 12
115 #define HORIZON_FIXED 1
116 #define HORIZON_MOVING 2
117 #define LABEL_COUNTER 1
118 #define LABEL_WARNING 2
120 #define HUDS_AUTOTICKS 0x0001
121 #define HUDS_VERT 0x0002
122 #define HUDS_HORZ 0x0000
123 #define HUDS_TOP 0x0004
124 #define HUDS_BOTTOM 0x0008
125 #define HUDS_LEFT HUDS_TOP
126 #define HUDS_RIGHT HUDS_BOTTOM
127 #define HUDS_BOTH (HUDS_LEFT | HUDS_RIGHT)
128 #define HUDS_NOTICKS 0x0010
129 #define HUDS_ARITHTIC 0x0020
130 #define HUDS_DECITICS 0x0040
131 #define HUDS_NOTEXT 0x0080
134 // #define HUD_VERTICAL 1
135 // #define HUD_HORIZONTAL 2
136 // #define HUD_FREEFLOAT 3
138 // Ladder orientation modes
139 // #define HUD_LEFT 1
140 // #define HUD_RIGHT 2
142 // #define HUD_BOTTOM 2
143 // #define HUD_V_LEFT 1
144 // #define HUD_V_RIGHT 2
145 // #define HUD_H_TOP 1
146 // #define HUD_H_BOTTOM 2
151 // #define HUD_NOLIM 2
152 // #define HUD_CIRC 3
154 // #define HUD_INSTR_LADDER 1
155 // #define HUD_INSTR_CLADDER 2
156 // #define HUD_INSTR_HORIZON 3
157 // #define HUD_INSTR_LABEL 4
160 extern float get_throttleval ( void );
161 extern float get_aileronval ( void );
162 extern float get_elevatorval ( void );
163 extern float get_elev_trimval( void );
164 extern float get_rudderval ( void );
165 extern float get_speed ( void );
166 extern float get_aoa ( void );
167 extern float get_roll ( void );
168 extern float get_pitch ( void );
169 extern float get_heading ( void );
170 extern float get_view_direction( void );
171 extern float get_altitude ( void );
172 extern float get_agl ( void );
173 extern float get_sideslip ( void );
174 extern float get_frame_rate ( void );
175 extern float get_latitude ( void );
176 extern float get_lat_min ( void );
177 extern float get_longitude ( void );
178 extern float get_long_min ( void );
179 extern float get_fov ( void );
180 extern float get_vfc_ratio ( void );
181 extern float get_vfc_tris_drawn ( void );
182 extern float get_vfc_tris_culled ( void );
183 extern float get_climb_rate ( void );
184 extern float get_mach( void );
185 extern char *coord_format_lat(float);
186 extern char *coord_format_lon(float);
187 //extern char *coord_format_latlon(float latitude, float longitude); // cockpit.cxx
189 extern char *get_formated_gmt_time( void );
191 enum hudinstype{ HUDno_instr,
203 typedef struct gltagRGBTRIPLE { // rgbt
211 GLfloat x0, y0, x1, y1;
214 fgLineSeg2D( GLfloat a = 0, GLfloat b =0, GLfloat c = 0, GLfloat d =0 )
215 : x0(a), y0(b), x1(c), y1(d) {}
217 fgLineSeg2D( const fgLineSeg2D & image )
218 : x0(image.x0), y0(image.y0), x1(image.x1), y1(image.y1) {}
220 fgLineSeg2D& operator= ( const fgLineSeg2D & image ) {
221 x0 = image.x0; y0 = image.y0; x1 = image.x1; y1 = image.y1; return *this;
233 #define USE_HUD_TextList
234 extern float HUD_TextSize;
235 extern fntRenderer *HUDtext;
236 extern float HUD_matrix[16];
243 fgText( float x = 0, float y = 0, char *c = NULL )
244 : x(x), y(y) {strncpy(msg,c,64-1);}
246 fgText( const fgText & image )
247 : x(image.x), y(image.y) {strcpy(msg,image.msg);}
249 fgText& operator = ( const fgText & image ) {
250 strcpy(msg,image.msg); x = image.x; y = image.y;
254 ~fgText() {msg[0]='\0';}
256 int getStringWidth ( char *str )
258 if ( HUDtext && str )
261 guiFntHandle->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
262 return FloatToInt( r - l );
267 int StringWidth (void )
269 if ( HUDtext && strlen( msg ))
272 guiFntHandle->getBBox ( msg, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
273 return FloatToInt( r - l );
278 void Draw(fntRenderer *fnt)
280 fnt->start2f( x, y );
286 puDrawString ( guiFnt, msg, FloatToInt(x), FloatToInt(y) );
291 vector < fgLineSeg2D > List;
293 fgLineList( void ) {}
294 ~fgLineList( void ) {}
295 void add( fgLineSeg2D seg ) { List.push_back(seg); }
296 void erase( void ) { List.erase( List.begin(), List.end() ); }
298 vector < fgLineSeg2D > :: iterator curSeg;
299 vector < fgLineSeg2D > :: iterator lastSeg;
300 curSeg = List.begin();
301 lastSeg = List.end();
303 for ( ; curSeg != lastSeg; curSeg++ ) {
312 vector< fgText > List;
314 fgTextList ( void ) { Font = 0; }
315 ~fgTextList( void ) {}
317 void setFont( fntRenderer *Renderer ) { Font = Renderer; }
318 void add( fgText String ) { List.push_back(String); }
319 void erase( void ) { List.erase( List.begin(), List.end() ); }
322 vector < fgText > :: iterator curString;
323 vector < fgText > :: iterator lastString;
324 if( Font == 0 ) return;
325 curString = List.begin();
326 lastString = List.end();
327 glPushAttrib( GL_COLOR_BUFFER_BIT );
328 glEnable ( GL_ALPHA_TEST ) ;
329 glEnable ( GL_BLEND ) ;
330 glAlphaFunc ( GL_GREATER, 0.1 ) ;
331 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
334 for( ; curString != lastString; curString++ ) {
335 curString->Draw(Font);
339 glDisable ( GL_TEXTURE_2D ) ;
345 inline void Text( fgTextList &List, float x, float y, char *s)
347 List.add( fgText( x, y, s) );
350 inline void Text( fgTextList &List, fgText &me)
355 inline void Line( fgLineList &List, float x1, float y1, float x2, float y2)
357 List.add(fgLineSeg2D(x1,y1,x2,y2));
361 // Declare our externals
362 extern fgTextList HUD_TextList;
363 extern fgLineList HUD_LineList;
364 extern fgLineList HUD_StippleLineList;
367 class instr_item { // An Abstract Base Class (ABC)
369 static UINT instances; // More than 64K instruments? Nah!
370 static int brightness;
371 static glRGBTRIPLE color;
374 RECT scrn_pos; // Framing - affects scale dimensions
375 // and orientation. Vert vs Horz, etc.
376 FLTFNPTR load_value_fn;
377 float disp_factor; // Multiply by to get numbers shown on scale.
381 UINT scr_span; // Working values for draw;
389 FLTFNPTR data_source,
392 bool working = true);
394 instr_item( const instr_item & image );
396 instr_item & operator = ( const instr_item & rhs );
397 virtual ~instr_item ();
399 int get_brightness ( void ) { return brightness;}
400 RECT get_location ( void ) { return scrn_pos; }
401 bool is_broken ( void ) { return broken; }
402 bool enabled ( void ) { return is_enabled;}
403 bool data_available ( void ) { return !!load_value_fn; }
404 float get_value ( void ) { return load_value_fn(); }
405 float data_scaling ( void ) { return disp_factor; }
406 UINT get_span ( void ) { return scr_span; }
407 POINT get_centroid ( void ) { return mid_span; }
408 UINT get_options ( void ) { return opts; }
410 UINT huds_vert (UINT options) { return( options & HUDS_VERT ); }
411 UINT huds_left (UINT options) { return( options & HUDS_LEFT ); }
412 UINT huds_right (UINT options) { return( options & HUDS_RIGHT ); }
413 UINT huds_both (UINT options) { return( (options & HUDS_BOTH) == HUDS_BOTH ); }
414 UINT huds_noticks (UINT options) { return( options & HUDS_NOTICKS ); }
415 UINT huds_notext (UINT options) { return( options & HUDS_NOTEXT ); }
416 UINT huds_top (UINT options) { return( options & HUDS_TOP ); }
417 UINT huds_bottom (UINT options) { return( options & HUDS_BOTTOM ); }
419 virtual void display_enable( bool working ) { is_enabled = !! working;}
421 virtual void update( void );
422 virtual void break_display ( bool bad );
423 virtual void SetBrightness( int illumination_level ); // fgHUDSetBright...
424 void SetPosition ( int x, int y, UINT width, UINT height );
425 UINT get_Handle( void );
426 virtual void draw( void ) = 0; // Required method in derived classes
428 void drawOneLine( float x1, float y1, float x2, float y2)
430 HUD_LineList.add(fgLineSeg2D(x1,y1,x2,y2));
432 void drawOneStippleLine( float x1, float y1, float x2, float y2)
434 HUD_StippleLineList.add(fgLineSeg2D(x1,y1,x2,y2));
436 void TextString( char *msg, float x, float y )
438 HUD_TextList.add(fgText(x, y, msg));
440 int getStringWidth ( char *str )
442 if ( HUDtext && str )
445 guiFntHandle->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
446 return FloatToInt( r - l );
453 typedef instr_item *HIptr;
454 //typedef deque < instr_item * > hud_deque_type;
455 //typedef hud_deque_type::iterator hud_deque_iterator;
456 //typedef hud_deque_type::const_iterator hud_deque_const_iterator;
458 extern deque< instr_item *> HUD_deque;
459 extern int HUD_style;
460 //extern hud_deque_type HUD_deque;
462 // instr_item This class has no other purpose than to maintain
463 // a linked list of instrument and derived class
467 class instr_label : public instr_item {
471 const char *post_str;
475 char format_buffer[80];
482 FLTFNPTR data_source,
483 const char *label_format,
484 const char *pre_label_string = 0,
485 const char *post_label_string = 0,
486 float scale_data = 1.0,
487 UINT options = HUDS_TOP,
488 fgLabelJust justification = CENTER_JUST,
489 int font_size = SMALL,
490 int blinking = NOBLINK,
491 bool working = true);
495 instr_label( const instr_label & image);
496 instr_label & operator = (const instr_label & rhs );
497 virtual void draw( void ); // Required method in base class
500 typedef instr_label * pInstlabel;
503 class lat_label : public instr_item {
507 const char *post_str;
511 char format_buffer[80];
518 FLTFNPTR data_source,
519 const char *label_format,
520 const char *pre_label_string = 0,
521 const char *post_label_string = 0,
522 float scale_data = 1.0,
523 UINT options = HUDS_TOP,
524 fgLabelJust justification = CENTER_JUST,
525 int font_size = SMALL,
526 int blinking = NOBLINK,
527 bool working = true);
531 lat_label( const lat_label & image);
532 lat_label & operator = (const lat_label & rhs );
533 virtual void draw( void ); // Required method in base class
536 typedef lat_label * pLatlabel;
538 class lon_label : public instr_item {
542 const char *post_str;
546 char format_buffer[80];
553 FLTFNPTR data_source,
554 const char *label_format,
555 const char *pre_label_string = 0,
556 const char *post_label_string = 0,
557 float scale_data = 1.0,
558 UINT options = HUDS_TOP,
559 fgLabelJust justification = CENTER_JUST,
560 int font_size = SMALL,
561 int blinking = NOBLINK,
562 bool working = true);
566 lon_label( const lon_label & image);
567 lon_label & operator = (const lon_label & rhs );
568 virtual void draw( void ); // Required method in base class
571 typedef lon_label * pLonlabel;
574 // instr_scale This class is an abstract base class for both moving
575 // scale and moving needle (fixed scale) indicators. It
576 // does not draw itself, but is not instanciable.
579 class instr_scale : public instr_item {
581 float range_shown; // Width Units.
582 float Maximum_value; // ceiling.
583 float Minimum_value; // Representation floor.
584 float scale_factor; // factor => screen units/range values.
585 UINT Maj_div; // major division marker units
586 UINT Min_div; // minor division marker units
587 UINT Modulo; // Roll over point
588 int signif_digits; // digits to show to the right.
598 float max_value = 100.0,
599 float min_value = 0.0,
600 float disp_scaling = 1.0,
601 UINT major_divs = 10,
605 bool working = true);
607 virtual ~instr_scale();
608 instr_scale( const instr_scale & image);
609 instr_scale & operator = (const instr_scale & rhs);
611 virtual void draw ( void ) {}; // No-op here. Defined in derived classes.
612 UINT div_min ( void ) { return Min_div;}
613 UINT div_max ( void ) { return Maj_div;}
614 float min_val ( void ) { return Minimum_value;}
615 float max_val ( void ) { return Maximum_value;}
616 UINT modulo ( void ) { return Modulo; }
617 float factor ( void ) { return scale_factor;}
618 float range_to_show( void ) { return range_shown;}
621 // hud_card_ This class displays the indicated quantity on
622 // a scale that moves past the pointer. It may be
623 // horizontal or vertical, read above(left) or below(right) of the base
626 class hud_card : public instr_scale {
629 float half_width_units;
638 float maxValue = 100.0,
639 float minValue = 0.0,
640 float disp_scaling = 1.0,
641 UINT major_divs = 10,
643 UINT modulator = 100,
645 float value_span = 100.0,
646 bool working = true);
649 hud_card( const hud_card & image);
650 hud_card & operator = (const hud_card & rhs );
651 // virtual void display_enable( bool setting );
652 virtual void draw( void ); // Required method in base class
655 typedef hud_card * pCardScale;
657 class guage_instr : public instr_scale {
665 float disp_scaling = 1.0,
666 float maxValue = 100,
668 UINT major_divs = 50,
672 bool working = true);
675 guage_instr( const guage_instr & image);
676 guage_instr & operator = (const guage_instr & rhs );
677 virtual void draw( void ); // Required method in base class
680 typedef guage_instr * pGuageInst;
682 // dual_instr_item This class was created to form the base class
683 // for both panel and HUD Turn Bank Indicators.
685 class dual_instr_item : public instr_item {
687 FLTFNPTR alt_data_source;
690 dual_instr_item ( int x,
694 FLTFNPTR chn1_source,
695 FLTFNPTR chn2_source,
697 UINT options = HUDS_TOP);
699 virtual ~dual_instr_item() {};
700 dual_instr_item( const dual_instr_item & image);
701 dual_instr_item & operator = (const dual_instr_item & rhs );
703 float current_ch1( void ) { return (float)alt_data_source();}
704 float current_ch2( void ) { return (float)get_value();}
705 virtual void draw ( void ) { }
708 class fgTBI_instr : public dual_instr_item {
719 FLTFNPTR chn1_source = get_roll,
720 FLTFNPTR chn2_source = get_sideslip,
721 float maxBankAngle = 45.0,
722 float maxSlipAngle = 5.0,
724 bool working = true);
726 fgTBI_instr( const fgTBI_instr & image);
727 fgTBI_instr & operator = (const fgTBI_instr & rhs );
731 UINT bank_limit( void ) { return BankLimit;}
732 UINT slew_limit( void ) { return SlewLimit;}
734 virtual void draw( void ); // Required method in base class
737 typedef fgTBI_instr * pTBI;
739 class HudLadder : public dual_instr_item {
753 fgLineList StippleLineList;
761 FLTFNPTR ptch_source = get_roll,
762 FLTFNPTR roll_source = get_pitch,
763 float span_units = 45.0,
764 float division_units = 10.0,
765 float minor_division = 0.0,
766 UINT screen_hole = 70,
768 bool working = true );
772 HudLadder( const HudLadder & image );
773 HudLadder & operator = ( const HudLadder & rhs );
774 virtual void draw( void );
776 void Text( float x, float y, char *s)
778 TextList.add( fgText( x, y, s) );
781 void Line( float x1, float y1, float x2, float y2)
783 LineList.add(fgLineSeg2D(x1,y1,x2,y2));
786 void StippleLine( float x1, float y1, float x2, float y2)
788 StippleLineList.add(fgLineSeg2D(x1,y1,x2,y2));
793 //using namespace std;
794 //deque <instr_item> * Hdeque_ptr;
796 extern void HUD_brightkey( bool incr_bright );
797 extern int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ );
798 extern int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ );
799 extern void fgUpdateHUD( void );
801 extern void drawOneLine ( UINT x1, UINT y1, UINT x2, UINT y2);
802 extern void drawOneLine ( RECT &rect);
803 extern void textString ( int x,
806 void *font = GLUT_BITMAP_8_BY_13);
807 extern void strokeString( int x,
810 void *font = GLUT_STROKE_ROMAN,
812 //extern void strokeString(float xx,
815 // void *font = GLUT_STROKE_ROMAN)
818 bool AddHUDInstrument( instr_item *pBlackBox );
819 void DrawHUD ( void );
820 bool DamageInstrument( INSTR_HANDLE unit );
821 bool RepairInstrument( INSTR_HANDLE unit );
824 void fgUpdateHUD ( Hptr hud );
825 void fgUpdateHUD2( Hptr hud ); // Future use?
826 void fgHUDSetTimeMode( Hptr hud, int time_of_day );