]> git.mxchange.org Git - flightgear.git/blob - Cockpit/hud.cxx
Renamed polar3d.h to polar3d.hxx
[flightgear.git] / Cockpit / hud.cxx
1 /**************************************************************************
2  * hud.cxx -- hud defines and prototypes
3  *
4  * Written by Michele America, started September 1997.
5  *
6  * Copyright (C) 1997  Michele F. America  - micheleamerica@geocities.com
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 HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #ifdef HAVE_WINDOWS_H
32 #  include <windows.h>
33 #endif
34
35 #include <GL/glut.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #ifdef HAVE_VALUES_H
40 #  include <values.h>  // for MAXINT
41 #endif
42
43 #include <Aircraft/aircraft.h>
44 #include <Debug/fg_debug.h>
45 #include <Include/fg_constants.h>
46 #include <Math/fg_random.h>
47 #include <Math/mat3.h>
48 #include <Math/polar3d.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Time/fg_timer.hxx>
51 #include <Weather/weather.h>
52
53 #include "hud.hxx"
54
55 #ifdef __sun__
56 extern "C" {
57   extern void *memmove(void *, const void *, size_t);
58 }
59 #endif
60
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 deque< instr_item * > 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 = x * cos(theta) + y * sin( theta );
126   yy = 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   fgPrintf( FG_COCKPIT, FG_INFO, "Initializing current aircraft HUD\n" );
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( 300, 100, 60, 10 );
173         break;
174
175       case 1:     // Artificial Horizon
176         HIptr = (instr_item *) new HudLadder( 270, 195, 120, 180 );
177         break;
178
179       case 2:    // KIAS
180         HIptr = (instr_item *) new hud_card( 160,
181                                              170,
182                                               35,
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:    // Angle of Attack
197         HIptr = (instr_item *) new hud_card( 450,
198                                              195,
199                                               30,
200                                              150,
201                                              get_aoa,
202                                              HUDS_LEFT | HUDS_VERT,
203                                              50, -40,
204                                              1.0,
205                                              5,    1,
206                                              0,
207                                              1,
208                                              21.0,
209                                              true);
210         break;
211
212       case 4:    // GYRO COMPASS
213         HIptr = (instr_item *) new hud_card( 200,
214                                              375,
215                                              260,
216                                               32,
217                                              get_heading,
218                                              HUDS_TOP,
219                                              360, 0,
220                                              1.0,
221                                              10,   5,
222                                              360,
223                                              0,
224                                              50,
225                                              true);
226         break;
227
228       case 5:    // AMSL
229         HIptr = (instr_item *) new hud_card( 490,
230                                              170,
231                                               35,
232                                              200,
233                                              get_altitude,
234                                              HUDS_RIGHT | HUDS_VERT,
235                                              15000, 0,
236                                              1.0,
237                                              100,  25,
238                                              0,
239                                              0,
240                                              250,
241                                              true);
242         break;
243
244       case 6:    // Digital KIAS
245         HIptr = (instr_item *) new instr_label ( 160,
246                                                  150,
247                                                   40,
248                                                   30,
249                                                  get_speed,
250                                                  "%5.0f",
251                                                  NULL,
252                                                  " Kts",
253                                                  HUDS_TOP,
254                                                  RIGHT_JUST,
255                                                  SMALL,
256                                                  0,
257                                                  TRUE );
258         break;
259
260       case 7:    // Digital Altimeter
261         HIptr = (instr_item *) new instr_label ( 160,
262                                                  135,
263                                                   40,
264                                                   10,
265                                                  get_altitude,
266                                                  "MSL  %5.0f",
267                                                  NULL,
268                                                  " m",
269                                                  HUDS_TOP,
270                                                  LEFT_JUST,
271                                                  SMALL,
272                                                  0,
273                                                  TRUE );
274         break;
275
276       case 8:    // Roll indication diagnostic
277         HIptr = (instr_item *) new instr_label ( 160,
278                                                  120,
279                                                   40,
280                                                   10,
281                                                  get_roll,
282                                                  "%5.2f",
283                                                  " Roll",
284                                                  " Deg",
285                                                  HUDS_TOP,
286                                                  RIGHT_JUST,
287                                                  SMALL,
288                                                  0,
289                                                  TRUE );
290         break;
291
292       case 9:    // Angle of attack diagnostic
293         HIptr = (instr_item *) new instr_label ( 440,
294                                                  150,
295                                                   60,
296                                                   10,
297                                                  get_aoa,
298                                                  "      %5.2f",
299                                                  "AOA",
300                                                  " Deg",
301                                                  HUDS_TOP,
302                                                  RIGHT_JUST,
303                                                  SMALL,
304                                                  0,
305                                                  TRUE );
306         break;
307
308       case 10:
309         HIptr = (instr_item *) new instr_label ( 440,
310                                                  135,
311                                                   60,
312                                                   10,
313                                                  get_heading,
314                                                  " %5.0f",
315                                                  "Heading ",
316                                                  " Deg",
317                                                  HUDS_TOP,
318                                                  RIGHT_JUST,
319                                                  SMALL,
320                                                  0,
321                                                  TRUE );
322         break;
323
324       case 11:
325         HIptr = (instr_item *) new instr_label ( 440,
326                                                  120,
327                                                   60,
328                                                   10,
329                                                  get_sideslip,
330                                                  "%5.2f",
331                                                  "Sideslip ",
332                                                  NULL,
333                                                  HUDS_TOP,
334                                                  RIGHT_JUST,
335                                                  SMALL,
336                                                  0,
337                                                  TRUE );
338         break;
339
340       case 12:
341         HIptr = (instr_item *) new instr_label( 440,
342                                                 100,
343                                                  60,
344                                                  10,
345                                                 get_throttleval,
346                                                 "%5.2f",
347                                                 "Throttle ",
348                                                 NULL,
349                                                 HUDS_TOP,
350                                                 RIGHT_JUST,
351                                                 SMALL,
352                                                 0,
353                                                 TRUE );
354         break;
355
356       case 13:
357         HIptr = (instr_item *) new instr_label( 440,
358                                                  85,
359                                                  60,
360                                                  10,
361                                                 get_elevatorval,
362                                                 "%5.2f",
363                                                 "Elevator ",
364                                                 NULL,
365                                                 HUDS_TOP,
366                                                 RIGHT_JUST,
367                                                 SMALL,
368                                                 0,
369                                                 TRUE );
370         break;
371
372       case 14:
373         HIptr = (instr_item *) new instr_label( 440,
374                                                  60,
375                                                  60,
376                                                  10,
377                                                 get_aileronval,
378                                                 "%5.2f",
379                                                 "Aileron  ",
380                                                 NULL,
381                                                 HUDS_TOP,
382                                                 RIGHT_JUST,
383                                                 SMALL,
384                                                 0,
385                                                 TRUE );
386         break;
387
388
389       case 15:
390         HIptr = (instr_item *) new instr_label( 10,
391                                                 10,
392                                                 60,
393                                                 10,
394                                                  get_frame_rate,
395                                                 "%.1f",
396                                                 "Frame rate = ",
397                                                 NULL,
398                                                 HUDS_TOP,
399                                                 RIGHT_JUST,
400                                                 SMALL,
401                                                 0,
402                                                 TRUE );
403         break;
404
405       case 16:
406         HIptr = (instr_item *) new instr_label( 10,
407                                                 25,
408                                                 90,
409                                                 10,
410                                                  get_vfc_ratio,
411                                                 "%.2f",
412                                                 "VFC Ratio = ",
413                                                 NULL,
414                                                 HUDS_TOP,
415                                                 RIGHT_JUST,
416                                                 SMALL,
417                                                 0,
418                                                 TRUE );
419         break;
420
421       case 17:
422         HIptr = (instr_item *) new instr_label( 10,
423                                                 40,
424                                                 90,
425                                                 10,
426                                                 get_fov,
427                                                 "%.1f",
428                                                 "FOV = ",
429                                                 NULL,
430                                                 HUDS_TOP,
431                                                 RIGHT_JUST,
432                                                 SMALL,
433                                                 0,
434                                                 TRUE );
435         break;
436       case 18:
437         HIptr = (instr_item *) new  guage_instr(  50,            // x
438                                                  200,            // y
439                                                  100,            // width
440                                                   20,            // height
441                                                  get_aileronval, // data source
442                                                  HUDS_BOTTOM,
443                                                  100.0,
444                                                  +1.0,
445                                                  -1.0);
446         break;
447
448       case 19:
449         HIptr = (instr_item *) new  guage_instr(  90,             // x
450                                                  225,             // y
451                                                   20,             // width
452                                                  100,             // height
453                                                  get_elevatorval, // data source
454                                                  HUDS_BOTH | HUDS_VERT,
455                                                  100.0,           // Scale data
456                                                   +1.0,           // Data Range
457                                                   -1.0);
458         break;
459
460       case 20:
461         HIptr = (instr_item *) new  guage_instr(  50,             // x
462                                                  350,             // y
463                                                  100,             // width
464                                                   20,             // height
465                                                  get_rudderval,   // data source
466                                                  HUDS_TOP,
467                                                  100.0,
468                                                  +1.0,
469                                                  -1.0);
470         break;
471
472       default:
473         HIptr = 0;;
474       }
475     if( HIptr ) {                   // Anything to install?
476       HUD_deque.insert( HUD_deque.begin(), HIptr);
477       }
478     index++;
479     }
480   while( HIptr );
481
482   return 0;  // For now. Later we may use this for an error code.
483 }
484
485
486 // fgUpdateHUD
487 //
488 // Performs a once around the list of calls to instruments installed in
489 // the HUD object with requests for redraw. Kinda. It will when this is
490 // all C++.
491 //
492 int global_day_night_switch = DAY;
493
494 void fgUpdateHUD( void ) {
495   int i;
496   int brightness;
497 //  int day_night_sw = current_aircraft.controls->day_night_switch;
498   int day_night_sw = global_day_night_switch;
499   int hud_displays = HUD_deque.size();
500   instr_item *pHUDInstr;
501
502   if( !hud_displays ) {  // Trust everyone, but ALWAYS cut the cards!
503     return;
504     }
505
506   pHUDInstr = HUD_deque[0];
507   brightness = pHUDInstr->get_brightness();
508 //  brightness = HUD_deque.at(0)->get_brightness();
509
510   glMatrixMode(GL_PROJECTION);
511   glPushMatrix();
512
513   glLoadIdentity();
514   gluOrtho2D(0, 640, 0, 480);
515   glMatrixMode(GL_MODELVIEW);
516   glPushMatrix();
517   glLoadIdentity();
518
519   glColor3f(1.0, 1.0, 1.0);
520   glIndexi(7);
521
522   glDisable(GL_DEPTH_TEST);
523   glDisable(GL_LIGHTING);
524
525   glLineWidth(1);
526
527   for( i = hud_displays; i; --i) { // Draw everything
528 //    if( HUD_deque.at(i)->enabled()) {
529     pHUDInstr = HUD_deque[i - 1];
530     if( pHUDInstr->enabled()) {
531                                    // We should to respond to a dial instead
532                                    // or as well to the of time of day. Of
533                                    // course, we have no dial!
534       if( day_night_sw == DAY) {
535         switch (brightness) {
536           case BRT_LIGHT:
537             glColor3f (0.1, 0.9, 0.1);
538             break;
539
540           case BRT_MEDIUM:
541             glColor3f (0.1, 0.7, 0.0);
542             break;
543
544           case BRT_DARK:
545             glColor3f (0.0, 0.5, 0.0);
546             }
547           }
548         else {
549           if( day_night_sw == NIGHT) {
550             switch (brightness) {
551               case BRT_LIGHT:
552                 glColor3f (0.9, 0.1, 0.1);
553                 break;
554
555               case BRT_MEDIUM:
556                 glColor3f (0.7, 0.0, 0.1);
557                 break;
558
559               case BRT_DARK:
560               default:
561                 glColor3f (0.5, 0.0, 0.0);
562               }
563             }
564           else {     // Just in case default
565             glColor3f (0.1, 0.9, 0.1);
566             }
567           }
568     //  fgPrintf( FG_COCKPIT, FG_DEBUG, "HUD Code %d  Status %d\n",
569     //            hud->code, hud->status );
570       pHUDInstr->draw();
571 //      HUD_deque.at(i)->draw(); // Responsible for broken or fixed variants.
572                               // No broken displays honored just now.
573       }
574     }
575
576   glEnable(GL_DEPTH_TEST);
577   glEnable(GL_LIGHTING);
578   glMatrixMode(GL_PROJECTION);
579   glPopMatrix();
580   glMatrixMode(GL_MODELVIEW);
581   glPopMatrix();
582 }
583
584 /* $Log$
585 /* Revision 1.15  1998/07/08 14:41:08  curt
586 /* Renamed polar3d.h to polar3d.hxx
587 /*
588  * Revision 1.14  1998/07/06 21:31:20  curt
589  * Removed an extraneous ^M.
590  *
591  * Revision 1.13  1998/07/03 13:16:28  curt
592  * Added Charlie Hotchkiss's HUD updates and improvementes.
593  *
594  * Revision 1.11  1998/06/05 18:17:10  curt
595  * Added the declaration of memmove needed by the stl which apparently
596  * solaris only defines for cc compilations and not for c++ (__STDC__)
597  *
598  * Revision 1.10  1998/05/17 16:58:12  curt
599  * Added a View Frustum Culling ratio display to the hud.
600  *
601  * Revision 1.9  1998/05/16 13:04:14  curt
602  * New updates from Charlie Hotchkiss.
603  *
604  * Revision 1.8  1998/05/13 18:27:54  curt
605  * Added an fov to hud display.
606  *
607  * Revision 1.7  1998/05/11 18:13:11  curt
608  * Complete C++ rewrite of all cockpit code by Charlie Hotchkiss.
609  *
610  * Revision 1.22  1998/04/18 04:14:02  curt
611  * Moved fg_debug.c to it's own library.
612  *
613  * Revision 1.21  1998/04/03 21:55:28  curt
614  * Converting to Gnu autoconf system.
615  * Tweaks to hud.c
616  *
617  * Revision 1.20  1998/03/09 22:48:40  curt
618  * Minor "formatting" tweaks.
619  *
620  * Revision 1.19  1998/02/23 20:18:28  curt
621  * Incorporated Michele America's hud changes.
622  *
623  * Revision 1.18  1998/02/21 14:53:10  curt
624  * Added Charlie's HUD changes.
625  *
626  * Revision 1.17  1998/02/20 00:16:21  curt
627  * Thursday's tweaks.
628  *
629  * Revision 1.16  1998/02/19 13:05:49  curt
630  * Incorporated some HUD tweaks from Michelle America.
631  * Tweaked the sky's sunset/rise colors.
632  * Other misc. tweaks.
633  *
634  * Revision 1.15  1998/02/16 13:38:39  curt
635  * Integrated changes from Charlie Hotchkiss.
636  *
637  * Revision 1.14  1998/02/12 21:59:41  curt
638  * Incorporated code changes contributed by Charlie Hotchkiss
639  * <chotchkiss@namg.us.anritsu.com>
640  *
641  * Revision 1.12  1998/02/09 15:07:48  curt
642  * Minor tweaks.
643  *
644  * Revision 1.11  1998/02/07 15:29:34  curt
645  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
646  * <chotchkiss@namg.us.anritsu.com>
647  *
648  * Revision 1.10  1998/02/03 23:20:14  curt
649  * Lots of little tweaks to fix various consistency problems discovered by
650  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
651  * passed arguments along to the real printf().  Also incorporated HUD changes
652  * by Michele America.
653  *
654  * Revision 1.9  1998/01/31 00:43:04  curt
655  * Added MetroWorks patches from Carmen Volpe.
656  *
657  * Revision 1.8  1998/01/27 00:47:51  curt
658  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
659  * system and commandline/config file processing code.
660  *
661  * Revision 1.7  1998/01/19 18:40:20  curt
662  * Tons of little changes to clean up the code and to remove fatal errors
663  * when building with the c++ compiler.
664  *
665  * Revision 1.6  1997/12/15 23:54:34  curt
666  * Add xgl wrappers for debugging.
667  * Generate terrain normals on the fly.
668  *
669  * Revision 1.5  1997/12/10 22:37:39  curt
670  * Prepended "fg" on the name of all global structures that didn't have it yet.
671  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
672  *
673  * Revision 1.4  1997/09/23 00:29:32  curt
674  * Tweaks to get things to compile with gcc-win32.
675  *
676  * Revision 1.3  1997/09/05 14:17:26  curt
677  * More tweaking with stars.
678  *
679  * Revision 1.2  1997/09/04 02:17:30  curt
680  * Shufflin' stuff.
681  *
682  * Revision 1.1  1997/08/29 18:03:22  curt
683  * Initial revision.
684  *
685  */