]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.hxx
Merge commit 'refs/merge-requests/1552' of git@gitorious.org:fg/flightgear into next
[flightgear.git] / src / Cockpit / hud.hxx
1 // hud.hxx -- hud defines and prototypes (initial draft)
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _OLDHUD_HXX
25 #define _OLDHUD_HXX
26
27 #ifndef __cplusplus
28 # error This library requires C++
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #ifdef __CYGWIN__
38 #include <ieeefp.h>
39 #endif
40
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <algorithm>    // for_each()
45 #include <vector>       // STL vector
46 #include <deque>        // STL double ended queue
47 #include <fstream>
48
49 namespace osg {
50     class State;
51 }
52
53 #include <simgear/math/SGMath.hxx>
54 #include <simgear/constants.h>
55
56 #include <Include/fg_typedefs.h>
57 #include <Aircraft/controls.hxx>
58 #include <FDM/flight.hxx>
59 #include <GUI/gui.h>
60 #include <Main/fg_props.hxx>
61 #include <Main/globals.hxx>
62 #include <Main/viewmgr.hxx>
63
64 class FGRunway;
65
66 using std::deque;
67 using std::vector;
68
69 #define float_to_int(v) SGMiscf::roundToInt(v)
70
71 // some of Norman's crazy optimizations. :-)
72
73 #ifndef WIN32
74 typedef struct {
75     int x, y;
76 } POINT;
77
78 typedef struct {
79     int top, bottom, left, right;
80 } RECT;
81 #endif
82
83 // View mode definitions
84
85 enum VIEW_MODES{ HUD_VIEW, PANEL_VIEW, CHASE_VIEW, TOWER_VIEW };
86
87 // Label constants
88 #define HUD_FONT_SMALL     1
89 #define HUD_FONT_LARGE     2
90
91 enum fgLabelJust{ LEFT_JUST, CENTER_JUST, RIGHT_JUST } ;
92
93 #define HUDS_AUTOTICKS           0x0001
94 #define HUDS_VERT                0x0002
95 #define HUDS_HORZ                0x0000
96 #define HUDS_TOP                 0x0004
97 #define HUDS_BOTTOM              0x0008
98 #define HUDS_LEFT     HUDS_TOP
99 #define HUDS_RIGHT    HUDS_BOTTOM
100 #define HUDS_BOTH     (HUDS_LEFT | HUDS_RIGHT)
101 #define HUDS_NOTICKS             0x0010
102 #define HUDS_ARITHTIC            0x0020
103 #define HUDS_DECITICS            0x0040
104 #define HUDS_NOTEXT              0x0080
105
106 // in cockpit.cxx
107 extern float get_throttleval ( void );
108 extern float get_aileronval  ( void );
109 extern float get_elevatorval ( void );
110 extern float get_elev_trimval( void );
111 extern float get_rudderval   ( void );
112 extern float get_speed       ( void );
113 extern float get_aoa         ( void );
114 extern float get_nlf         ( void );
115 extern float get_roll        ( void );
116 extern float get_pitch       ( void );
117 extern float get_heading     ( void );
118 extern float get_view_direction( void );
119 extern float get_altitude    ( void );
120 extern float get_agl         ( void );
121 extern float get_sideslip    ( void );
122 extern float get_frame_rate  ( void );
123 extern float get_latitude    ( void );
124 extern float get_lat_min     ( void );
125 extern float get_longitude   ( void );
126 extern float get_long_min    ( void );
127 extern float get_fov         ( void );
128 extern float get_vfc_ratio   ( void );
129 extern float get_vfc_tris_drawn   ( void );
130 extern float get_vfc_tris_culled   ( void );
131 extern float get_climb_rate  ( void );
132 extern float get_mach( void );
133 extern char *coord_format_lat(float);
134 extern char *coord_format_lon(float);
135
136 extern char *get_formated_gmt_time( void );
137
138 enum  hudinstype{ HUDno_instr,
139                   HUDscale,
140                   HUDlabel,
141                   HUDladder,
142                   HUDcirc_ladder,
143                   HUDhorizon,
144                   HUDgauge,
145                   HUDdual_inst,
146                   HUDmoving_scale,
147                   HUDtbi
148 };
149
150 typedef struct gltagRGBTRIPLE { // rgbt
151     GLfloat Blue;
152     GLfloat Green;
153     GLfloat Red;
154 } glRGBTRIPLE;
155
156 class fgLineSeg2D {
157 private:
158     GLfloat x0, y0, x1, y1;
159
160 public:
161     fgLineSeg2D( GLfloat a = 0, GLfloat b =0, GLfloat c = 0, GLfloat d =0 )
162         : x0(a), y0(b),  x1(c), y1(d) {}
163
164     fgLineSeg2D( const fgLineSeg2D & image )
165         : x0(image.x0), y0(image.y0), x1(image.x1), y1(image.y1) {}
166
167     fgLineSeg2D& operator= ( const fgLineSeg2D & image ) { // seems unused
168         x0 = image.x0; y0 = image.y0; x1 = image.x1; y1 = image.y1; return *this;
169     }
170
171     void draw() const
172     {
173         glVertex2f(x0, y0);
174         glVertex2f(x1, y1);
175     }
176 };
177
178 class DrawLineSeg2D {
179     public:
180         void operator() (const fgLineSeg2D& elem) const {
181             elem.draw();
182         }
183 };
184
185
186 #define USE_HUD_TextList
187 extern fntTexFont        *HUD_Font;
188 extern float              HUD_TextSize;
189 extern fntRenderer       *HUDtext;
190 extern float HUD_matrix[16];
191
192 class fgText {
193 private:
194     float x, y;
195     string msg;
196         bool digit;
197     // seems unused   
198         
199 public:
200         fgText(float x, float y, const string& c, bool digits=false): x(x), y(y), msg( c), digit( digits) {};   
201
202     fgText( const fgText & image )
203                 : x(image.x), y(image.y), msg(image.msg), digit(image.digit) { }
204
205         fgText& operator = ( const fgText & image ) {
206                 x = image.x; y = image.y; msg= image.msg; digit = image.digit;
207                 return *this;
208         }
209         
210     static int getStringWidth ( const char *str )
211     {
212         if ( HUDtext && str ) {
213             float r, l ;
214             HUD_Font->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
215             return float_to_int( r - l );
216         }
217         return 0 ;
218     }
219
220     int StringWidth ()
221     {
222         if ( HUDtext && msg != "") {
223             float r, l ;
224             HUD_Font->getBBox ( msg.c_str(), HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
225             return float_to_int( r - l );
226         }
227         return 0 ;
228     }
229
230     // this code is changed to display Numbers with big/small digits
231     // according to MIL Standards for example Altitude above 10000 ft
232     // is shown as 10ooo.
233
234     void Draw(fntRenderer *fnt) {
235         if (digit) {
236             int c=0;
237             
238             int p=4;
239
240             if (msg[0]=='-') {
241                 //if negative value then increase the c and p values
242                 //for '-' sign.  c++;
243                 p++;
244             }
245                         
246                         for (string::size_type i = 0; i < msg.size(); i++) {
247                 if ((msg[i]>='0') && (msg[i]<='9'))
248                     c++;
249             }
250             float orig_size = fnt->getPointSize();
251             if (c>p) {
252                 fnt->setPointSize(HUD_TextSize * 0.8);
253                 int p2=(c-3)*8;  //advance to the last 3 digits
254
255                 fnt->start2f(x+p2,y);
256                 fnt->puts(msg.c_str() + c - 3); // display last 3 digits
257
258                 fnt->setPointSize(HUD_TextSize * 1.2);
259               
260                 fnt->start2f(x,y);
261                                 fnt->puts(msg.substr(0,c-3).c_str());
262             } else {
263                 fnt->setPointSize(HUD_TextSize * 1.2);
264                 fnt->start2f( x, y );
265                 fnt->puts(msg.c_str());
266             }
267             fnt->setPointSize(orig_size);
268         } else {
269             //if digits not true
270             fnt->start2f( x, y );
271             fnt->puts( msg.c_str()) ;
272         }
273     }
274
275     void Draw()
276     {
277                 guiFnt.drawString( msg.c_str(), float_to_int(x), float_to_int(y) );
278     }
279 };
280
281 class fgLineList {
282     vector < fgLineSeg2D > List;
283 public:
284     fgLineList( void ) {}
285     void add( const fgLineSeg2D& seg ) { List.push_back(seg); }
286         void erase( void ) { List.clear();}
287     void draw( void ) {
288         glBegin(GL_LINES);
289         for_each( List.begin(), List.end(), DrawLineSeg2D());
290         glEnd();
291     }
292 };
293
294 class fgTextList {
295     fntRenderer *Font;
296     vector< fgText > List;
297 public:
298     fgTextList ( void ) { Font = 0; }
299
300     void setFont( fntRenderer *Renderer ) { Font = Renderer; }
301     void add( const fgText& String ) { List.push_back(String); }
302     void erase( void ) { List.clear(); }
303     void draw( void );
304 };
305
306
307 inline void Text( fgTextList &List, float x, float y, char *s)
308 {
309     List.add( fgText( x, y, s) );
310 }
311
312 inline void Text( fgTextList &List, const fgText &me)
313 {
314     List.add(me);
315 }
316
317 inline void Line( fgLineList &List, float x1, float y1, float x2, float y2)
318 {
319     List.add(fgLineSeg2D(x1,y1,x2,y2));
320 }
321
322
323 // Declare our externals
324 extern fgTextList         HUD_TextList;
325 extern fgLineList         HUD_LineList;
326 extern fgLineList         HUD_StippleLineList;
327
328
329 class instr_item : public SGReferenced {  // An Abstract Base Class (ABC)
330 private:
331     static UINT        instances;     // More than 64K instruments? Nah!
332     static int         brightness;
333     static glRGBTRIPLE color;
334
335     UINT               handle;
336     RECT               scrn_pos;      // Framing - affects scale dimensions
337                                       // and orientation. Vert vs Horz, etc.
338     FLTFNPTR           load_value_fn;
339     float              disp_factor;   // Multiply by to get numbers shown on scale.
340     UINT               opts;
341     bool               is_enabled;
342     bool               broken;
343     UINT               scr_span;      // Working values for draw;
344     POINT              mid_span;      //
345     int                digits;
346
347 public:
348     instr_item( int            x,
349                 int            y,
350                 UINT           height,
351                 UINT           width,
352                 FLTFNPTR       data_source,
353                 float          data_scaling,
354                 UINT           options,
355                 bool           working = true,
356                 int            digit = 0);
357
358     virtual ~instr_item ();
359
360     void    set_data_source ( FLTFNPTR fn ) { load_value_fn = fn; }
361     int     get_brightness  ( void ) { return brightness;}
362     RECT    get_location    ( void ) { return scrn_pos;  }
363     bool    is_broken       ( void ) { return broken;    }
364     bool    enabled         ( void ) { return is_enabled;}
365     bool    data_available  ( void ) { return !!load_value_fn; }
366     float   get_value       ( void ) { return load_value_fn(); }
367     float   data_scaling    ( void ) { return disp_factor; }
368     UINT    get_span        ( void ) { return scr_span;  }
369     POINT   get_centroid    ( void ) { return mid_span;  }
370     UINT    get_options     ( void ) { return opts;      }
371     int     get_digits      ( void ) { return digits;         }
372     inline int get_x() const { return scrn_pos.left; }
373     inline int get_y() const { return scrn_pos.top; }
374     inline int get_width() const { return scrn_pos.right; }
375     inline int get_height() const { return scrn_pos.bottom; }
376
377     UINT    huds_vert     (UINT options) { return (options & HUDS_VERT); }
378     UINT    huds_left     (UINT options) { return (options & HUDS_LEFT); }
379     UINT    huds_right    (UINT options) { return (options & HUDS_RIGHT); }
380     UINT    huds_both     (UINT options) {
381         return ((options & HUDS_BOTH) == HUDS_BOTH);
382     }
383     UINT    huds_noticks  (UINT options) { return (options & HUDS_NOTICKS); }
384     UINT    huds_notext   (UINT options) { return (options & HUDS_NOTEXT); }
385     UINT    huds_top      (UINT options) { return (options & HUDS_TOP); }
386     UINT    huds_bottom   (UINT options) { return (options & HUDS_BOTTOM); }
387
388     virtual void display_enable( bool working ) { is_enabled = working;}
389
390     virtual void break_display ( bool bad );
391     virtual void SetBrightness( int illumination_level ); // fgHUDSetBright...
392     void         SetPosition  ( int x, int y, UINT width, UINT height );
393     UINT         get_Handle( void );
394     virtual void draw( void ) = 0;   // Required method in derived classes
395
396     void drawOneLine( float x1, float y1, float x2, float y2)
397     {
398         HUD_LineList.add(fgLineSeg2D(x1,y1,x2,y2));
399     }
400     void drawOneStippleLine( float x1, float y1, float x2, float y2)
401     {
402         HUD_StippleLineList.add(fgLineSeg2D(x1,y1,x2,y2));
403     }
404     void TextString( char *msg, float x, float y, bool digit )
405     {
406         HUD_TextList.add(fgText(x, y, msg,digit));
407     }
408     int getStringWidth ( char *str )
409     {
410         if ( HUDtext && str ) {
411             float r, l ;
412             HUD_Font->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
413             return float_to_int( r - l );
414         }
415         return 0 ;
416     }
417
418
419
420 };
421
422 typedef instr_item *HIptr;
423
424 class HUDdraw {
425     public:
426         void operator() (HIptr elem) const {
427             if ( elem->enabled())
428                 elem->draw();
429         }
430 };
431
432
433 extern int HUD_style;
434
435 // instr_item           This class has no other purpose than to maintain
436 //                      a linked list of instrument and derived class
437 // object pointers.
438
439
440 class instr_label : public instr_item {
441 private:
442     const char  *pformat;
443     
444     fgLabelJust justify;
445     int         fontSize;
446     int         blink;
447     string      format_buffer;
448     bool        lat;
449     bool        lon;
450     bool        lbox;
451     SGPropertyNode_ptr lon_node;
452     SGPropertyNode_ptr lat_node;
453
454 public:
455     instr_label(const SGPropertyNode *);
456     virtual void draw(void);
457 };
458
459
460 //
461 // fgRunway_instr   This class is responsible for rendering the active runway
462 //                  in the hud (if visible).
463 class runway_instr : public instr_item {
464 private:
465         void boundPoint(const sgdVec3& v, sgdVec3& m);
466         bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
467         bool drawLine(const sgdVec3& a1, const sgdVec3& a2,
468                 const sgdVec3& p1, const sgdVec3& p2);
469         void drawArrow();
470         FGRunway* get_active_runway();
471         void get_rwy_points(sgdVec3 *points);
472         void setLineWidth(void);
473
474         sgdVec3 points3d[6], points2d[6];
475         double mm[16],pm[16], arrowScale, arrowRad, lnScale;
476         double scaleDist, default_pitch, default_heading;
477         GLint view[4];
478         FGRunway* runway;
479         FGViewer* cockpit_view;
480         unsigned short stippleOut, stippleCen;
481         bool drawIA, drawIAAlways;
482         RECT location;
483         POINT center;
484
485 public:
486     runway_instr(const SGPropertyNode *);
487
488     virtual void draw( void );
489     void setArrowRotationRadius(double radius);
490     // Scales the runway indication arrow
491     void setArrowScale(double scale);
492     // Draws arrow when runway is not visible in HUD if draw=true
493     void setDrawArrow(bool draw);
494     // Always draws arrow if draw=true;
495     void setDrawArrowAlways(bool draw);
496     // Sets the maximum line scale
497     void setLineScale(double scale);
498     // Sets the distance where to start scaling the lines
499     void setScaleDist(double dist_nm);
500     // Sets the stipple pattern of the outline of the runway
501     void setStippleOutline(unsigned short stipple);
502     // Sets the stipple patter of the center line of the runway
503     void setStippleCenterline(unsigned short stipple);
504 };
505
506
507 //
508 // instr_scale           This class is an abstract base class for both moving
509 //                       scale and moving needle (fixed scale) indicators. It
510 // does not draw itself, but is not instanciable.
511 //
512
513 class instr_scale : public instr_item {
514 private:
515     float range_shown;   // Width Units.
516     float Maximum_value; //                ceiling.
517     float Minimum_value; // Representation floor.
518     float scale_factor;  // factor => screen units/range values.
519     UINT   Maj_div;       // major division marker units
520     UINT   Min_div;       // minor division marker units
521     UINT   Modulo;        // Roll over point
522     int    signif_digits; // digits to show to the right.
523
524 public:
525     instr_scale( int          x,
526                  int          y,
527                  UINT         width,
528                  UINT         height,
529                  FLTFNPTR     load_fn,
530                  UINT         options,
531                  float        show_range,
532                  float        max_value,
533                  float        min_value,
534                  float        disp_scaling,
535                  UINT         major_divs,
536                  UINT         minor_divs,
537                  UINT         rollover,
538                  int          dp_showing,
539                  bool         working = true);
540
541     virtual void draw    ( void ) {}; // No-op here. Defined in derived classes.
542     UINT   div_min       ( void ) { return Min_div;}
543     UINT   div_max       ( void ) { return Maj_div;}
544     float  min_val       ( void ) { return Minimum_value;}
545     float  max_val       ( void ) { return Maximum_value;}
546     UINT   modulo        ( void ) { return Modulo; }
547     float  factor        ( void ) { return scale_factor;}
548     float  range_to_show ( void ) { return range_shown;}
549 };
550
551 // hud_card                This class displays the indicated quantity on
552 //                         a scale that moves past the pointer. It may be
553 // horizontal or vertical, read above(left) or below(right) of the base
554 // line.
555
556 class hud_card : public instr_scale {
557 private:
558     float  val_span;
559     string type;
560     float  half_width_units;
561     bool   draw_tick_bottom;
562     bool   draw_tick_top;
563     bool   draw_tick_right;
564     bool   draw_tick_left;
565     bool   draw_cap_bottom;
566     bool   draw_cap_top;
567     bool   draw_cap_right;
568     bool   draw_cap_left;
569     float  marker_offset;
570     bool   pointer;
571     string pointer_type;
572     string tick_type;
573     string tick_length;
574     float  radius;
575     float  maxValue;
576     float  minValue;
577     int    divisions;
578     int    zoom;
579     UINT   Maj_div;
580     UINT   Min_div;
581
582 public:
583     hud_card(const SGPropertyNode *);
584     //    virtual void display_enable( bool setting );                  // FIXME
585     virtual void draw(void);
586     void circles(float, float, float);
587     void fixed(float, float, float, float, float, float);
588     void zoomed_scale(int, int);
589 };
590
591
592 class gauge_instr : public instr_scale {
593 public:
594     gauge_instr(const SGPropertyNode *);
595     virtual void draw( void );       // Required method in base class
596 };
597
598
599 //
600 // dual_instr_item         This class was created to form the base class
601 //                         for both panel and HUD Turn Bank Indicators.
602
603 class dual_instr_item : public instr_item {
604 private:
605     FLTFNPTR alt_data_source;
606
607 public:
608     dual_instr_item ( int       x,
609                       int       y,
610                       UINT      width,
611                       UINT      height,
612                       FLTFNPTR  chn1_source,
613                       FLTFNPTR  chn2_source,
614                       bool      working,
615                       UINT      options );
616
617     float current_ch1( void ) { return (float)alt_data_source(); }
618     float current_ch2( void ) { return (float)get_value(); }
619     virtual void draw( void ) {}
620 };
621
622
623 class fgTBI_instr : public dual_instr_item {
624 private:
625     UINT BankLimit;
626     UINT SlewLimit;
627     UINT scr_hole;
628     bool tsi;
629     float rad;
630
631 public:
632     fgTBI_instr(const SGPropertyNode *);
633
634     UINT bank_limit(void) { return BankLimit; }
635     UINT slew_limit(void) { return SlewLimit; }
636
637     virtual void draw(void);
638 };
639
640
641 class HudLadder : public dual_instr_item {
642 private:
643     UINT   width_units;
644     int    div_units;
645     UINT   minor_div;
646     UINT   label_pos;
647     UINT   scr_hole;
648     float  vmax;
649     float  vmin;
650     float  factor;
651     string hudladder_type;
652     bool   frl;
653     bool   target_spot;
654     bool   velocity_vector;
655     bool   drift_marker;
656     bool   alpha_bracket;
657     bool   energy_marker;
658     bool   climb_dive_marker;
659     bool   glide_slope_marker;
660     float  glide_slope;
661     bool   energy_worm;
662     bool   waypoint_marker;
663     int    zenith;
664     int    nadir;
665     int    hat;
666
667     // The Ladder has it's own temporary display lists
668     fgTextList         TextList;
669     fgLineList         LineList;
670     fgLineList         StippleLineList;
671
672 public:
673     HudLadder(const SGPropertyNode *);
674
675     virtual void draw(void);
676     void drawZenith(float, float, float);
677     void drawNadir(float, float, float);
678
679     void Text(float x, float y, char *s)
680     {
681         TextList.add(fgText(x, y, s));
682     }
683
684     void Line(float x1, float y1, float x2, float y2)
685     {
686         LineList.add(fgLineSeg2D(x1, y1, x2, y2));
687     }
688
689     void StippleLine(float x1, float y1, float x2, float y2)
690     {
691         StippleLineList.add(fgLineSeg2D(x1, y1, x2, y2));
692     }
693 };
694
695
696 extern int  fgHUDInit();
697 extern int  fgHUDInit2();
698 extern void fgUpdateHUD( osg::State* );
699 extern void fgUpdateHUD( osg::State*, GLfloat x_start, GLfloat y_start,
700                          GLfloat x_end, GLfloat y_end );
701
702
703
704
705 class HUD_Properties : public SGPropertyChangeListener {
706 public:
707     HUD_Properties();
708     void valueChanged(SGPropertyNode *n);
709     void setColor() const;
710     bool isVisible() const { return _visible; }
711     bool isAntialiased() const { return _antialiased; }
712     bool isTransparent() const { return _transparent; }
713     float alphaClamp() const { return _cl; }
714
715 private:
716     float clamp(float f) { return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f; }
717     SGPropertyNode_ptr _current;
718     SGPropertyNode_ptr _visibility;
719     SGPropertyNode_ptr _antialiasing;
720     SGPropertyNode_ptr _transparency;
721     SGPropertyNode_ptr _red, _green, _blue, _alpha;
722     SGPropertyNode_ptr _alpha_clamp;
723     SGPropertyNode_ptr _brightness;
724     bool _visible;
725     bool _antialiased;
726     bool _transparent;
727     float _r, _g, _b, _a, _cl;
728 };
729
730 #endif // _OLDHUD_H