]> git.mxchange.org Git - flightgear.git/blob - Cockpit/hud.cxx
Added Charlie Hotchkiss's HUD updates and improvementes.
[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.h>
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 //using namespace std;
68 \r
69 \rdeque< instr_item * > HUD_deque;
70
71 class locRECT {
72   public:
73     RECT rect;
74
75     locRECT( UINT left, UINT top, UINT right, UINT bottom);
76     RECT get_rect(void) { return rect;}
77 };
78
79 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
80 {
81   rect.left   =  left;
82   rect.top    =  top;
83   rect.right  =  right;
84   rect.bottom =  bottom;
85
86 }
87 // #define DEBUG
88
89 void drawOneLine( UINT x1, UINT y1, UINT x2, UINT y2)
90 {
91   glBegin(GL_LINES);
92   glVertex2f(x1, y1);
93   glVertex2f(x2, y2);
94   glEnd();
95 }
96
97 void drawOneLine( RECT &rect)
98 {
99   glBegin(GL_LINES);
100   glVertex2f(rect.left, rect.top);
101   glVertex2f(rect.right, rect.bottom);
102   glEnd();
103 }
104
105 //
106 // The following code deals with painting the "instrument" on the display
107 //
108    /* textString - Bitmap font string */
109
110 void textString( int x, int y, char *msg, void *font ){
111         glRasterPos2f(x, y);
112         while (*msg) {
113                 glutBitmapCharacter(font, *msg);
114                 msg++;
115     }
116 }
117
118 /* strokeString - Stroke font string */
119
120 void strokeString(int x, int y, char *msg, void *font, float theta)
121 {
122 int xx;
123 int yy;
124
125         glPushMatrix();
126   glRotatef(theta * RAD_TO_DEG, 0.0, 0.0, 1.0);
127   xx = x * cos(theta) + y * sin( theta );
128   yy = y * cos(theta) - x * sin( theta );
129         glTranslatef( xx, yy, 0);
130         glScalef(.1, .1, 0.0);
131         while (*msg) {
132                 glutStrokeCharacter(font, *msg);
133                 msg++;
134         }
135         glPopMatrix();
136 }
137
138 //========================= End of Class Implementations===================
139 // fgHUDInit
140 //
141 // Constructs a HUD object and then adds in instruments. At the present
142 // the instruments are hard coded into the routine. Ultimately these need
143 // to be defined by the aircraft's instrumentation records so that the
144 // display for a Piper Cub doesn't show the speed range of a North American
145 // mustange and the engine readouts of a B36!
146 //
147
148 #define INSTRDEFS 21
149
150 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
151 {
152   instr_item *HIptr;
153   int index;
154
155   fgPrintf( FG_COCKPIT, FG_INFO, "Initializing current aircraft HUD\n" );
156
157   HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());  // empty the HUD deque
158
159 //  hud->code = 1;
160 //  hud->status = 0;
161
162   // For now lets just hardcode the hud here.
163   // In the future, hud information has to come from the same place
164   // aircraft information came from.
165
166 //  fgHUDSetTimeMode( hud, NIGHT );
167 //  fgHUDSetBrightness( hud, BRT_LIGHT );
168
169   index = 0;
170
171   do {
172     switch ( index ) {
173       case 0:     // TBI
174         HIptr = (instr_item *) new fgTBI_instr( 300, 100, 60, 10 );
175         break;
176
177       case 1:     // Artificial Horizon
178         HIptr = (instr_item *) new HudLadder( 270, 195, 120, 180 );
179         break;
180
181       case 2:    // KIAS
182         HIptr = (instr_item *) new hud_card( 160,
183                                              170,
184                                               35,
185                                              200,
186                                              get_speed,
187                                              HUDS_LEFT | HUDS_VERT,
188                                              200.0, 0.0,
189                                              1.0,
190                                              10,  5,
191                                              0,
192                                              0,
193                                              50.0,
194                                              true);
195
196         break;
197
198       case 3:    // Angle of Attack
199         HIptr = (instr_item *) new hud_card( 450,
200                                              195,
201                                               30,
202                                              150,
203                                              get_aoa,
204                                              HUDS_LEFT | HUDS_VERT,
205                                              50, -40,
206                                              1.0,
207                                              5,    1,
208                                              0,
209                                              1,
210                                              21.0,
211                                              true);
212         break;
213
214       case 4:    // GYRO COMPASS
215         HIptr = (instr_item *) new hud_card( 200,
216                                              375,
217                                              260,
218                                               32,
219                                              get_heading,
220                                              HUDS_TOP,
221                                              360, 0,
222                                              1.0,
223                                              10,   5,
224                                              360,
225                                              0,
226                                              50,
227                                              true);
228         break;
229
230       case 5:    // AMSL
231         HIptr = (instr_item *) new hud_card( 490,
232                                              170,
233                                               35,
234                                              200,
235                                              get_altitude,
236                                              HUDS_RIGHT | HUDS_VERT,
237                                              15000, 0,
238                                              1.0,
239                                              100,  25,
240                                              0,
241                                              0,
242                                              250,
243                                              true);
244         break;
245
246       case 6:    // Digital KIAS
247         HIptr = (instr_item *) new instr_label ( 160,
248                                                  150,
249                                                   40,
250                                                   30,
251                                                  get_speed,
252                                                  "%5.0f",
253                                                  NULL,
254                                                  " Kts",
255                                                  HUDS_TOP,
256                                                  RIGHT_JUST,
257                                                  SMALL,
258                                                  0,
259                                                  TRUE );
260         break;
261
262       case 7:    // Digital Altimeter
263         HIptr = (instr_item *) new instr_label ( 160,
264                                                  135,
265                                                   40,
266                                                   10,
267                                                  get_altitude,
268                                                  "MSL  %5.0f",
269                                                  NULL,
270                                                  " m",
271                                                  HUDS_TOP,
272                                                  LEFT_JUST,
273                                                  SMALL,
274                                                  0,
275                                                  TRUE );
276         break;
277
278       case 8:    // Roll indication diagnostic
279         HIptr = (instr_item *) new instr_label ( 160,
280                                                  120,
281                                                   40,
282                                                   10,
283                                                  get_roll,
284                                                  "%5.2f",
285                                                  " Roll",
286                                                  " Deg",
287                                                  HUDS_TOP,
288                                                  RIGHT_JUST,
289                                                  SMALL,
290                                                  0,
291                                                  TRUE );
292         break;
293
294       case 9:    // Angle of attack diagnostic
295         HIptr = (instr_item *) new instr_label ( 440,
296                                                  150,
297                                                   60,
298                                                   10,
299                                                  get_aoa,
300                                                  "      %5.2f",
301                                                  "AOA",
302                                                  " Deg",
303                                                  HUDS_TOP,
304                                                  RIGHT_JUST,
305                                                  SMALL,
306                                                  0,
307                                                  TRUE );
308         break;
309
310       case 10:
311         HIptr = (instr_item *) new instr_label ( 440,
312                                                  135,
313                                                   60,
314                                                   10,
315                                                  get_heading,
316                                                  " %5.0f",
317                                                  "Heading ",
318                                                  " Deg",
319                                                  HUDS_TOP,
320                                                  RIGHT_JUST,
321                                                  SMALL,
322                                                  0,
323                                                  TRUE );
324         break;
325
326       case 11:
327         HIptr = (instr_item *) new instr_label ( 440,
328                                                  120,
329                                                   60,
330                                                   10,
331                                                  get_sideslip,
332                                                  "%5.2f",
333                                                  "Sideslip ",
334                                                  NULL,
335                                                  HUDS_TOP,
336                                                  RIGHT_JUST,
337                                                  SMALL,
338                                                  0,
339                                                  TRUE );
340         break;
341
342       case 12:
343         HIptr = (instr_item *) new instr_label( 440,
344                                                 100,
345                                                  60,
346                                                  10,
347                                                 get_throttleval,
348                                                 "%5.2f",
349                                                 "Throttle ",
350                                                 NULL,
351                                                 HUDS_TOP,
352                                                 RIGHT_JUST,
353                                                 SMALL,
354                                                 0,
355                                                 TRUE );
356         break;
357
358       case 13:
359         HIptr = (instr_item *) new instr_label( 440,
360                                                  85,
361                                                  60,
362                                                  10,
363                                                 get_elevatorval,
364                                                 "%5.2f",
365                                                 "Elevator ",
366                                                 NULL,
367                                                 HUDS_TOP,
368                                                 RIGHT_JUST,
369                                                 SMALL,
370                                                 0,
371                                                 TRUE );
372         break;
373
374       case 14:
375         HIptr = (instr_item *) new instr_label( 440,
376                                                  60,
377                                                  60,
378                                                  10,
379                                                 get_aileronval,
380                                                 "%5.2f",
381                                                 "Aileron  ",
382                                                 NULL,
383                                                 HUDS_TOP,
384                                                 RIGHT_JUST,
385                                                 SMALL,
386                                                 0,
387                                                 TRUE );
388         break;
389
390
391       case 15:
392         HIptr = (instr_item *) new instr_label( 10,
393                                                 10,
394                                                 60,
395                                                 10,
396                                                  get_frame_rate,
397                                                 "%.1f",
398                                                 "Frame rate = ",
399                                                 NULL,
400                                                 HUDS_TOP,
401                                                 RIGHT_JUST,
402                                                 SMALL,
403                                                 0,
404                                                 TRUE );
405         break;
406
407       case 16:
408         HIptr = (instr_item *) new instr_label( 10,
409                                                 25,
410                                                 90,
411                                                 10,
412                                                  get_vfc_ratio,
413                                                 "%.2f",
414                                                 "VFC Ratio = ",
415                                                 NULL,
416                                                 HUDS_TOP,
417                                                 RIGHT_JUST,
418                                                 SMALL,
419                                                 0,
420                                                 TRUE );
421         break;
422
423       case 17:
424         HIptr = (instr_item *) new instr_label( 10,
425                                                 40,
426                                                 90,
427                                                 10,
428                                                 get_fov,
429                                                 "%.1f",
430                                                 "FOV = ",
431                                                 NULL,
432                                                 HUDS_TOP,
433                                                 RIGHT_JUST,
434                                                 SMALL,
435                                                 0,
436                                                 TRUE );
437         break;
438       case 18:
439         HIptr = (instr_item *) new  guage_instr(  50,            // x
440                                                  200,            // y
441                                                  100,            // width
442                                                   20,            // height
443                                                  get_aileronval, // data source
444                                                  HUDS_BOTTOM,
445                                                  100.0,
446                                                  +1.0,
447                                                  -1.0);
448         break;
449
450       case 19:
451         HIptr = (instr_item *) new  guage_instr(  90,             // x
452                                                  225,             // y
453                                                   20,             // width
454                                                  100,             // height
455                                                  get_elevatorval, // data source
456                                                  HUDS_BOTH | HUDS_VERT,
457                                                  100.0,           // Scale data
458                                                   +1.0,           // Data Range
459                                                   -1.0);
460         break;
461
462       case 20:
463         HIptr = (instr_item *) new  guage_instr(  50,             // x
464                                                  350,             // y
465                                                  100,             // width
466                                                   20,             // height
467                                                  get_rudderval,   // data source
468                                                  HUDS_TOP,
469                                                  100.0,
470                                                  +1.0,
471                                                  -1.0);
472         break;
473
474       default:
475         HIptr = 0;;
476       }
477     if( HIptr ) {                   // Anything to install?
478       HUD_deque.insert( HUD_deque.begin(), HIptr);
479       }
480     index++;
481     }
482   while( HIptr );
483
484   return 0;  // For now. Later we may use this for an error code.
485 }
486
487
488 // fgUpdateHUD
489 //
490 // Performs a once around the list of calls to instruments installed in
491 // the HUD object with requests for redraw. Kinda. It will when this is
492 // all C++.
493 //
494 int global_day_night_switch = DAY;
495
496 void fgUpdateHUD( void ) {
497   int i;
498   int brightness;
499 //  int day_night_sw = current_aircraft.controls->day_night_switch;
500   int day_night_sw = global_day_night_switch;
501   int hud_displays = HUD_deque.size();
502   instr_item *pHUDInstr;
503
504   if( !hud_displays ) {  // Trust everyone, but ALWAYS cut the cards!
505     return;
506     }
507
508   pHUDInstr = HUD_deque[0];
509   brightness = pHUDInstr->get_brightness();
510 //  brightness = HUD_deque.at(0)->get_brightness();
511
512   glMatrixMode(GL_PROJECTION);
513   glPushMatrix();
514
515   glLoadIdentity();
516   gluOrtho2D(0, 640, 0, 480);
517   glMatrixMode(GL_MODELVIEW);
518   glPushMatrix();
519   glLoadIdentity();
520
521   glColor3f(1.0, 1.0, 1.0);
522   glIndexi(7);
523
524   glDisable(GL_DEPTH_TEST);
525   glDisable(GL_LIGHTING);
526
527   glLineWidth(1);
528
529   for( i = hud_displays; i; --i) { // Draw everything
530 //    if( HUD_deque.at(i)->enabled()) {
531     pHUDInstr = HUD_deque[i - 1];
532     if( pHUDInstr->enabled()) {
533                                    // We should to respond to a dial instead
534                                    // or as well to the of time of day. Of
535                                    // course, we have no dial!
536       if( day_night_sw == DAY) {
537         switch (brightness) {
538           case BRT_LIGHT:
539             glColor3f (0.1, 0.9, 0.1);
540             break;
541
542           case BRT_MEDIUM:
543             glColor3f (0.1, 0.7, 0.0);
544             break;
545
546           case BRT_DARK:
547             glColor3f (0.0, 0.5, 0.0);
548             }
549           }
550         else {
551           if( day_night_sw == NIGHT) {
552             switch (brightness) {
553               case BRT_LIGHT:
554                 glColor3f (0.9, 0.1, 0.1);
555                 break;
556
557               case BRT_MEDIUM:
558                 glColor3f (0.7, 0.0, 0.1);
559                 break;
560
561               case BRT_DARK:
562               default:
563                 glColor3f (0.5, 0.0, 0.0);
564               }
565             }
566           else {     // Just in case default
567             glColor3f (0.1, 0.9, 0.1);
568             }
569           }
570     //  fgPrintf( FG_COCKPIT, FG_DEBUG, "HUD Code %d  Status %d\n",
571     //            hud->code, hud->status );
572       pHUDInstr->draw();
573 //      HUD_deque.at(i)->draw(); // Responsible for broken or fixed variants.
574                               // No broken displays honored just now.
575       }
576     }
577
578   glEnable(GL_DEPTH_TEST);
579   glEnable(GL_LIGHTING);
580   glMatrixMode(GL_PROJECTION);
581   glPopMatrix();
582   glMatrixMode(GL_MODELVIEW);
583   glPopMatrix();
584 }
585
586 /* $Log$
587 /* Revision 1.13  1998/07/03 13:16:28  curt
588 /* Added Charlie Hotchkiss's HUD updates and improvementes.
589 /*
590  * Revision 1.11  1998/06/05 18:17:10  curt
591  * Added the declaration of memmove needed by the stl which apparently
592  * solaris only defines for cc compilations and not for c++ (__STDC__)
593  *
594  * Revision 1.10  1998/05/17 16:58:12  curt
595  * Added a View Frustum Culling ratio display to the hud.
596  *
597  * Revision 1.9  1998/05/16 13:04:14  curt
598  * New updates from Charlie Hotchkiss.
599  *
600  * Revision 1.8  1998/05/13 18:27:54  curt
601  * Added an fov to hud display.
602  *
603  * Revision 1.7  1998/05/11 18:13:11  curt
604  * Complete C++ rewrite of all cockpit code by Charlie Hotchkiss.
605  *
606  * Revision 1.22  1998/04/18 04:14:02  curt
607  * Moved fg_debug.c to it's own library.
608  *
609  * Revision 1.21  1998/04/03 21:55:28  curt
610  * Converting to Gnu autoconf system.
611  * Tweaks to hud.c
612  *
613  * Revision 1.20  1998/03/09 22:48:40  curt
614  * Minor "formatting" tweaks.
615  *
616  * Revision 1.19  1998/02/23 20:18:28  curt
617  * Incorporated Michele America's hud changes.
618  *
619  * Revision 1.18  1998/02/21 14:53:10  curt
620  * Added Charlie's HUD changes.
621  *
622  * Revision 1.17  1998/02/20 00:16:21  curt
623  * Thursday's tweaks.
624  *
625  * Revision 1.16  1998/02/19 13:05:49  curt
626  * Incorporated some HUD tweaks from Michelle America.
627  * Tweaked the sky's sunset/rise colors.
628  * Other misc. tweaks.
629  *
630  * Revision 1.15  1998/02/16 13:38:39  curt
631  * Integrated changes from Charlie Hotchkiss.
632  *
633  * Revision 1.14  1998/02/12 21:59:41  curt
634  * Incorporated code changes contributed by Charlie Hotchkiss
635  * <chotchkiss@namg.us.anritsu.com>
636  *
637  * Revision 1.12  1998/02/09 15:07:48  curt
638  * Minor tweaks.
639  *
640  * Revision 1.11  1998/02/07 15:29:34  curt
641  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
642  * <chotchkiss@namg.us.anritsu.com>
643  *
644  * Revision 1.10  1998/02/03 23:20:14  curt
645  * Lots of little tweaks to fix various consistency problems discovered by
646  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
647  * passed arguments along to the real printf().  Also incorporated HUD changes
648  * by Michele America.
649  *
650  * Revision 1.9  1998/01/31 00:43:04  curt
651  * Added MetroWorks patches from Carmen Volpe.
652  *
653  * Revision 1.8  1998/01/27 00:47:51  curt
654  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
655  * system and commandline/config file processing code.
656  *
657  * Revision 1.7  1998/01/19 18:40:20  curt
658  * Tons of little changes to clean up the code and to remove fatal errors
659  * when building with the c++ compiler.
660  *
661  * Revision 1.6  1997/12/15 23:54:34  curt
662  * Add xgl wrappers for debugging.
663  * Generate terrain normals on the fly.
664  *
665  * Revision 1.5  1997/12/10 22:37:39  curt
666  * Prepended "fg" on the name of all global structures that didn't have it yet.
667  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
668  *
669  * Revision 1.4  1997/09/23 00:29:32  curt
670  * Tweaks to get things to compile with gcc-win32.
671  *
672  * Revision 1.3  1997/09/05 14:17:26  curt
673  * More tweaking with stars.
674  *
675  * Revision 1.2  1997/09/04 02:17:30  curt
676  * Shufflin' stuff.
677  *
678  * Revision 1.1  1997/08/29 18:03:22  curt
679  * Initial revision.
680  *
681  */