]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud_ladr.cxx
Norman Vine contributed code to do anti-aliases HUD lines.
[flightgear.git] / src / Cockpit / hud_ladr.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #ifdef HAVE_WINDOWS_H
6 #  include <windows.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include <simgear/constants.h>
13 #include <simgear/math/fg_random.h>
14 #include <simgear/math/polar3d.hxx>
15
16 #include <Aircraft/aircraft.hxx>
17 #include <Scenery/scenery.hxx>
18 #include <Time/fg_timer.hxx>
19 #include <GUI/gui.h>
20
21 #include "hud.hxx"
22
23 #define DO_PANEL_HACK
24
25 //====================== Top of HudLadder Class =======================
26 HudLadder ::   HudLadder(  int       x,
27                int       y,
28                UINT      width,
29                UINT      height,
30                UINT      mini,
31                FLTFNPTR  ptch_source,
32                FLTFNPTR  roll_source,
33                float    span_units,
34                float    major_div,
35                float    minor_div,
36                UINT      screen_hole,
37                UINT      lbl_pos,
38                bool      working) :
39    dual_instr_item( x, y, width, height,
40                     ptch_source,
41                     roll_source,
42                     working,
43                     HUDS_RIGHT),
44                     width_units    ( (int)(span_units)   ),
45                     div_units      ( (int)(major_div < 0? -major_div: major_div) ),
46                     minor_div      ( (int)(minor_div)    ),
47                     label_pos      ( lbl_pos      ),
48                     scr_hole       ( screen_hole  ),
49                     vmax           ( span_units/2 ),
50                     vmin           ( -vmax        )
51    {
52        if( !width_units ) {
53            width_units = 45;
54        }
55        factor = (float)get_span() / (float) width_units;
56        minimal = mini;
57    }
58    
59 HudLadder ::  ~HudLadder()
60     {
61     }
62            
63     HudLadder ::
64             HudLadder( const HudLadder & image ) :
65             dual_instr_item( (dual_instr_item &) image),
66             width_units    ( image.width_units   ),
67             div_units      ( image.div_units     ),
68             label_pos      ( image.label_pos     ),
69             scr_hole       ( image.scr_hole      ),
70             vmax           ( image.vmax ),
71             vmin           ( image.vmin ),
72             factor         ( image.factor        )
73     {
74     }
75 HudLadder & HudLadder ::  operator = ( const HudLadder & rhs )
76     {
77         if( !(this == &rhs)) {
78             (dual_instr_item &)(*this) = (dual_instr_item &)rhs;
79             width_units  = rhs.width_units;
80             div_units    = rhs.div_units;
81             label_pos    = rhs.label_pos;
82             scr_hole     = rhs.scr_hole;
83             vmax         = rhs.vmax;
84             vmin         = rhs.vmin;
85             factor       = rhs.factor;
86         }
87         return *this;
88     }
89                            
90 //
91 //  Draws a climb ladder in the center of the HUD
92 //
93
94 void HudLadder :: draw( void )
95 {
96     POINT  centroid    = get_centroid();
97         
98     float   roll_value = current_ch2();
99     
100     glPushMatrix();
101     glTranslatef( centroid.x, centroid.y, 0);
102         // Alex's panel patch
103 //      glScalef( current_options.get_fov()/55.0, 1.0, 1.0 );
104     glRotatef(roll_value * RAD_TO_DEG, 0.0, 0.0, 1.0);
105
106     // Draw the target spot.
107 #define CENTER_DIAMOND_SIZE 6.0f
108         
109     glBegin(GL_LINE_LOOP);
110         glVertex2f( CENTER_DIAMOND_SIZE, 0.0);
111         glVertex2f( 0.0, CENTER_DIAMOND_SIZE);
112         glVertex2f( -CENTER_DIAMOND_SIZE, 0.0);
113         glVertex2f( 0.0, -CENTER_DIAMOND_SIZE);
114     glEnd();
115
116     if( minimal || ! div_units ) {
117         glPopMatrix();      
118         return;
119     }
120
121         float  x_ini;
122         float  x_end;
123         float  y;
124
125         float pitch_value = current_ch1() * RAD_TO_DEG;
126         vmin              = pitch_value - (float)width_units * 0.5f;
127         vmax              = pitch_value + (float)width_units * 0.5f;
128
129         RECT   box         = get_location();
130
131         float   half_span  = box.right * 0.5f ;
132
133         char     TextLadder[8] ;
134         float    label_length ;
135         float    label_height ;
136         float    left ;
137         float    right ;
138         float    bot ;
139         float    top ;
140         float    text_offset = 4.0f ;
141         float    zero_offset = 10.0f ;
142         
143         fntFont *font      = HUDtext->getFont();
144         float    pointsize = HUDtext->getPointSize();
145         float    italic    = HUDtext->getSlant();
146         
147         TextList.setFont( HUDtext );
148         TextList.erase();
149         LineList.erase();
150         StippleLineList.erase();
151         
152         int last = FloatToInt(vmax)+1;
153         int i    = FloatToInt(vmin);
154         
155         if( !scr_hole ) {
156                 x_end =  half_span;
157                 for( ; i<last ; i++ ) {
158                 
159                         y =  (((float)(i - pitch_value) * factor) + .5f);
160                         if( !(i % div_units ))    {        //  At integral multiple of div
161                 
162                                 sprintf( TextLadder, "%d", i );
163                                 font->getBBox ( TextLadder, pointsize, italic,
164                                                                 &left, &right, &bot, &top ) ;
165                                 
166                                 label_length  = right - left;
167                                 label_length += text_offset;
168                                 label_height  = (top - bot) * 0.5f;
169                 
170                                 x_ini = -half_span;
171
172                                 if( i >= 0 ) {
173                                         // Make zero point wider on left
174                                         if( i == 0 )
175                                                 x_ini -= zero_offset;
176                                         // Zero or above draw solid lines
177                                         Line(x_ini, y, x_end, y);
178                                 } else {
179                                         // Below zero draw dashed lines.
180                                         StippleLine(x_ini, y, x_end, y);
181                                 }
182                     
183                                 // Calculate the position of the left text and write it.
184                                 Text( x_ini-label_length, y-label_height, TextLadder );
185                                 Text( x_end+text_offset,  y-label_height, TextLadder );
186                         }
187                 }
188         } else  { // scr_hole != 0
189                 // Draw ladder with space in the middle of the lines
190                 float  x_ini2;
191                 float  x_end2;
192                 float hole = (float)((scr_hole)*0.5f);
193                 
194                 x_end  = -half_span + hole;
195                 x_ini2 =  half_span - hole;
196                 
197                 for( ; i<last ; i++ )      {
198                 
199                         y = (((float)(i - pitch_value) * factor) + .5);
200                         if( !(i % div_units ))    {        //  At integral multiple of div
201                 
202                                 sprintf( TextLadder, "%d", i );
203                                 font->getBBox ( TextLadder, pointsize, italic,
204                                                                 &left, &right, &bot, &top ) ;
205                                 label_length  = right - left;
206                                 label_length += text_offset;
207                                 label_height  = (top - bot) * 0.5f;
208                 
209                                 // Start by calculating the points and drawing the
210                                 // left side lines.
211                                 x_ini  = -half_span;
212                                 x_end2 =  half_span;
213                     
214                                 if( i >= 0 ) { 
215                                         // Make zero point wider on left
216                                         if( i == 0 ) {
217                                                 x_ini -= zero_offset;
218                                                 x_end2 += zero_offset;
219                                         }
220                                         // Zero or above draw solid lines
221                                         Line(x_ini, y, x_end, y);
222                                         Line(x_ini2, y, x_end2, y);
223                                 } else {
224                                         // Below zero draw dashed lines.
225                                         StippleLine(x_ini,  y, x_end,  y);
226                                         StippleLine(x_ini2, y, x_end2, y);
227                                 }
228                                 // Calculate the location of the left side label using
229                                 Text( x_ini-label_length, y-label_height, TextLadder );
230                                 // Calculate the location and draw the right side label
231                                 Text( x_end2+text_offset, y-label_height, TextLadder );
232                         }
233                 }
234         }
235         TextList.draw();
236         
237         LineList.draw();
238
239         glEnable(GL_LINE_STIPPLE);
240 #ifdef DO_PANEL_HACK
241         glLineStipple( 1, current_options.get_panel_status() ? 0x0F0F : 0x00FF );
242 #else
243         glLineStipple( 1, 0x00FF );
244 #endif
245         
246         StippleLineList.draw( );
247         glDisable(GL_LINE_STIPPLE);
248
249 //   } // if(div_units)
250     glPopMatrix();
251 }
252