]> git.mxchange.org Git - flightgear.git/blob - Simulator/Cockpit/hud.cxx
Initial revision.
[flightgear.git] / Simulator / Cockpit / hud.cxx
1 // hud.cxx -- hud defines and prototypes
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - micheleamerica@geocities.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #ifdef __BORLANDC__
33 #  define exception c_exception
34 #endif
35 #include <math.h>
36
37 #include <GL/glut.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <Aircraft/aircraft.hxx>
42 #include <Debug/logstream.hxx>
43 #include <Include/fg_constants.h>
44 #include <Main/options.hxx>
45 #include <Math/fg_random.h>
46 #include <Math/mat3.h>
47 #include <Math/polar3d.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Time/fg_timer.hxx>
50
51 #if defined ( __sun__ ) || defined ( __sgi )
52 extern "C" {
53   extern void *memmove(void *, const void *, size_t);
54 }
55 #endif
56
57 #include "hud.hxx"
58
59
60 static char units[5];
61
62 // The following routines obtain information concerntin the aircraft's
63 // current state and return it to calling instrument display routines.
64 // They should eventually be member functions of the aircraft.
65 //
66
67 HudContainerType HUD_deque;
68
69 class locRECT {
70   public:
71     RECT rect;
72
73     locRECT( UINT left, UINT top, UINT right, UINT bottom);
74     RECT get_rect(void) { return rect;}
75 };
76
77 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
78 {
79   rect.left   =  left;
80   rect.top    =  top;
81   rect.right  =  right;
82   rect.bottom =  bottom;
83
84 }
85 // #define DEBUG
86
87 void drawOneLine( UINT x1, UINT y1, UINT x2, UINT y2)
88 {
89   glBegin(GL_LINES);
90   glVertex2f(x1, y1);
91   glVertex2f(x2, y2);
92   glEnd();
93 }
94
95 void drawOneLine( RECT &rect)
96 {
97   glBegin(GL_LINES);
98   glVertex2f(rect.left, rect.top);
99   glVertex2f(rect.right, rect.bottom);
100   glEnd();
101 }
102
103 //
104 // The following code deals with painting the "instrument" on the display
105 //
106    /* textString - Bitmap font string */
107
108 void textString( int x, int y, char *msg, void *font ){
109         glRasterPos2f(x, y);
110         while (*msg) {
111                 glutBitmapCharacter(font, *msg);
112                 msg++;
113     }
114 }
115
116 /* strokeString - Stroke font string */
117
118 void strokeString(int x, int y, char *msg, void *font, float theta)
119 {
120 int xx;
121 int yy;
122
123         glPushMatrix();
124   glRotatef(theta * RAD_TO_DEG, 0.0, 0.0, 1.0);
125   xx = (int)(x * cos(theta) + y * sin( theta ));
126   yy = (int)(y * cos(theta) - x * sin( theta ));
127         glTranslatef( xx, yy, 0);
128         glScalef(.1, .1, 0.0);
129         while (*msg) {
130                 glutStrokeCharacter(font, *msg);
131                 msg++;
132         }
133         glPopMatrix();
134 }
135
136 //========================= End of Class Implementations===================
137 // fgHUDInit
138 //
139 // Constructs a HUD object and then adds in instruments. At the present
140 // the instruments are hard coded into the routine. Ultimately these need
141 // to be defined by the aircraft's instrumentation records so that the
142 // display for a Piper Cub doesn't show the speed range of a North American
143 // mustange and the engine readouts of a B36!
144 //
145
146 #define INSTRDEFS 21
147
148 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
149 {
150   instr_item *HIptr;
151   int index;
152
153   FG_LOG( FG_COCKPIT, FG_INFO, "Initializing current aircraft HUD" );
154
155   HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());  // empty the HUD deque
156
157 //  hud->code = 1;
158 //  hud->status = 0;
159
160   // For now lets just hardcode the hud here.
161   // In the future, hud information has to come from the same place
162   // aircraft information came from.
163
164 //  fgHUDSetTimeMode( hud, NIGHT );
165 //  fgHUDSetBrightness( hud, BRT_LIGHT );
166
167   index = 0;
168
169   do {
170     switch ( index ) {
171       case 0:     // TBI
172         HIptr = (instr_item *) new fgTBI_instr( 270, 100, 60, 10 );
173         break;
174
175       case 1:     // Artificial Horizon
176         HIptr = (instr_item *) new HudLadder( 240, 195, 120, 180 );
177         break;
178
179       case 2:    // KIAS
180         HIptr = (instr_item *) new hud_card( 130,
181                                              170,
182                                               28,
183                                              200,
184                                              get_speed,
185                                              HUDS_LEFT | HUDS_VERT,
186                                              200.0, 0.0,
187                                              1.0,
188                                              10,  5,
189                                              0,
190                                              0,
191                                              50.0,
192                                              true);
193
194         break;
195
196       case 3:    // Radio Altimeter
197         HIptr = (instr_item *) new hud_card( 420,
198                                              195,
199                                               25,
200                                              150,
201                                              get_agl,
202                                              HUDS_LEFT | HUDS_VERT,
203                                              1000, 0,
204                                              1.0,
205                                              25,    5,
206                                              0,
207                                              0,
208                                              200.0,
209                                              true);
210         break;
211
212       case 4:    // GYRO COMPASS
213         HIptr = (instr_item *) new hud_card( 200,
214                                              375,
215                                              200,
216                                               28,
217                                              get_heading,
218                                              HUDS_TOP,
219                                              360, 0,
220                                                1.0,
221                                                5,   1,
222                                              360,
223                                                0,
224                                               25,
225                                              true);
226         break;
227
228       case 5:    // AMSL
229         HIptr = (instr_item *) new hud_card( 460,
230                                              170,
231                                               35,
232                                              200,
233                                              get_altitude,
234                                              HUDS_RIGHT | HUDS_VERT,
235                                              15000, -500,
236                                              1.0,
237                                              100,  25,
238                                              0,
239                                              0,
240                                              250,
241                                              true);
242         break;
243
244       case 6:
245         HIptr = (instr_item *) new  guage_instr( 250,            // x
246                                                  350,            // y
247                                                  100,            // width
248                                                   20,            // height
249                                                  get_aileronval, // data source
250                                                  HUDS_BOTTOM | HUDS_NOTEXT,
251                                                  100.0,
252                                                  +1.0,
253                                                  -1.0);
254         break;
255
256       case 7:
257         HIptr = (instr_item *) new  guage_instr( 170,             // x
258                                                  225,             // y
259                                                   20,             // width
260                                                  100,             // height
261                                                  get_elevatorval, // data source
262                                                  HUDS_RIGHT | HUDS_VERT | HUDS_NOTEXT,
263                                                 -100.0,           // Scale data
264                                                   +1.0,           // Data Range
265                                                   -1.0);
266         break;
267
268       case 8:
269         HIptr = (instr_item *) new  guage_instr( 250,             // x
270                                                  200,             // y
271                                                  100,             // width
272                                                   20,             // height
273                                                  get_rudderval,   // data source
274                                                  HUDS_TOP | HUDS_NOTEXT,
275                                                  100.0,
276                                                  +1.0,
277                                                  -1.0);
278         break;
279
280       case 9:
281         HIptr = (instr_item *) new  guage_instr( 100,             // x
282                                                  190,
283                                                   20,
284                                                  160,             // height
285                                                  get_throttleval, // data source
286                                                  HUDS_VERT | HUDS_RIGHT | HUDS_NOTEXT,
287                                                  100.0,
288                                                    1.0,
289                                                    0.0);
290         break;
291
292       case 10:    // Digital KIAS
293         HIptr = (instr_item *) new instr_label ( 110,
294                                                  150,
295                                                   40,
296                                                   30,
297                                                  get_speed,
298                                                  "%5.0f",
299                                                  NULL,
300                                                  " Kts",
301                                                  1.0,
302                                                  HUDS_TOP,
303                                                  RIGHT_JUST,
304                                                  SMALL,
305                                                  0,
306                                                  TRUE );
307         break;
308
309       case 11:    // Digital Rate of Climb
310         HIptr = (instr_item *) new instr_label ( 110,
311                                                  135,
312                                                   40,
313                                                   10,
314                                                  get_climb_rate,
315                                                  "%5.0f",
316                                                  " Climb",
317                                                  NULL,
318                                                  1.0,
319                                                  HUDS_TOP,
320                                                  RIGHT_JUST,
321                                                  SMALL,
322                                                  0,
323                                                  TRUE );
324         break;
325
326       case 12:    // Roll indication diagnostic
327         HIptr = (instr_item *) new instr_label ( 110,
328                                                  120,
329                                                   40,
330                                                   10,
331                                                  get_roll,
332                                                  "%5.2f",
333                                                  " Roll",
334                                                  " Deg",
335                                                  1.0,
336                                                  HUDS_TOP,
337                                                  RIGHT_JUST,
338                                                  SMALL,
339                                                  0,
340                                                  TRUE );
341         break;
342
343       case 13:    // Angle of attack diagnostic
344         HIptr = (instr_item *) new instr_label ( 440,
345                                                  150,
346                                                   60,
347                                                   10,
348                                                  get_aoa,
349                                                  "      %5.2f",
350                                                  "AOA",
351                                                  " Deg",
352                                                  1.0,
353                                                  HUDS_TOP,
354                                                  RIGHT_JUST,
355                                                  SMALL,
356                                                  0,
357                                                  TRUE );
358         break;
359
360       case 14:
361         HIptr = (instr_item *) new instr_label ( 440,
362                                                  135,
363                                                   60,
364                                                   10,
365                                                  get_heading,
366                                                  " %5.1f",
367                                                  "Heading ",
368                                                  " Deg",
369                                                  1.0,
370                                                  HUDS_TOP,
371                                                  RIGHT_JUST,
372                                                  SMALL,
373                                                  0,
374                                                  TRUE );
375         break;
376
377       case 15:
378         HIptr = (instr_item *) new instr_label ( 440,
379                                                  120,
380                                                   60,
381                                                   10,
382                                                  get_sideslip,
383                                                  "%5.2f",
384                                                  "Sideslip ",
385                                                  NULL,
386                                                  1.0,
387                                                  HUDS_TOP,
388                                                  RIGHT_JUST,
389                                                  SMALL,
390                                                  0,
391                                                  TRUE );
392         break;
393
394       case 16:
395         HIptr = (instr_item *) new instr_label( 440,
396                                                 100,
397                                                  60,
398                                                  10,
399                                                 get_throttleval,
400                                                 "%5.2f",
401                                                 "Throttle ",
402                                                 NULL,
403                                                  1.0,
404                                                 HUDS_TOP,
405                                                 RIGHT_JUST,
406                                                 SMALL,
407                                                 0,
408                                                 TRUE );
409         break;
410
411       case 17:
412         HIptr = (instr_item *) new instr_label( 440,
413                                                  85,
414                                                  60,
415                                                  10,
416                                                 get_elevatorval,
417                                                 "%5.2f",
418                                                 "Elevator ",
419                                                 NULL,
420                                                  1.0,
421                                                 HUDS_TOP,
422                                                 RIGHT_JUST,
423                                                 SMALL,
424                                                 0,
425                                                 TRUE );
426         break;
427
428       case 18:
429         HIptr = (instr_item *) new instr_label( 440,
430                                                  60,
431                                                  60,
432                                                  10,
433                                                 get_aileronval,
434                                                 "%5.2f",
435                                                 "Aileron  ",
436                                                 NULL,
437                                                  1.0,
438                                                 HUDS_TOP,
439                                                 RIGHT_JUST,
440                                                 SMALL,
441                                                 0,
442                                                 TRUE );
443         break;
444
445
446       case 19:
447         HIptr = (instr_item *) new instr_label( 10,
448                                                 10,
449                                                 60,
450                                                 10,
451                                                  get_frame_rate,
452                                                 "%.0f",
453                                                 "Frame rate = ",
454                                                 NULL,
455                                                  1.0,
456                                                 HUDS_TOP,
457                                                 RIGHT_JUST,
458                                                 SMALL,
459                                                 0,
460                                                 TRUE );
461         break;
462
463       case 20:
464           switch( current_options.get_tris_or_culled() ) {
465           case 0:
466               HIptr = (instr_item *) new instr_label( 10,
467                                                       25,
468                                                       90,
469                                                       10,
470                                                       get_vfc_tris_drawn,
471                                                       "%.0f",
472                                                       "Tris Rendered = ",
473                                                       NULL,
474                                                       1.0,
475                                                       HUDS_TOP,
476                                                       RIGHT_JUST,
477                                                       SMALL,
478                                                       0,
479                                                       TRUE );
480               break;
481           case 1:
482               HIptr = (instr_item *) new instr_label( 10,
483                                                       25,
484                                                       90,
485                                                       10,
486                                                       get_vfc_ratio,
487                                                       "%.2f",
488                                                       "VFC Ratio = ",
489                                                       NULL,
490                                                       1.0,
491                                                       HUDS_TOP,
492                                                       RIGHT_JUST,
493                                                       SMALL,
494                                                       0,
495                                                       TRUE );
496               break;
497           }
498           break;
499
500       case 21:
501         HIptr = (instr_item *) new instr_label( 10,
502                                                 40,
503                                                 90,
504                                                 10,
505                                                 get_fov,
506                                                 "%.1f",
507                                                 "FOV = ",
508                                                 NULL,
509                                                 1.0,
510                                                 HUDS_TOP,
511                                                 RIGHT_JUST,
512                                                 SMALL,
513                                                 0,
514                                                 TRUE );
515         break;
516
517       default:
518         HIptr = 0;;
519       }
520     if( HIptr ) {                   // Anything to install?
521       HUD_deque.insert( HUD_deque.begin(), HIptr);
522       }
523     index++;
524     }
525   while( HIptr );
526
527   return 0;  // For now. Later we may use this for an error code.
528 }
529
530 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
531 {
532     int index;
533
534     FG_LOG( FG_COCKPIT, FG_INFO, "Initializing current aircraft HUD" );
535
536     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
537
538     //  hud->code = 1;
539     //  hud->status = 0;
540
541     // For now lets just hardcode the hud here.
542     // In the future, hud information has to come from the same place
543     // aircraft information came from.
544
545     //  fgHUDSetTimeMode( hud, NIGHT );
546     //  fgHUDSetBrightness( hud, BRT_LIGHT );
547
548     //  index = 0;
549     index = 19;  
550
551     instr_item* p;
552
553     p = new instr_label( 10, 10, 60, 10,
554                          get_frame_rate,
555                          "%.0f",
556                          "Frame rate = ",
557                          NULL,
558                          1.0,
559                          HUDS_TOP,
560                          RIGHT_JUST,
561                          SMALL,
562                          0,
563                          TRUE );
564     HUD_deque.push_front( p );
565
566     if ( current_options.get_tris_or_culled() == 0 )
567         p = new instr_label( 10, 25, 90, 10,
568                              get_vfc_tris_drawn,
569                              "%.0f",
570                              "Tris Rendered = ",
571                              NULL,
572                              1.0,
573                              HUDS_TOP,
574                              RIGHT_JUST,
575                              SMALL,
576                              0,
577                              TRUE );
578     else
579         p = new instr_label( 10, 25, 90, 10,
580                              get_vfc_ratio,
581                              "%.2f",
582                              "VFC Ratio = ",
583                              NULL,
584                              1.0,
585                              HUDS_TOP,
586                              RIGHT_JUST,
587                              SMALL,
588                              0,
589                              TRUE );
590     HUD_deque.push_front( p );
591
592     p = new instr_label( 10, 40, 90, 10,
593                          get_fov,
594                          "%.1f",
595                          "FOV = ",
596                          NULL,
597                          1.0,
598                          HUDS_TOP,
599                          RIGHT_JUST,
600                          SMALL,
601                          0,
602                          TRUE );
603     HUD_deque.push_front( p );
604
605     const int x_pos = 480;
606     p = new instr_label( x_pos, 40, 40, 30,
607                          get_speed,
608                          "%5.0f",
609                          "Airspeed ",
610                          " Kts",
611                          1.0,
612                          HUDS_TOP,
613                          RIGHT_JUST,
614                          SMALL,
615                          0,
616                          TRUE );
617     HUD_deque.push_front( p );
618
619     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
620         strcpy(units, " ft");
621     } else {
622         strcpy(units, " m");
623     }
624     p = new instr_label( x_pos, 25, 40, 10,
625                          get_altitude,
626                          "%5.0f",
627                          "Altitude ",
628                          units,
629                          1.0,
630                          HUDS_TOP,
631                          RIGHT_JUST,
632                          SMALL,
633                          0,
634                          TRUE );
635     HUD_deque.push_front( p );
636
637     p = new instr_label( x_pos, 10, 60, 10,
638                          get_heading,
639                          "%5.1f",
640                          "Heading  ",
641                          " Deg",
642                          1.0,
643                          HUDS_TOP,
644                          RIGHT_JUST,
645                          SMALL,
646                          0,
647                          TRUE );
648     HUD_deque.push_front( p );
649
650     return 0;  // For now. Later we may use this for an error code.
651 }
652
653 int global_day_night_switch = DAY;
654
655 void HUD_brightkey( bool incr_bright )
656 {
657 instr_item *pHUDInstr = HUD_deque[0];
658 int brightness        = pHUDInstr->get_brightness();
659
660   if( current_options.get_hud_status() ) {
661     if( incr_bright ) {
662       switch (brightness) {
663         case BRT_LIGHT:
664               current_options.set_hud_status(0);
665           break;
666
667         case BRT_MEDIUM:
668           brightness = BRT_LIGHT;
669           break;
670
671         case BRT_DARK:
672           brightness = BRT_MEDIUM;
673           break;
674
675         case BRT_BLACK:
676           brightness = BRT_DARK;
677           break;
678
679         default:
680           brightness = BRT_BLACK;
681         }
682       }
683     else {
684       switch (brightness) {
685         case BRT_LIGHT:
686           brightness = BRT_MEDIUM;
687           break;
688
689         case BRT_MEDIUM:
690           brightness = BRT_DARK;
691           break;
692
693         case BRT_DARK:
694           brightness = BRT_BLACK;
695           break;
696
697         case BRT_BLACK:
698         default:
699               current_options.set_hud_status(0);
700         }
701       }
702     }
703   else {
704     current_options.set_hud_status(1);
705     if( incr_bright ) {
706       if( DAY == global_day_night_switch ) {
707         brightness = BRT_BLACK;
708         }
709       else {
710         brightness = BRT_DARK;
711         global_day_night_switch = DAY;
712         }
713       }
714     else {
715       if( NIGHT == global_day_night_switch ) {
716         brightness = BRT_DARK;
717         }
718       else {
719         brightness = BRT_MEDIUM;
720         global_day_night_switch = NIGHT;
721         }
722       }
723     }
724   pHUDInstr->SetBrightness( brightness );
725 }
726
727 // fgUpdateHUD
728 //
729 // Performs a once around the list of calls to instruments installed in
730 // the HUD object with requests for redraw. Kinda. It will when this is
731 // all C++.
732 //
733 void fgUpdateHUD( void ) {
734   int i;
735   int brightness;
736 //  int day_night_sw = current_aircraft.controls->day_night_switch;
737   int day_night_sw = global_day_night_switch;
738   int hud_displays = HUD_deque.size();
739   instr_item *pHUDInstr;
740
741   if( !hud_displays ) {  // Trust everyone, but ALWAYS cut the cards!
742     return;
743     }
744
745   pHUDInstr = HUD_deque[0];
746   brightness = pHUDInstr->get_brightness();
747 //  brightness = HUD_deque.at(0)->get_brightness();
748
749   glMatrixMode(GL_PROJECTION);
750   glPushMatrix();
751
752   glLoadIdentity();
753   gluOrtho2D(0, 640, 0, 480);
754   glMatrixMode(GL_MODELVIEW);
755   glPushMatrix();
756   glLoadIdentity();
757
758   glColor3f(1.0, 1.0, 1.0);
759   glIndexi(7);
760
761   glDisable(GL_DEPTH_TEST);
762   glDisable(GL_LIGHTING);
763
764   glLineWidth(1);
765
766   HudIterator current = HUD_deque.begin();
767   HudIterator last = HUD_deque.end();
768
769   for ( ; current != last; ++current ) {
770     pHUDInstr = *current;
771
772     // for( i = hud_displays; i; --i) { // Draw everything
773     // if( HUD_deque.at(i)->enabled()) {
774     // pHUDInstr = HUD_deque[i - 1];
775     if( pHUDInstr->enabled()) {
776                                    // We should to respond to a dial instead
777                                    // or as well to the of time of day. Of
778                                    // course, we have no dial!
779       if( day_night_sw == DAY) {
780         switch (brightness) {
781           case BRT_LIGHT:
782             glColor3f (0.1, 0.9, 0.1);
783             break;
784
785           case BRT_MEDIUM:
786             glColor3f (0.1, 0.7, 0.0);
787             break;
788
789           case BRT_DARK:
790             glColor3f (0.0, 0.5, 0.0);
791             break;
792
793           case BRT_BLACK:
794             glColor3f( 0.0, 0.0, 0.0);
795             break;
796
797           default:;
798             }
799           }
800         else {
801           if( day_night_sw == NIGHT) {
802             switch (brightness) {
803               case BRT_LIGHT:
804                 glColor3f (0.9, 0.1, 0.1);
805                 break;
806
807               case BRT_MEDIUM:
808                 glColor3f (0.7, 0.0, 0.1);
809                 break;
810
811               case BRT_DARK:
812               default:
813                 glColor3f (0.5, 0.0, 0.0);
814               }
815             }
816           else {     // Just in case default
817             glColor3f (0.1, 0.9, 0.1);
818             }
819           }
820     //  fgPrintf( FG_COCKPIT, FG_DEBUG, "HUD Code %d  Status %d\n",
821     //            hud->code, hud->status );
822       pHUDInstr->draw();
823 //      HUD_deque.at(i)->draw(); // Responsible for broken or fixed variants.
824                               // No broken displays honored just now.
825       }
826     }
827
828   glEnable(GL_DEPTH_TEST);
829   glEnable(GL_LIGHTING);
830   glMatrixMode(GL_PROJECTION);
831   glPopMatrix();
832   glMatrixMode(GL_MODELVIEW);
833   glPopMatrix();
834 }
835