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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # error This library requires C++
31 #include <simgear/compiler.h>
44 //#ifdef HAVE_VALUES_H
45 //# include <values.h> // for MAXINT
48 #include <algorithm> // for_each()
49 #include <vector> // STL vector
50 #include <deque> // STL double ended queue
53 #include <simgear/constants.h>
54 //#include <simgear/props/props.hxx>
56 #include <Include/fg_typedefs.h>
57 #include <Aircraft/aircraft.hxx>
58 #include <Aircraft/controls.hxx>
59 #include <FDM/flight.hxx>
61 #include <Main/globals.hxx>
62 #include <Main/viewmgr.hxx>
63 #include <Airports/runways.hxx>
65 #include "hud_opts.hxx"
70 SG_USING_NAMESPACE(std);
73 // some of Norman's crazy optimizations. :-)
81 int top, bottom, left, right;
85 // View mode definitions
87 enum VIEW_MODES{ HUD_VIEW, PANEL_VIEW, CHASE_VIEW, TOWER_VIEW };
90 #define HUD_FONT_SMALL 1
91 #define HUD_FONT_LARGE 2
93 enum fgLabelJust{ LEFT_JUST, CENTER_JUST, RIGHT_JUST } ;
95 #define HUDS_AUTOTICKS 0x0001
96 #define HUDS_VERT 0x0002
97 #define HUDS_HORZ 0x0000
98 #define HUDS_TOP 0x0004
99 #define HUDS_BOTTOM 0x0008
100 #define HUDS_LEFT HUDS_TOP
101 #define HUDS_RIGHT HUDS_BOTTOM
102 #define HUDS_BOTH (HUDS_LEFT | HUDS_RIGHT)
103 #define HUDS_NOTICKS 0x0010
104 #define HUDS_ARITHTIC 0x0020
105 #define HUDS_DECITICS 0x0040
106 #define HUDS_NOTEXT 0x0080
109 // #define HUD_VERTICAL 1
110 // #define HUD_HORIZONTAL 2
111 // #define HUD_FREEFLOAT 3
113 // Ladder orientation modes
114 // #define HUD_LEFT 1
115 // #define HUD_RIGHT 2
117 // #define HUD_BOTTOM 2
118 // #define HUD_V_LEFT 1
119 // #define HUD_V_RIGHT 2
120 // #define HUD_H_TOP 1
121 // #define HUD_H_BOTTOM 2
126 // #define HUD_NOLIM 2
127 // #define HUD_CIRC 3
129 // #define HUD_INSTR_LADDER 1
130 // #define HUD_INSTR_CLADDER 2
131 // #define HUD_INSTR_HORIZON 3
132 // #define HUD_INSTR_LABEL 4
135 extern float get_throttleval ( void );
136 extern float get_aileronval ( void );
137 extern float get_elevatorval ( void );
138 extern float get_elev_trimval( void );
139 extern float get_rudderval ( void );
140 extern float get_speed ( void );
141 extern float get_aoa ( void );
142 extern float get_nlf ( void );
143 extern float get_roll ( void );
144 extern float get_pitch ( void );
145 extern float get_heading ( void );
146 extern float get_view_direction( void );
147 extern float get_altitude ( void );
148 extern float get_agl ( void );
149 extern float get_sideslip ( void );
150 extern float get_frame_rate ( void );
151 extern float get_latitude ( void );
152 extern float get_lat_min ( void );
153 extern float get_longitude ( void );
154 extern float get_long_min ( void );
155 extern float get_fov ( void );
156 extern float get_vfc_ratio ( void );
157 extern float get_vfc_tris_drawn ( void );
158 extern float get_vfc_tris_culled ( void );
159 extern float get_climb_rate ( void );
160 extern float get_mach( void );
161 extern char *coord_format_lat(float);
162 extern char *coord_format_lon(float);
163 //extern char *coord_format_latlon(float latitude, float longitude); // cockpit.cxx
165 // $$$ begin - added, VS Renganathan, 13 Oct 2K
166 // #define FIGHTER_HUD
167 extern float get_anzg (void);
168 extern float get_Vx (void);
169 extern float get_Vy (void);
170 extern float get_Vz (void);
171 extern float get_Ax (void);
172 extern float get_Ay (void);
173 extern float get_Az (void);
174 extern int get_iaux1 (void);
175 extern int get_iaux2 (void);
176 extern int get_iaux3 (void);
177 extern int get_iaux4 (void);
178 extern int get_iaux5 (void);
179 extern int get_iaux6 (void);
180 extern int get_iaux7 (void);
181 extern int get_iaux8 (void);
182 extern int get_iaux9 (void);
183 extern int get_iaux10 (void);
184 extern int get_iaux11 (void);
185 extern int get_iaux12 (void);
186 extern float get_aux1(void);
187 extern float get_aux2(void);
188 extern float get_aux3(void);
189 extern float get_aux4(void);
190 extern float get_aux5 (void);
191 extern float get_aux6 (void);
192 extern float get_aux7 (void);
193 extern float get_aux8(void);
194 extern float get_aux9(void);
195 extern float get_aux10(void);
196 extern float get_aux11(void);
197 extern float get_aux12(void);
198 extern float get_aux13(void);
199 extern float get_aux14(void);
200 extern float get_aux15(void);
201 extern float get_aux16(void);
202 extern float get_aux17(void);
203 extern float get_aux18(void);
204 // $$$ end - added, VS Renganathan, 13 Oct 2K
206 extern char *get_formated_gmt_time( void );
207 extern void fgHUDReshape(void);
209 enum hudinstype{ HUDno_instr,
221 typedef struct gltagRGBTRIPLE { // rgbt
229 GLfloat x0, y0, x1, y1;
232 fgLineSeg2D( GLfloat a = 0, GLfloat b =0, GLfloat c = 0, GLfloat d =0 )
233 : x0(a), y0(b), x1(c), y1(d) {}
235 fgLineSeg2D( const fgLineSeg2D & image )
236 : x0(image.x0), y0(image.y0), x1(image.x1), y1(image.y1) {}
238 fgLineSeg2D& operator= ( const fgLineSeg2D & image ) {
239 x0 = image.x0; y0 = image.y0; x1 = image.x1; y1 = image.y1; return *this;
251 class DrawLineSeg2D {
253 void operator() (const fgLineSeg2D& elem) const {
259 #define USE_HUD_TextList
260 extern fntTexFont *HUD_Font;
261 extern float HUD_TextSize;
262 extern fntRenderer *HUDtext;
263 extern float HUD_matrix[16];
271 fgText(float x = 0, float y = 0, char *c = NULL,int digits=0): x(x), y(y)
277 fgText( const fgText & image )
278 : x(image.x), y(image.y),digit(image.digit) { strcpy(msg,image.msg); }
280 fgText& operator = ( const fgText & image ) {
281 strcpy(msg,image.msg); x = image.x; y = image.y;digit=image.digit;
285 ~fgText() { msg[0]='\0'; }
287 int getStringWidth ( char *str )
289 if ( HUDtext && str ) {
291 HUD_Font->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
292 return FloatToInt( r - l );
297 int StringWidth (void )
299 if ( HUDtext && strlen( msg )) {
301 HUD_Font->getBBox ( msg, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
302 return FloatToInt( r - l );
307 // this code is changed to display Numbers with big/small digits
308 // according to MIL Standards for example Altitude above 10000 ft
309 // is shown as 10ooo.
311 void Draw(fntRenderer *fnt,int digits) {
318 //if negative value then increase the c and p values
323 while (tmp[i]!='\0') {
324 if ((tmp[i]>='0') && (tmp[i]<='9'))
329 fnt->setPointSize(HUD_TextSize * 0.8);
334 fnt->start2f(x+p2,y);
337 fnt->setPointSize(HUD_TextSize * 1.2);
339 strncpy(tmp2,msg,p1);
345 fnt->setPointSize(HUD_TextSize * 1.2);
346 fnt->start2f( x, y );
350 //if digits not equal to 1
351 fnt->setPointSize(HUD_TextSize * 0.8);
352 fnt->start2f( x, y );
359 guiFnt.drawString( msg, FloatToInt(x), FloatToInt(y) );
364 vector < fgLineSeg2D > List;
366 fgLineList( void ) {}
367 ~fgLineList( void ) {}
368 void add( const fgLineSeg2D& seg ) { List.push_back(seg); }
369 void erase( void ) { List.erase( List.begin(), List.end() ); }
372 for_each( List.begin(), List.end(), DrawLineSeg2D());
379 vector< fgText > List;
381 fgTextList ( void ) { Font = 0; }
382 ~fgTextList( void ) {}
384 void setFont( fntRenderer *Renderer ) { Font = Renderer; }
385 void add( const fgText& String ) { List.push_back(String); }
386 void erase( void ) { List.erase( List.begin(), List.end() ); }
391 inline void Text( fgTextList &List, float x, float y, char *s)
393 List.add( fgText( x, y, s) );
396 inline void Text( fgTextList &List, const fgText &me)
401 inline void Line( fgLineList &List, float x1, float y1, float x2, float y2)
403 List.add(fgLineSeg2D(x1,y1,x2,y2));
407 // Declare our externals
408 extern fgTextList HUD_TextList;
409 extern fgLineList HUD_LineList;
410 extern fgLineList HUD_StippleLineList;
413 class instr_item { // An Abstract Base Class (ABC)
415 static UINT instances; // More than 64K instruments? Nah!
416 static int brightness;
417 static glRGBTRIPLE color;
420 RECT scrn_pos; // Framing - affects scale dimensions
421 // and orientation. Vert vs Horz, etc.
422 FLTFNPTR load_value_fn;
423 float disp_factor; // Multiply by to get numbers shown on scale.
427 UINT scr_span; // Working values for draw;
436 FLTFNPTR data_source,
442 instr_item( const instr_item & image );
444 virtual ~instr_item ();
446 void set_data_source ( FLTFNPTR fn ) { load_value_fn = fn; }
447 int get_brightness ( void ) { return brightness;}
448 RECT get_location ( void ) { return scrn_pos; }
449 bool is_broken ( void ) { return broken; }
450 bool enabled ( void ) { return is_enabled;}
451 bool data_available ( void ) { return !!load_value_fn; }
452 float get_value ( void ) { return load_value_fn(); }
453 float data_scaling ( void ) { return disp_factor; }
454 UINT get_span ( void ) { return scr_span; }
455 POINT get_centroid ( void ) { return mid_span; }
456 UINT get_options ( void ) { return opts; }
457 int get_digits ( void ) { return digits; }
458 inline int get_x() const { return scrn_pos.left; }
459 inline int get_y() const { return scrn_pos.top; }
460 inline int get_width() const { return scrn_pos.right; }
461 inline int get_height() const { return scrn_pos.bottom; }
463 UINT huds_vert (UINT options) { return (options & HUDS_VERT); }
464 UINT huds_left (UINT options) { return (options & HUDS_LEFT); }
465 UINT huds_right (UINT options) { return (options & HUDS_RIGHT); }
466 UINT huds_both (UINT options) {
467 return ((options & HUDS_BOTH) == HUDS_BOTH);
469 UINT huds_noticks (UINT options) { return (options & HUDS_NOTICKS); }
470 UINT huds_notext (UINT options) { return (options & HUDS_NOTEXT); }
471 UINT huds_top (UINT options) { return (options & HUDS_TOP); }
472 UINT huds_bottom (UINT options) { return (options & HUDS_BOTTOM); }
474 virtual void display_enable( bool working ) { is_enabled = working;}
476 virtual void update( void );
477 virtual void break_display ( bool bad );
478 virtual void SetBrightness( int illumination_level ); // fgHUDSetBright...
479 void SetPosition ( int x, int y, UINT width, UINT height );
480 UINT get_Handle( void );
481 virtual void draw( void ) = 0; // Required method in derived classes
483 void drawOneLine( float x1, float y1, float x2, float y2)
485 HUD_LineList.add(fgLineSeg2D(x1,y1,x2,y2));
487 void drawOneStippleLine( float x1, float y1, float x2, float y2)
489 HUD_StippleLineList.add(fgLineSeg2D(x1,y1,x2,y2));
491 void TextString( char *msg, float x, float y,int digit )
493 HUD_TextList.add(fgText(x, y, msg,digit));
495 int getStringWidth ( char *str )
497 if ( HUDtext && str ) {
499 HUD_Font->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
500 return FloatToInt( r - l );
505 //code to draw ticks as small circles
506 void drawOneCircle(float x1, float y1, float r)
508 glBegin(GL_LINE_LOOP); // Use polygon to approximate a circle
509 for (int count=0; count<25; count++) {
510 float cosine = r * cos(count * 2 * SG_PI/10.0);
511 float sine = r * sin(count * 2 * SG_PI/10.0);
512 glVertex2f(cosine+x1, sine+y1);
519 typedef instr_item *HIptr;
523 void operator() (HIptr elem) const {
524 if ( elem->enabled())
530 extern deque< instr_item *> HUD_deque;
531 extern int HUD_style;
532 //extern hud_deque_type HUD_deque;
534 // instr_item This class has no other purpose than to maintain
535 // a linked list of instrument and derived class
539 class instr_label : public instr_item {
543 const char *post_str;
547 char format_buffer[80];
553 instr_label(const SGPropertyNode *);
556 instr_label(const instr_label& image);
557 virtual void draw(void);
561 class lat_label : public instr_item {
565 const char *post_str;
569 char format_buffer[80];
576 FLTFNPTR data_source,
577 const char *label_format,
578 const char *pre_label_string,
579 const char *post_label_string,
582 fgLabelJust justification,
590 lat_label( const lat_label & image);
591 virtual void draw( void ); // Required method in base class
595 class lon_label : public instr_item {
599 const char *post_str;
603 char format_buffer[80];
610 FLTFNPTR data_source,
611 const char *label_format,
612 const char *pre_label_string,
613 const char *post_label_string,
616 fgLabelJust justification,
625 lon_label( const lon_label & image);
626 virtual void draw( void ); // Required method in base class
631 // fgRunway_instr This class is responsible for rendering the active runway
632 // in the hud (if visible).
633 class runway_instr : public instr_item {
635 void boundPoint(const sgdVec3& v, sgdVec3& m);
636 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
637 bool drawLine(const sgdVec3& a1, const sgdVec3& a2,
638 const sgdVec3& p1, const sgdVec3& p2);
640 bool get_active_runway(FGRunway& rwy);
641 void get_rwy_points(sgdVec3 *points);
642 void setLineWidth(void);
644 sgdVec3 points3d[6], points2d[6];
645 double mm[16],pm[16], arrowScale, arrowRad, lnScale;
646 double scaleDist, default_pitch, default_heading;
649 FGViewer* cockpit_view;
650 unsigned short stippleOut, stippleCen;
651 bool drawIA, drawIAAlways;
656 runway_instr(const SGPropertyNode *);
658 virtual void draw( void );
659 void setArrowRotationRadius(double radius);
660 // Scales the runway indication arrow
661 void setArrowScale(double scale);
662 // Draws arrow when runway is not visible in HUD if draw=true
663 void setDrawArrow(bool draw);
664 //Always draws arrow if draw=true;
665 void setDrawArrowAlways(bool draw);
666 //Sets the maximum line scale
667 void setLineScale(double scale);
668 //Sets the distance where to start scaling the lines
669 void setScaleDist(double dist_nm);
670 //Sets the stipple pattern of the outline of the runway
671 void setStippleOutline(unsigned short stipple);
672 //Sets the stipple patter of the center line of the runway
673 void setStippleCenterline(unsigned short stipple);
678 // instr_scale This class is an abstract base class for both moving
679 // scale and moving needle (fixed scale) indicators. It
680 // does not draw itself, but is not instanciable.
683 class instr_scale : public instr_item {
685 float range_shown; // Width Units.
686 float Maximum_value; // ceiling.
687 float Minimum_value; // Representation floor.
688 float scale_factor; // factor => screen units/range values.
689 UINT Maj_div; // major division marker units
690 UINT Min_div; // minor division marker units
691 UINT Modulo; // Roll over point
692 int signif_digits; // digits to show to the right.
709 bool working = true);
711 virtual ~instr_scale();
712 instr_scale( const instr_scale & image);
714 virtual void draw ( void ) {}; // No-op here. Defined in derived classes.
715 UINT div_min ( void ) { return Min_div;}
716 UINT div_max ( void ) { return Maj_div;}
717 float min_val ( void ) { return Minimum_value;}
718 float max_val ( void ) { return Maximum_value;}
719 UINT modulo ( void ) { return Modulo; }
720 float factor ( void ) { return scale_factor;}
721 float range_to_show ( void ) { return range_shown;}
724 // hud_card This class displays the indicated quantity on
725 // a scale that moves past the pointer. It may be
726 // horizontal or vertical, read above(left) or below(right) of the base
729 class hud_card : public instr_scale {
733 float half_width_units;
734 bool draw_tick_bottom;
736 bool draw_tick_right;
738 bool draw_cap_bottom;
756 hud_card(const SGPropertyNode *);
757 // virtual void display_enable( bool setting ); // FIXME
758 virtual void draw(void);
759 void circles(float, float, float);
760 void fixed(float, float, float, float, float, float);
761 void zoomed_scale(int, int);
765 class gauge_instr : public instr_scale {
767 gauge_instr(const SGPropertyNode *);
768 virtual void draw( void ); // Required method in base class
773 // dual_instr_item This class was created to form the base class
774 // for both panel and HUD Turn Bank Indicators.
776 class dual_instr_item : public instr_item {
778 FLTFNPTR alt_data_source;
781 dual_instr_item ( int x,
785 FLTFNPTR chn1_source,
786 FLTFNPTR chn2_source,
790 virtual ~dual_instr_item() {};
791 dual_instr_item( const dual_instr_item & image);
793 float current_ch1( void ) { return (float)alt_data_source();}
794 float current_ch2( void ) { return (float)get_value();}
795 virtual void draw( void ) { }
799 class fgTBI_instr : public dual_instr_item {
808 fgTBI_instr(const SGPropertyNode *);
810 UINT bank_limit(void) { return BankLimit; }
811 UINT slew_limit(void) { return SlewLimit; }
813 virtual void draw(void);
817 class HudLadder : public dual_instr_item {
827 string hudladder_type;
830 bool velocity_vector;
834 bool climb_dive_marker;
835 bool glide_slope_marker;
838 bool waypoint_marker;
843 // The Ladder has it's own temporary display lists
846 fgLineList StippleLineList;
849 HudLadder(const SGPropertyNode *);
852 virtual void draw(void);
853 void drawZenith(float, float, float);
854 void drawNadir(float, float, float);
856 void Text(float x, float y, char *s)
858 TextList.add(fgText(x, y, s));
861 void Line(float x1, float y1, float x2, float y2)
863 LineList.add(fgLineSeg2D(x1, y1, x2, y2));
866 void StippleLine(float x1, float y1, float x2, float y2)
868 StippleLineList.add(fgLineSeg2D(x1, y1, x2, y2));
873 //using namespace std;
874 //deque <instr_item> * Hdeque_ptr;
876 extern int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ );
877 extern int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ );
878 extern void fgUpdateHUD( void );
879 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
880 GLfloat x_end, GLfloat y_end );
883 bool AddHUDInstrument( instr_item *pBlackBox );
884 void DrawHUD ( void );
885 bool DamageInstrument( INSTR_HANDLE unit );
886 bool RepairInstrument( INSTR_HANDLE unit );
889 void fgUpdateHUD ( Hptr hud );
890 void fgUpdateHUD2( Hptr hud ); // Future use?
891 void fgHUDSetTimeMode( Hptr hud, int time_of_day );
897 class HUD_Properties : public SGPropertyChangeListener {
900 void valueChanged(SGPropertyNode *n);
901 void setColor() const;
902 bool isVisible() const { return _visible; }
903 bool isAntialiased() const { return _antialiased; }
904 bool isTransparent() const { return _transparent; }
905 float alphaClamp() const { return _cl; }
908 float clamp(float f) { return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f; }
909 SGPropertyNode_ptr _current;
910 SGPropertyNode_ptr _visibility;
911 SGPropertyNode_ptr _antialiasing;
912 SGPropertyNode_ptr _transparency;
913 SGPropertyNode_ptr _red, _green, _blue, _alpha;
914 SGPropertyNode_ptr _alpha_clamp;
915 SGPropertyNode_ptr _brightness;
919 float _r, _g, _b, _a, _cl;