]> git.mxchange.org Git - flightgear.git/blob - Cockpit/hud.c
fcae83f7832a4348590d03e278fe1341b802ff66
[flightgear.git] / Cockpit / hud.c
1 /**************************************************************************
2  * hud.c -- hud defines and prototypes
3  *
4  * Written by Michele America, started September 1997.
5  *
6  * Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25  
26
27 #ifdef WIN32
28 #  include <windows.h>
29 #endif
30
31 #include <GL/glut.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #  include <values.h>  // for MAXINT
37 #endif //
38
39 #include "hud.h"
40
41 #include <Include/fg_constants.h>
42 #include <Aircraft/aircraft.h>
43 #include <Main/fg_debug.h>
44 #include <Math/fg_random.h>
45 #include <Math/mat3.h>
46 #include <Math/polar.h>
47 #include <Scenery/scenery.h>
48 #include <Time/fg_timer.h>
49 #include <Weather/weather.h>
50
51 // #define DEBUG
52
53 #define drawOneLine(x1,y1,x2,y2)  glBegin(GL_LINES);  \
54    glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();
55
56
57 // The following routines obtain information concerntin the aircraft's
58 // current state and return it to calling instrument display routines.
59 // They should eventually be member functions of the aircraft.
60 //
61
62 double get_throttleval( void )
63 {
64         fgCONTROLS *pcontrols;
65
66   pcontrols = current_aircraft.controls;
67   return pcontrols->throttle[0];     // Hack limiting to one engine
68 }
69
70 double get_aileronval( void )
71 {
72         fgCONTROLS *pcontrols;
73
74   pcontrols = current_aircraft.controls;
75   return pcontrols->aileron;
76 }
77
78 double get_elevatorval( void )
79 {
80         fgCONTROLS *pcontrols;
81
82   pcontrols = current_aircraft.controls;
83   return pcontrols->elevator;
84 }
85
86 double get_elev_trimval( void )
87 {
88         fgCONTROLS *pcontrols;
89
90   pcontrols = current_aircraft.controls;
91   return pcontrols->elevator_trim;
92 }
93
94 double get_rudderval( void )
95 {
96         fgCONTROLS *pcontrols;
97
98   pcontrols = current_aircraft.controls;
99   return pcontrols->rudder;
100 }
101
102 double get_speed( void )
103 {
104         fgFLIGHT *f;
105
106         f = current_aircraft.flight;
107         return( FG_V_equiv_kts );    // Make an explicit function call.
108 }
109
110 double get_aoa( void )
111 {
112         fgFLIGHT *f;
113               
114         f = current_aircraft.flight;
115         return( FG_Gamma_vert_rad * RAD_TO_DEG );
116 }
117
118 double get_roll( void )
119 {
120         fgFLIGHT *f;
121
122         f = current_aircraft.flight;
123         return( FG_Phi );
124 }
125
126 double get_pitch( void )
127 {
128         fgFLIGHT *f;
129               
130         f = current_aircraft.flight;
131         return( FG_Theta );
132 }
133
134 double get_heading( void )
135 {
136         fgFLIGHT *f;
137
138         f = current_aircraft.flight;
139         return( FG_Psi*RAD_TO_DEG );
140 }
141
142 double get_altitude( void )
143 {
144         fgFLIGHT *f;
145         // double rough_elev;
146
147         f = current_aircraft.flight;
148         // rough_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC,
149         //                                 FG_Latitude  * RAD_TO_ARCSEC);
150
151         return( FG_Altitude * FEET_TO_METER /* -rough_elev */ );
152 }
153
154 //
155 // The following code deals with painting the "instrument" on the display
156 //
157    /* textString - Bitmap font string */
158
159 static void textString(int x, int y, char *msg, void *font)
160 {
161         glRasterPos2f(x, y);
162         while (*msg) {
163                 glutBitmapCharacter(font, *msg);
164                 msg++;
165     }
166 }
167
168 /* strokeString - Stroke font string */
169 /*
170 static void strokeString(int x, int y, char *msg, void *font)
171 {
172         glPushMatrix();
173         glTranslatef(x, y, 0);
174         glScalef(.04, .04, .04);
175         while (*msg) {
176                 glutStrokeCharacter(font, *msg);
177                 msg++;
178         }
179         glPopMatrix();
180 }
181 */
182
183 /*
184
185         Draws a measuring scale anywhere on the HUD
186
187
188         Needs: HUD_scale struct
189
190 */
191 static void drawscale( HUD_scale * pscale )
192 {
193   double vmin, vmax;
194   int marker_x;
195   int marker_y;
196   int scr_span;
197   int mid_scr;
198   register i;
199   double factor;
200   char TextScale[80];
201   int condition;
202   double cur_value = (*(pscale->load_value))();
203
204   vmin = cur_value - pscale->width_units / 2.0; // width units == needle travel
205   vmax = cur_value + pscale->width_units / 2.0; // or picture unit span.
206
207   scr_span = pscale->scr_max - pscale->scr_min; // Run of scan in pix coord
208   scr_span |= 1;  // If span is odd number of units, mid will be correct.
209                   // If not it will be high by one coordinate unit. This is
210                   // an artifact of integer division needed for screen loc's.
211
212   mid_scr  = (scr_span >> 1) + pscale->scr_min; // Middle is half that +
213                                                 // starting point.
214
215              // Calculate the number of screen units per indicator unit
216              // We must force floating point calculation or the factor
217              // will be low and miss locate tics by several units.
218
219   factor   = (double)scr_span / (double)pscale->width_units;
220
221              // Separate calculations for location of markings by scale
222              // type V vs H.
223
224   if( pscale->type == VERTICAL )     // Vertical scale
225     {
226
227     if( pscale->orientation == LEFT )     // Calculate x marker offset
228       marker_x = pscale->scr_pos-6;
229     else
230       if( pscale->orientation == RIGHT )
231         marker_x = pscale->scr_pos;
232
233     // Draw the basic markings for the scale...
234
235     drawOneLine( pscale->scr_pos,         // Vertical bar
236                  pscale->scr_min,
237                  pscale->scr_pos,
238                  pscale->scr_max );
239     if( pscale->orientation == LEFT )
240       {
241       drawOneLine( pscale->scr_pos-3,     // Bottom tick bar
242                    pscale->scr_min,
243                    pscale->scr_pos,
244                    pscale->scr_min );
245
246       drawOneLine( pscale->scr_pos-3,     // Top tick bar
247                    pscale->scr_max,
248                    pscale->scr_pos,
249                    pscale->scr_max );
250
251       drawOneLine( pscale->scr_pos,       // Middle tick bar /Index
252                    mid_scr,
253                    pscale->scr_pos+6,
254                    mid_scr );
255
256       }
257     else
258       if( pscale->orientation == RIGHT )
259         {
260         drawOneLine( pscale->scr_pos,
261                      pscale->scr_min,
262                      pscale->scr_pos+3,
263                      pscale->scr_min );
264
265         drawOneLine( pscale->scr_pos,
266                      pscale->scr_max,
267                      pscale->scr_pos+3,
268                      pscale->scr_max );
269
270         drawOneLine( pscale->scr_pos,
271                      mid_scr,
272                      pscale->scr_pos-6,
273                      mid_scr );
274         }
275
276     // Work through from bottom to top of scale. Calculating where to put
277     // minor and major ticks.
278
279     for( i = vmin; i <= vmax; i++ )
280       {
281       if( pscale->sub_type == LIMIT ) {           // Don't show ticks
282         condition = (i >= pscale->minimum_value); // below Minimum value.
283         }
284       else {
285         if( pscale->sub_type == NOLIMIT ) {
286           condition = 1;
287           }
288         }
289       if( condition )   // Show a tick if necessary
290         {
291         // Calculate the location of this tick
292         marker_y = pscale->scr_min + (i - vmin) * factor;
293
294         // Block calculation artifact from drawing ticks below min coordinate.
295         // Calculation here accounts for text height.
296
297         if( marker_y < (pscale->scr_min + 4)) {  // Magic number!!!
298           continue;
299           }
300         if( (i%pscale->div_min) == 0) {
301           if( pscale->orientation == LEFT )
302             {
303             drawOneLine( marker_x + 3, marker_y, marker_x + 6, marker_y );
304             }
305           else {
306             if( pscale->orientation == RIGHT )
307               {
308               drawOneLine( marker_x, marker_y, marker_x + 3, marker_y );
309               }
310             }
311           }
312         if( (i%pscale->div_max) == 0 )            {
313           drawOneLine( marker_x,     marker_y,
314                        marker_x + 6, marker_y );
315           sprintf( TextScale, "%d", i );
316           if( pscale->orientation == LEFT )              {
317             textString( marker_x -  8 * strlen(TextScale) - 2, marker_y - 4,
318                         TextScale, GLUT_BITMAP_8_BY_13 );
319             }
320           else  {
321             if( pscale->orientation == RIGHT )              {
322             textString( marker_x + 10,                         marker_y - 4,
323                         TextScale, GLUT_BITMAP_8_BY_13 );
324               }
325             }
326           }
327         } // End if condition
328       } // End for range of i from vmin to vmax
329     }  // End if VERTICAL SCALE TYPE
330   if( pscale->type == HORIZONTAL )     // Horizontal scale
331     {
332     if( pscale->orientation == TOP ) {
333       marker_y = pscale->scr_pos;
334       }
335     else {
336       if( pscale->orientation == BOTTOM ) {
337         marker_y = pscale->scr_pos - 6;
338         }
339       }
340     drawOneLine( pscale->scr_min,
341                  pscale->scr_pos,
342                  pscale->scr_max,
343                  pscale->scr_pos );
344     if( pscale->orientation == TOP )
345       {
346       drawOneLine( pscale->scr_min,
347                    pscale->scr_pos,
348                    pscale->scr_min,
349                    pscale->scr_pos-3 );
350
351       drawOneLine( pscale->scr_max,
352                    pscale->scr_pos,
353                    pscale->scr_max,
354                    pscale->scr_pos-3 );
355
356       drawOneLine( mid_scr,
357                    pscale->scr_pos,
358                    mid_scr,
359                    pscale->scr_pos-6 );
360
361       }
362     else {
363       if( pscale->orientation == BOTTOM )
364         {
365         drawOneLine( pscale->scr_min,
366                      pscale->scr_pos,
367                      pscale->scr_min,
368                      pscale->scr_pos+3 );
369
370         drawOneLine( pscale->scr_max,
371                      pscale->scr_pos,
372                      pscale->scr_max,
373                      pscale->scr_pos+3 );
374
375         drawOneLine( mid_scr,
376                      pscale->scr_pos,
377                      mid_scr,
378                      pscale->scr_pos+6 );
379         }
380       }
381
382     for( i = vmin; i <= vmax; i++ )  // increment is faster than addition
383       {
384       if( pscale->sub_type == LIMIT ) {
385         condition = (i >= pscale->minimum_value);
386         }
387       else {
388         if( pscale->sub_type == NOLIMIT ) {
389           condition = 1;
390           }
391         }
392       if( condition )        {
393         marker_x = pscale->scr_min+(i-vmin)*factor;
394         if( i%pscale->div_min==0 ) {
395           if( pscale->orientation == TOP )
396             {
397             drawOneLine( marker_x, marker_y, marker_x, marker_y+3 );
398             }
399           else {
400             if( pscale->orientation == BOTTOM )
401               {
402               drawOneLine( marker_x, marker_y+3, marker_x, marker_y+6 );
403               }
404             }
405           }
406         if( i%pscale->div_max==0 )
407           {
408           sprintf( TextScale, "%d", i );
409           if( pscale->orientation == TOP )
410             {
411             drawOneLine( marker_x, marker_y, marker_x, marker_y+6 );
412             textString ( marker_x-4*strlen(TextScale), marker_y+14,
413                          TextScale, GLUT_BITMAP_8_BY_13 );
414             }
415           else {
416             if( pscale->orientation == BOTTOM )
417               {
418               drawOneLine( marker_x, marker_y, marker_x, marker_y+6 );
419               textString ( marker_x+10, marker_y-4,
420                            TextScale, GLUT_BITMAP_8_BY_13 );
421               }
422             }
423           }
424         }
425       }
426     }
427
428 }
429
430 //
431 //      Draws a climb ladder in the center of the HUD
432 //
433
434 static void drawladder( HUD_ladder *ladder )
435 {
436   double vmin, vmax;
437   double roll_value, pitch_value;
438   int marker_x, marker_y;
439 #ifdef DEBUGHUD
440   int mid_scr;
441 #endif
442   int scr_min, scr_max;
443   int x_ini, x_end;
444   int y_ini, y_end;
445   int new_x_ini, new_x_end;
446   int new_y_ini, new_y_end;
447   register i;
448   double factor;
449   char TextLadder[80];
450   int condition;
451
452   roll_value = (*ladder->load_roll)();
453   pitch_value = (*ladder->load_pitch)()*RAD_TO_DEG;
454
455   vmin = pitch_value - ladder->width_units/2;
456   vmax = pitch_value + ladder->width_units/2;
457
458   scr_min = ladder->y_pos - (ladder->scr_height/2);
459   scr_max = scr_min       + ladder->scr_height;
460
461 #ifdef DEBUGHUD
462   mid_scr = scr_min       + (scr_max-scr_min)/2;
463 #endif
464
465   marker_x = ladder->x_pos - ladder->scr_width/2;
466
467   factor = (scr_max-scr_min)/ladder->width_units;
468
469   for( i=vmin; i<=vmax; i+=1 )
470     {
471     condition = 1;
472     if( condition )
473       {
474       marker_y = scr_min+(i-vmin)*factor;
475       if( i%ladder->div_units==0 )
476         {
477         sprintf( TextLadder, "%d", i );
478         if( ladder->scr_hole == 0 )
479           {
480           if( i ) {
481             x_ini = ladder->x_pos - ladder->scr_width/2;
482             }
483           else {
484             x_ini = ladder->x_pos - ladder->scr_width/2 - 10;
485             }
486           y_ini = marker_y;
487           x_end = ladder->x_pos + ladder->scr_width/2;
488           y_end = marker_y;
489           new_x_ini = ladder->x_pos +                             \
490                       (x_ini - ladder->x_pos) * cos(roll_value) - \
491                       (y_ini - ladder->y_pos) * sin(roll_value);
492           new_y_ini = ladder->y_pos +                             \
493                       (x_ini - ladder->x_pos) * sin(roll_value) + \
494                       (y_ini - ladder->y_pos) * cos(roll_value);
495           new_x_end = ladder->x_pos +                             \
496                       (x_end - ladder->x_pos) * cos(roll_value) - \
497                       (y_end - ladder->y_pos) * sin(roll_value);
498           new_y_end = ladder->y_pos +                             \
499                       (x_end - ladder->x_pos) * sin(roll_value) + \
500                       (y_end - ladder->y_pos) * cos(roll_value);
501
502           if( i >= 0 )
503             {
504             drawOneLine( new_x_ini, new_y_ini, new_x_end, new_y_end );
505             }
506           else
507             {
508             glEnable(GL_LINE_STIPPLE);
509             glLineStipple( 1, 0x00FF );
510             drawOneLine( new_x_ini, new_y_ini, new_x_end, new_y_end );
511             glDisable(GL_LINE_STIPPLE);
512             }
513           textString( new_x_ini -  8 * strlen(TextLadder) - 8,
514                       new_y_ini -  4,
515                       TextLadder, GLUT_BITMAP_8_BY_13 );
516           textString( new_x_end + 10,
517                       new_y_end -  4,
518                       TextLadder, GLUT_BITMAP_8_BY_13 );
519           }
520         else
521           {
522           if( i != 0 )  {
523             x_ini = ladder->x_pos - ladder->scr_width/2;
524             }
525           else          {
526             x_ini = ladder->x_pos - ladder->scr_width/2 - 10;
527             }
528           y_ini = marker_y;
529           x_end = ladder->x_pos - ladder->scr_width/2 + ladder->scr_hole/2;
530           y_end = marker_y;
531           new_x_ini = ladder->x_pos+                             \
532                       (x_ini - ladder->x_pos) * cos(roll_value) -\
533                       (y_ini - ladder->y_pos) * sin(roll_value);
534           new_y_ini = ladder->y_pos+                             \
535                       (x_ini - ladder->x_pos) * sin(roll_value) +\
536                       (y_ini - ladder->y_pos) * cos(roll_value);
537           new_x_end = ladder->x_pos+                             \
538                       (x_end - ladder->x_pos) * cos(roll_value) -\
539                       (y_end - ladder->y_pos) * sin(roll_value);
540           new_y_end = ladder->y_pos+                             \
541                       (x_end - ladder->x_pos) * sin(roll_value) +\
542                       (y_end - ladder->y_pos) * cos(roll_value);
543
544           if( i >= 0 )
545             {
546             drawOneLine( new_x_ini, new_y_ini, new_x_end, new_y_end );
547             }
548           else
549             {
550             glEnable(GL_LINE_STIPPLE);
551             glLineStipple( 1, 0x00FF );
552             drawOneLine( new_x_ini, new_y_ini, new_x_end, new_y_end );
553             glDisable(GL_LINE_STIPPLE);
554             }
555           textString( new_x_ini - 8 * strlen(TextLadder) - 8,
556                       new_y_ini - 4,
557                       TextLadder, GLUT_BITMAP_8_BY_13 );
558
559           x_ini = ladder->x_pos + ladder->scr_width/2 - ladder->scr_hole/2;
560           y_ini = marker_y;
561           if( i != 0 )  {
562             x_end = ladder->x_pos + ladder->scr_width/2;
563             }
564           else          {
565             x_end = ladder->x_pos + ladder->scr_width/2 + 10;
566             }
567           y_end = marker_y;
568           new_x_ini = ladder->x_pos +                        \
569                       (x_ini-ladder->x_pos)*cos(roll_value) -\
570                       (y_ini-ladder->y_pos)*sin(roll_value);
571           new_y_ini = ladder->y_pos +                        \
572                       (x_ini-ladder->x_pos)*sin(roll_value) +\
573                       (y_ini-ladder->y_pos)*cos(roll_value);
574           new_x_end = ladder->x_pos +                        \
575                       (x_end-ladder->x_pos)*cos(roll_value) -\
576                                          (y_end-ladder->y_pos)*sin(roll_value);
577           new_y_end = ladder->y_pos +                        \
578                       (x_end-ladder->x_pos)*sin(roll_value) +\
579                       (y_end-ladder->y_pos)*cos(roll_value);
580
581           if( i >= 0 )
582             {
583             drawOneLine( new_x_ini, new_y_ini, new_x_end, new_y_end );
584             }
585           else
586             {
587             glEnable(GL_LINE_STIPPLE);
588             glLineStipple( 1, 0x00FF );
589             drawOneLine( new_x_ini, new_y_ini, new_x_end, new_y_end );
590             glDisable(GL_LINE_STIPPLE);
591             }
592           textString( new_x_end+10, new_y_end-4,
593                       TextLadder, GLUT_BITMAP_8_BY_13 );
594           }
595         }
596             /* if( i%pscale->div_max==0 )
597             {
598                 drawOneLine( marker_x, marker_y, marker_x+6, marker_y );
599                 sprintf( TextScale, "%d", i );
600                 if( pscale->orientation == LEFT )
601                 {
602                         textString( marker_x-8*strlen(TextScale)-2, marker_y-4,
603                               TextScale, GLUT_BITMAP_8_BY_13 );
604                 }
605                 else if( pscale->orientation == RIGHT )
606                 {
607                         textString( marker_x+10, marker_y-4,
608                               TextScale, GLUT_BITMAP_8_BY_13 );
609                 }
610             } */
611         }
612     }
613
614 }
615
616 //
617 //      Draws an artificial horizon line in the center of the HUD
618 //      (with or without a center hole)
619 //
620 //      Needs: x_center, y_center, length, hole
621 //
622
623 static void drawhorizon( HUD_horizon *horizon )
624 {
625   int x_inc1, y_inc1;
626   int x_inc2, y_inc2;
627 //      struct fgFLIGHT *f = &current_aircraft.flight;
628   double sin_bank, cos_bank;
629   double bank_angle;
630
631   bank_angle = (*horizon->load_value)();
632
633         // sin_bank = sin( FG_2PI-FG_Phi );
634         // cos_bank = cos( FG_2PI-FG_Phi );
635   sin_bank = sin(FG_2PI-bank_angle);
636   cos_bank = cos(FG_2PI-bank_angle);
637   x_inc1 = (int)(horizon->scr_width * cos_bank);
638   y_inc1 = (int)(horizon->scr_width * sin_bank);
639   x_inc2 = (int)(horizon->scr_hole  * cos_bank);
640   y_inc2 = (int)(horizon->scr_hole  * sin_bank);
641
642   if( horizon->scr_hole == 0 )
643     {
644     drawOneLine( horizon->x_pos - x_inc1, horizon->y_pos - y_inc1, \
645                  horizon->x_pos + x_inc1, horizon->y_pos + y_inc1 );
646     }
647   else
648     {
649     drawOneLine( horizon->x_pos - x_inc1, horizon->y_pos - y_inc1, \
650                  horizon->x_pos - x_inc2, horizon->y_pos - y_inc2 );
651     drawOneLine( horizon->x_pos + x_inc2, horizon->y_pos + y_inc2, \
652                  horizon->x_pos + x_inc1, horizon->y_pos + y_inc1 );
653     }
654 }
655
656 //  drawControlSurfaces()
657 //      Draws a representation of the control surfaces in their current state
658 //      anywhere in the HUD
659 //
660
661 static void drawControlSurfaces( HUD_control_surfaces *ctrl_surf )
662 {
663         int x_ini, y_ini;
664         int x_end, y_end;
665         /* int x_1, y_1; */
666         /* int x_2, y_2; */
667         fgCONTROLS *pCtls;
668
669         x_ini = ctrl_surf->x_pos;
670         y_ini = ctrl_surf->y_pos;
671         x_end = x_ini + 150;
672         y_end = y_ini + 60;
673
674         drawOneLine( x_ini, y_ini, x_end, y_ini );
675         drawOneLine( x_ini, y_ini, x_ini, y_end );
676         drawOneLine( x_ini, y_end, x_end, y_end );
677         drawOneLine( x_end, y_end, x_end, y_ini );
678         drawOneLine( x_ini + 30, y_ini, x_ini + 30, y_end );
679         drawOneLine( x_ini + 30, y_ini + 30, x_ini + 90, y_ini + 30 );
680         drawOneLine( x_ini + 90, y_ini, x_ini + 90, y_end );
681         drawOneLine( x_ini + 120, y_ini, x_ini + 120, y_end );
682
683         pCtls = current_aircraft.controls;
684
685         /* Draw elevator diagram */
686         textString( x_ini + 1, y_end-11, "E", GLUT_BITMAP_8_BY_13 );
687         drawOneLine( x_ini + 15, y_ini + 5, x_ini + 15, y_ini + 55 );
688         drawOneLine( x_ini + 14, y_ini + 30, x_ini + 16, y_ini + 30 );
689         if( pCtls->elevator <= -0.01 || pCtls->elevator >= 0.01 )
690         {
691                 drawOneLine( x_ini + 10, y_ini + 5 + (int)(((pCtls->elevator + 1.0)/2)*50.0), \
692                                 x_ini + 20, y_ini + 5 + (int)(((pCtls->elevator + 1.0)/2)*50.0) );
693         }
694         else
695         {
696                 drawOneLine( x_ini + 7, y_ini + 5 + (int)(((pCtls->elevator + 1.0)/2)*50.0), \
697                                 x_ini + 23, y_ini + 5 + (int)(((pCtls->elevator + 1.0)/2)*50.0) );
698         }
699
700         /* Draw aileron diagram */
701         textString( x_ini + 30 + 1, y_end-11, "A", GLUT_BITMAP_8_BY_13 );
702         drawOneLine( x_ini + 35, y_end-15, x_ini + 85, y_end-15 );
703         drawOneLine( x_ini + 60, y_end-14, x_ini + 60, y_end-16 );
704         if( pCtls->aileron <= -0.01 || pCtls->aileron >= 0.01 )
705         {
706                 drawOneLine( x_ini + 35 + (int)(((pCtls->aileron + 1.0)/2)*50.0), y_end-20, \
707                                 x_ini + 35 + (int)(((pCtls->aileron + 1.0)/2)*50.0), y_end-10 );
708         }
709         else
710         {
711                 drawOneLine( x_ini + 35 + (int)(((pCtls->aileron + 1.0) / 2) * 50.0),
712                  y_end - 25,
713                  x_ini + 35 + (int)(((pCtls->aileron + 1.0) / 2) * 50.0),
714                  y_end -  5 );
715         }
716
717         /* Draw rudder diagram */
718         textString ( x_ini + 30 + 1, y_ini + 21, "R", GLUT_BITMAP_8_BY_13 );
719         drawOneLine( x_ini + 35, y_ini + 15, x_ini + 85, y_ini + 15 );
720         drawOneLine( x_ini + 60, y_ini + 14, x_ini + 60, y_ini + 16 );
721
722         if( pCtls->rudder <= -0.01 || pCtls->rudder >= 0.01 )
723         {
724                 drawOneLine( x_ini + 35 + (int)(((pCtls->rudder + 1.0) / 2) * 50.0),
725                  y_ini + 20,
726                                          x_ini + 35 + (int)(((pCtls->rudder + 1.0) / 2) * 50.0),
727                  y_ini + 10 );
728         }
729         else
730         {
731                 drawOneLine( x_ini + 35 + (int)(((pCtls->rudder + 1.0) / 2) * 50.0),
732                  y_ini + 25,
733                  x_ini + 35 + (int)(((pCtls->rudder + 1.0) / 2) * 50.0),
734                  y_ini +  5 );
735         }
736
737
738         /* Draw throttle diagram */
739         textString( x_ini + 90 + 1, y_end-11, "T", GLUT_BITMAP_8_BY_13 );
740         textString( x_ini + 90 + 1, y_end-21, "r", GLUT_BITMAP_8_BY_13 );
741         drawOneLine( x_ini + 105, y_ini + 5, x_ini + 105, y_ini + 55 );
742         drawOneLine( x_ini + 100, y_ini + 5 + (int)(pCtls->throttle[0]*50.0), \
743                         x_ini + 110, y_ini + 5 + (int)(pCtls->throttle[0]*50.0) );
744
745
746         /* Draw elevator trim diagram */
747         textString( x_ini + 121, y_end-11, "T", GLUT_BITMAP_8_BY_13 );
748         textString( x_ini + 121, y_end-22, "m", GLUT_BITMAP_8_BY_13 );
749         drawOneLine( x_ini + 135, y_ini + 5, x_ini + 135, y_ini + 55 );
750         drawOneLine( x_ini + 134, y_ini + 30, x_ini + 136, y_ini + 30 );
751         if( pCtls->elevator_trim <= -0.01 || pCtls->elevator_trim >= 0.01 )
752         {
753                 drawOneLine( x_ini + 130, y_ini + 5 + (int)(((pCtls->elevator_trim + 1)/2)*50.0), \
754                                 x_ini + 140, y_ini + 5 + (int)(((pCtls->elevator_trim + 1.0)/2)*50.0) );
755         }
756         else
757         {
758                 drawOneLine( x_ini + 127, y_ini + 5 + (int)(((pCtls->elevator_trim + 1.0)/2)*50.0), \
759                                 x_ini + 143, y_ini + 5 + (int)(((pCtls->elevator_trim + 1.0)/2)*50.0) );
760         }
761
762 }
763
764 //
765 // Draws a label anywhere in the HUD
766 //
767 //
768
769 static void drawlabel( HUD_label *label )
770 {
771   char buffer[80];
772   char string[80];
773   int posincr;
774   int lenstr;
775
776   if( !label ) { // Eliminate the possible, but absurd case.
777     return;
778     }
779
780   if( label->pre_str != NULL) {
781     if( label->post_str != NULL ) {
782       sprintf( buffer, "%s%s%s", label->pre_str,  \
783                                  label->format,   \
784                                  label->post_str );
785       }
786     else {
787       sprintf( buffer, "%s%s",   label->pre_str, \
788                                  label->format );
789       }
790     }
791   else {
792     if( label->post_str != NULL ) {
793       sprintf( buffer, "%s%s",   label->format,  \
794                                  label->post_str );
795       }
796     } // else do nothing if both pre and post strings are nulls. Interesting.
797
798
799   sprintf( string, buffer, (*label->load_value)() );
800 #ifdef DEBUGHUD
801         fgPrintf( FG_COCKPIT, FG_DEBUG,  buffer );
802         fgPrintf( FG_COCKPIT, FG_DEBUG,  "\n" );
803         fgPrintf( FG_COCKPIT, FG_DEBUG, string );
804         fgPrintf( FG_COCKPIT, FG_DEBUG, "\n" );
805 #endif
806   lenstr = strlen( string );
807   if( label->justify == LEFT_JUST ) {
808    posincr = -lenstr*8;
809    }
810   else {
811     if( label->justify == CENTER_JUST ) {
812       posincr = -lenstr*4;
813       }
814     else {
815       if( label->justify == RIGHT_JUST ) {
816         posincr = 0;
817         }
818       }
819     }
820
821   if( label->size == SMALL ) {
822     textString( label->x_pos + posincr, label->y_pos,
823                 string, GLUT_BITMAP_8_BY_13);
824     }
825   else  {
826     if( label->size == LARGE ) {
827       textString( label->x_pos + posincr, label->y_pos,
828                   string, GLUT_BITMAP_9_BY_15);
829       }
830     }
831 }
832 // The following routines concern HUD object/component object construction
833 //
834
835 // fgHUDInit
836 //
837 // Constructs a HUD object and then adds in instruments. At the present
838 // the instruments are hard coded into the routine. Ultimately these need
839 // to be defined by the aircraft's instrumentation records so that the
840 // display for a Piper Cub doesn't show the speed range of a North American
841 // mustange and the engine readouts of a B36!
842 //
843 Hptr fgHUDInit( fgAIRCRAFT *current_aircraft )
844 {
845   Hptr hud;
846
847   fgPrintf( FG_COCKPIT, FG_INFO, "Initializing HUD\n" );
848
849   hud = (Hptr)calloc(sizeof( HUD),1);
850   if( hud == NULL )
851     return( NULL );
852
853   hud->code = 1;
854   hud->status = 0;
855
856   // For now lets just hardcode the hud here.
857   // In the future, hud information has to come from the same place
858   // aircraft information came from.
859
860   fgHUDAddHorizon( hud, 590, 50, 40, 20, get_roll );
861   fgHUDAddLadder ( hud, 330, 190, 90, 180, 70, 10,
862                    NONE, 45, get_roll, get_pitch );
863   fgHUDAddScale  ( hud, VERTICAL,     LIMIT, 220, 100, 280, 5, 10,
864                       LEFT,    0, 100, 50, get_speed );
865   fgHUDAddScale  ( hud, VERTICAL,   NOLIMIT, 440, 100, 280, 1,  5,
866                       RIGHT, -40,  50, 25, get_aoa );
867   fgHUDAddScale  ( hud, HORIZONTAL, NOLIMIT, 280, 220, 440, 5, 10,
868                       TOP,     0,  50, 50, get_heading );
869   fgHUDAddLabel  ( hud, 180, 85, SMALL, NOBLINK,
870                    RIGHT_JUST, NULL, " Kts", "%5.0f", get_speed );
871   fgHUDAddLabel  ( hud, 180, 73, SMALL, NOBLINK,
872                    RIGHT_JUST, NULL, " m", "%5.0f", get_altitude );
873   fgHUDAddControlSurfaces( hud, 10, 10, NULL );
874
875 //  fgHUDAddControl( hud, HORIZONTAL, 50,  25, get_aileronval  ); // was 10, 10
876 //  fgHUDAddControl( hud, VERTICAL,   150, 25, get_elevatorval ); // was 10, 10
877 //  fgHUDAddControl( hud, HORIZONTAL, 250, 25, get_rudderval   ); // was 10, 10
878
879   return( hud );
880 }
881
882
883 // add_instrument
884 //
885 // This is a stand in for linked list code that will get replaced later
886 // by some more elegant list handling code.
887
888 void add_instrument( Hptr hud, HIptr pinstrument )
889 {
890     if( !hud || !pinstrument ) {
891         return;
892     }
893
894     pinstrument->next = hud->instruments;
895     hud->instruments = pinstrument;
896 }
897
898
899 // fgHUDAddHorizon
900 //
901 // Constructs a HUD_horizon "object" and installs it into the hud instrument
902 // list.
903
904 Hptr fgHUDAddHorizon( Hptr hud,     \
905                       int x_pos,    \
906                       int y_pos,    \
907                       int length,   \
908                       int hole_len, \
909                       double (*load_value)() )
910 {
911     HUD_horizon *phorizon;
912     HUD_instr   *pinstrument;
913
914     if( !hud ) {
915         return NULL;
916     }
917                                        // construct the parent object
918     pinstrument = (HIptr)calloc(sizeof(HUD_instr),1);
919     if( pinstrument == NULL ) {
920         return( NULL );
921     }
922     pinstrument->type    = HUDhorizon;  //  ARTIFICIAL_HORIZON;
923
924                                       // Construct the horizon
925     phorizon = (HUD_horizon *) calloc( sizeof(HUD_horizon),1);
926     if( phorizon == NULL )   {
927         return( NULL );
928     }
929
930     phorizon->x_pos      = x_pos;
931     phorizon->y_pos      = y_pos;
932     phorizon->scr_width  = length;
933     phorizon->scr_hole   = hole_len;
934     phorizon->load_value = load_value;
935     //  Install the horizon in the parent.
936     pinstrument->instr   = phorizon;
937     //  Install the instrument into hud.
938     add_instrument( hud, pinstrument);
939
940     return( hud );
941 }
942
943 // fgHUDAddScale
944 //
945 // Constructs a HUD_scale "object" and installs it into the hud instrument
946 // list.
947
948 Hptr fgHUDAddScale( Hptr hud,        \
949                     int type,        \
950                     int sub_type,    \
951                     int scr_pos,     \
952                     int scr_min,     \
953                     int scr_max,     \
954                     int div_min,     \
955                     int div_max,     \
956                     int orientation, \
957                     int min_value,   \
958                     int max_value,   \
959                     int width_units, \
960                     double (*load_value)() )
961 {
962   HUD_scale *pscale;
963   HUD_instr *pinstrument;
964
965   if( !hud ) {
966     return NULL;
967     }
968
969         pinstrument = (HIptr)calloc(sizeof(HUD_instr),1);
970         if( pinstrument == NULL ) {
971      return( NULL );
972      }
973
974   pinstrument->type = HUDscale;
975
976   pscale = ( HUD_scale *)calloc(sizeof(HUD_scale),1);
977   if( pscale == NULL )   {
978      return( NULL );
979     }
980   pscale->type          = type;
981   pscale->sub_type      = sub_type;
982   pscale->scr_pos       = scr_pos;
983   pscale->scr_min       = scr_min;
984   pscale->scr_max       = scr_max;
985   pscale->div_min       = div_min;
986   pscale->div_max       = div_max;
987   pscale->orientation   = orientation;
988   pscale->minimum_value = min_value;
989   pscale->maximum_value = max_value;
990   pscale->width_units   = width_units;
991   pscale->load_value    = load_value;
992                                      // Install the scale
993   pinstrument->instr = pscale;
994                                       //  Install the instrument into hud.
995   add_instrument( hud, pinstrument);
996
997   return( hud );
998 }
999
1000 // fgHUDAddLabel
1001 //
1002 // Constructs a HUD_Label object and installs it into the hud instrument
1003 // list.
1004 Hptr fgHUDAddLabel( Hptr hud,       \
1005                     int x_pos,      \
1006                     int y_pos,      \
1007                     int size,       \
1008                     int blink,      \
1009                     int justify,    \
1010                                                   char *pre_str,  \
1011                     char *post_str, \
1012                     char *format,   \
1013                     double (*load_value)() )
1014 {
1015         HUD_label *plabel;
1016         HUD_instr *pinstrument;
1017
1018   if( !hud ) {
1019     return NULL;
1020     }
1021
1022         pinstrument = (HIptr)calloc(sizeof(HUD_instr),1);
1023         if( pinstrument == NULL ) {
1024     return NULL;
1025     }
1026         pinstrument->type = HUDlabel;
1027
1028         plabel = (HUD_label *)calloc(sizeof(HUD_label),1);
1029         if( plabel == NULL ){
1030     return NULL;
1031     }
1032
1033   plabel->x_pos      = x_pos;
1034   plabel->y_pos      = y_pos;
1035   plabel->size       = size;
1036   plabel->blink      = blink;
1037   plabel->justify    = justify;
1038   plabel->pre_str    = pre_str;
1039   plabel->post_str   = post_str;
1040   plabel->format     = format;
1041   plabel->load_value = load_value;
1042                                       // Install the label
1043         pinstrument->instr = plabel;
1044                                       //  Install the instrument into hud.
1045   add_instrument( hud, pinstrument);
1046
1047         return( hud );
1048 }
1049
1050 // fgHUDAddLadder
1051 //
1052 // Contains code that constructs a ladder "object" and installs it as
1053 // a hud instrument in the hud instrument list.
1054 //
1055 Hptr fgHUDAddLadder( Hptr hud,        \
1056                      int x_pos,       \
1057                      int y_pos,       \
1058                      int scr_width,   \
1059                      int scr_height,  \
1060                                                    int hole_len,    \
1061                      int div_units,   \
1062                      int label_pos,   \
1063                      int width_units, \
1064                                                    double (*load_roll)(),
1065                      double (*load_pitch)() )
1066 {
1067         HUD_ladder *pladder;
1068         HUD_instr  *pinstrument;
1069
1070   if( !hud ) {
1071     return NULL;
1072     }
1073
1074         pinstrument = (HIptr)calloc(sizeof(HUD_instr),1);
1075         if( pinstrument == NULL )
1076                 return( NULL );
1077
1078         pinstrument->type = HUDladder;
1079
1080         pladder = (HUD_ladder *)calloc(sizeof(HUD_ladder),1);
1081         if( pladder == NULL )
1082                 return( NULL );
1083
1084   pladder->type           = 0; // Not used.
1085   pladder->x_pos          = x_pos;
1086   pladder->y_pos          = y_pos;
1087   pladder->scr_width      = scr_width;
1088   pladder->scr_height     = scr_height;
1089   pladder->scr_hole       = hole_len;
1090   pladder->div_units      = div_units;
1091   pladder->label_position = label_pos;
1092   pladder->width_units    = width_units;
1093   pladder->load_roll      = load_roll;
1094   pladder->load_pitch     = load_pitch;
1095
1096   pinstrument->instr      = pladder;
1097                                       //  Install the instrument into hud.
1098   add_instrument( hud, pinstrument);
1099         return hud;
1100 }
1101
1102 //   fgHUDAddControlSurfaces()
1103 //
1104 //   Adds the control surface indicators which make up for the lack of seat
1105 //   of the pants feel. Should be unnecessary with joystick and pedals
1106 //   enabled. But that is another improvement. Also, what of flaps? Spoilers?
1107 //   This may need to be expanded or flattened into multiple indicators,
1108 //   vertical and horizontal.
1109
1110 Hptr fgHUDAddControlSurfaces( Hptr hud,
1111                               int x_pos,
1112                               int y_pos,
1113                               double (*load_value)() )
1114 {
1115         HUD_control_surfaces *pcontrol_surfaces;
1116         HUD_instr *pinstrument;
1117
1118     if( !hud ) {
1119       return NULL;
1120     }
1121
1122     // Construct shell
1123     pinstrument = (HIptr)calloc(sizeof(HUD_instr),1);
1124     if( !pinstrument ) {
1125       return NULL;
1126       }
1127     pinstrument->type = HUDcontrol_surfaces;
1128
1129     // Construct core
1130     pcontrol_surfaces = (HUD_control_surfaces *)calloc(sizeof(HUD_control),1);
1131     if( !pcontrol_surfaces ) {
1132       return( NULL );
1133       }
1134
1135     pcontrol_surfaces->x_pos = x_pos;
1136     pcontrol_surfaces->y_pos = y_pos;
1137     pcontrol_surfaces->load_value = load_value;
1138
1139     pinstrument->instr     = pcontrol_surfaces;
1140                                                    // Install
1141     add_instrument( hud, pinstrument);
1142
1143     return hud;
1144 }
1145
1146 // fgHUDAddControl
1147 //
1148 //
1149
1150 Hptr fgHUDAddControl( Hptr hud,        \
1151                       int ctrl_x,      \
1152                       int ctrl_y,      \
1153                       int ctrl_length, \
1154                       int orientation, \
1155                       int alignment,   \
1156                       int min_value,   \
1157                       int max_value,   \
1158                       int width_units, \
1159                       double (*load_value)() )
1160 {
1161     HUD_control *pcontrol;
1162     HUD_instr *pinstrument;
1163
1164     if( !hud ) {
1165       return NULL;
1166     }
1167
1168     // Construct shell
1169     pinstrument = (HIptr)calloc(sizeof(HUD_instr),1);
1170     if( !pinstrument ) {
1171       return NULL;
1172       }
1173     pinstrument->type = HUDcontrol;
1174
1175     // Construct core
1176     pcontrol = (HUD_control *)calloc(sizeof(HUD_control),1);
1177     if( !(pcontrol == NULL) ) {
1178       return( NULL );
1179       }
1180     pcontrol->ctrl_x        = ctrl_x;
1181     pcontrol->ctrl_y        = ctrl_y;
1182     pcontrol->ctrl_length   = ctrl_length;
1183     pcontrol->orientation   = orientation;
1184     pcontrol->alignment     = alignment;
1185     pcontrol->min_value     = min_value;
1186     pcontrol->max_value     = max_value;
1187     pcontrol->width_units   = width_units;
1188     pcontrol->load_value    = load_value;
1189                                                    // Integrate
1190     pinstrument->instr     = pcontrol;
1191                                                    // Install
1192     add_instrument( hud, pinstrument);
1193
1194     return hud;
1195 }
1196
1197 /*
1198 Hptr fgHUDAddMovingHorizon(  Hptr hud,     \
1199                              int x_pos,    \
1200                              int y_pos,    \
1201                              int length,   \
1202                              int hole_len, \
1203                              int color )
1204 {
1205
1206 }
1207
1208 Hptr fgHUDAddCircularLadder( Hptr hud,    \
1209                              int scr_min, \
1210                              int scr_max, \
1211                              int div_min, \
1212                              int div_max, \
1213                              int max_value )
1214 {
1215
1216 }
1217
1218 Hptr fgHUDAddNumDisp( Hptr hud,           \
1219                       int x_pos,          \
1220                       int y_pos,          \
1221                       int size,           \
1222                       int color,          \
1223                       int blink,          \
1224                                                           char *pre_str,      \
1225                       char *post_str )
1226 {
1227
1228 }
1229 */
1230
1231 // fgUpdateHUD
1232 //
1233 // Performs a once around the list of calls to instruments installed in
1234 // the HUD object with requests for redraw. Kinda. It will when this is
1235 // all C++.
1236 //
1237
1238 void fgUpdateHUD( Hptr hud ) {
1239     HIptr phud_instr;
1240
1241     glMatrixMode(GL_PROJECTION);
1242     glPushMatrix();
1243
1244     glLoadIdentity();
1245     gluOrtho2D(0, 640, 0, 480);
1246     glMatrixMode(GL_MODELVIEW);
1247     glPushMatrix();
1248     glLoadIdentity();
1249
1250     glColor3f(1.0, 1.0, 1.0);
1251     glIndexi(7);
1252
1253     glDisable(GL_DEPTH_TEST);
1254     glDisable(GL_LIGHTING);
1255
1256     glLineWidth(1);
1257     glColor3f (0.1, 0.9, 0.1);
1258
1259     fgPrintf( FG_COCKPIT, FG_DEBUG, "HUD Code %d  Status %d\n",
1260               hud->code, hud->status );
1261
1262     phud_instr = hud->instruments;
1263     while( phud_instr ) {
1264         /* printf("Drawing Instrument %d\n", phud_instr->type); */
1265
1266         switch (phud_instr->type) {
1267     case HUDhorizon:   // ARTIFICIAL HORIZON
1268             drawhorizon( (pHUDhorizon)phud_instr->instr );
1269             break;
1270
1271     case HUDscale:     // Need to simplify this call.
1272             drawscale (  (pHUDscale)  phud_instr->instr  );
1273             break;
1274
1275     case HUDlabel:
1276             drawlabel (  (pHUDlabel)  phud_instr->instr  );
1277             break;
1278
1279     case HUDladder:
1280             drawladder(  (pHUDladder) phud_instr->instr  );
1281             break;
1282
1283 //    case HUDcontrol:
1284 //      drawControl( (pHUDcontrol) phud_instr->instr );
1285 //      break;
1286
1287     case HUDcontrol_surfaces:
1288             drawControlSurfaces( (pHUDControlSurfaces) phud_instr->instr );
1289             break;
1290
1291     default:; // Ignore anything you don't know about.
1292     }
1293
1294   phud_instr = phud_instr->next;
1295   }
1296
1297   glEnable(GL_DEPTH_TEST);
1298   glEnable(GL_LIGHTING);
1299   glMatrixMode(GL_PROJECTION);
1300   glPopMatrix();
1301   glMatrixMode(GL_MODELVIEW);
1302   glPopMatrix();
1303 }
1304
1305
1306 /* $Log$
1307 /* Revision 1.15  1998/02/16 13:38:39  curt
1308 /* Integrated changes from Charlie Hotchkiss.
1309 /*
1310  * Revision 1.14  1998/02/12 21:59:41  curt
1311  * Incorporated code changes contributed by Charlie Hotchkiss
1312  * <chotchkiss@namg.us.anritsu.com>
1313  *
1314  * Revision 1.12  1998/02/09 15:07:48  curt
1315  * Minor tweaks.
1316  *
1317  * Revision 1.11  1998/02/07 15:29:34  curt
1318  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
1319  * <chotchkiss@namg.us.anritsu.com>
1320  *
1321  * Revision 1.10  1998/02/03 23:20:14  curt
1322  * Lots of little tweaks to fix various consistency problems discovered by
1323  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
1324  * passed arguments along to the real printf().  Also incorporated HUD changes
1325  * by Michele America.
1326  *
1327  * Revision 1.9  1998/01/31 00:43:04  curt
1328  * Added MetroWorks patches from Carmen Volpe.
1329  *
1330  * Revision 1.8  1998/01/27 00:47:51  curt
1331  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
1332  * system and commandline/config file processing code.
1333  *
1334  * Revision 1.7  1998/01/19 18:40:20  curt
1335  * Tons of little changes to clean up the code and to remove fatal errors
1336  * when building with the c++ compiler.
1337  *
1338  * Revision 1.6  1997/12/15 23:54:34  curt
1339  * Add xgl wrappers for debugging.
1340  * Generate terrain normals on the fly.
1341  *
1342  * Revision 1.5  1997/12/10 22:37:39  curt
1343  * Prepended "fg" on the name of all global structures that didn't have it yet.
1344  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
1345  *
1346  * Revision 1.4  1997/09/23 00:29:32  curt
1347  * Tweaks to get things to compile with gcc-win32.
1348  *
1349  * Revision 1.3  1997/09/05 14:17:26  curt
1350  * More tweaking with stars.
1351  *
1352  * Revision 1.2  1997/09/04 02:17:30  curt
1353  * Shufflin' stuff.
1354  *
1355  * Revision 1.1  1997/08/29 18:03:22  curt
1356  * Initial revision.
1357  *
1358  */