1 // hud.cxx -- hud defines and prototypes
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America - micheleamerica@geocities.com
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.
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.
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.
33 # define exception c_exception
42 # include <values.h> // for MAXINT
45 #include <Aircraft/aircraft.hxx>
46 #include <Debug/logstream.hxx>
48 #include <Include/fg_constants.h>
49 #include <Main/options.hxx>
50 #include <Math/fg_random.h>
51 #include <Math/mat3.h>
52 #include <Math/polar3d.hxx>
53 #include <Network/network.h>
54 #include <Scenery/scenery.hxx>
55 #include <Time/fg_timer.hxx>
57 #if defined ( __sun__ ) || defined ( __sgi )
59 extern void *memmove(void *, const void *, size_t);
67 // The following routines obtain information concerning the aircraft's
68 // current state and return it to calling instrument display routines.
69 // They should eventually be member functions of the aircraft.
72 deque< instr_item * > HUD_deque;
74 fgTextList HUD_TextList;
75 fgLineList HUD_LineList;
76 fgLineList HUD_StippleLineList;
82 locRECT( UINT left, UINT top, UINT right, UINT bottom);
83 RECT get_rect(void) { return rect;}
86 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
96 void drawOneLine( UINT x1, UINT y1, UINT x2, UINT y2)
104 void drawOneLine( RECT &rect)
107 glVertex2f(rect.left, rect.top);
108 glVertex2f(rect.right, rect.bottom);
113 // The following code deals with painting the "instrument" on the display
115 /* textString - Bitmap font string */
117 void textString( int x, int y, char *msg, void *font ){
121 // puDrawString ( NULL, msg, x, y );
125 glutBitmapCharacter(font, *msg);
132 /* strokeString - Stroke font string */
133 void strokeString(int x, int y, char *msg, void *font, float theta)
138 float sintheta,costheta;
144 glRotatef(theta * RAD_TO_DEG, 0.0, 0.0, 1.0);
145 sintheta = sin(theta);
146 costheta = cos(theta);
147 xx = (int)(x * costheta + y * sintheta);
148 yy = (int)(y * costheta - x * sintheta);
149 glTranslatef( xx, yy, 0);
150 glScalef(.1, .1, 0.0);
151 while( (c=*msg++) ) {
152 glutStrokeCharacter(font, c);
158 int getStringWidth ( char *str )
160 if ( HUDtext && str )
163 guiFntHandle->getBBox ( str, HUD_TextSize, 0, &l, &r, NULL, NULL ) ;
164 return FloatToInt( r - l );
169 //========================= End of Class Implementations===================
172 // Constructs a HUD object and then adds in instruments. At the present
173 // the instruments are hard coded into the routine. Ultimately these need
174 // to be defined by the aircraft's instrumentation records so that the
175 // display for a Piper Cub doesn't show the speed range of a North American
176 // mustange and the engine readouts of a B36!
181 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
188 int min_x = 25; //off/2;
189 int max_x = 615; //640-(off/2);
191 int max_y = 430; //480-off;
194 unsigned int text_h = 10;
195 unsigned int ladr_w2 = 60;
201 font_size = (current_options.get_xsize() > 1000) ? LARGE : SMALL;
205 FG_LOG( FG_COCKPIT, FG_INFO, "Initializing current aircraft HUD" );
207 // deque < instr_item * > :: iterator first = HUD_deque.begin();
208 // deque < instr_item * > :: iterator last = HUD_deque.end();
209 // HUD_deque.erase( first, last); // empty the HUD deque
211 HUD_deque.erase( HUD_deque.begin(), HUD_deque.end()); // empty the HUD deque
216 // For now lets just hardcode the hud here.
217 // In the future, hud information has to come from the same place
218 // aircraft information came from.
220 // fgHUDSetTimeMode( hud, NIGHT );
221 // fgHUDSetBrightness( hud, BRT_LIGHT );
224 // int x = 290; /*cen_x-30*/
225 // int y = 45; /*off-5*/
226 // HIptr = (instr_item *) new fgTBI_instr( x, y, ladr_w2, text_h );
227 HIptr = (instr_item *) new fgTBI_instr( 290, 45, 60, 10 );
228 HUD_deque.insert( HUD_deque.begin(), HIptr);
230 // case 1: // Artificial Horizon
231 HIptr = (instr_item *) new HudLadder( cen_x-ladr_w2, cen_y-ladr_h2,
232 2*ladr_w2, 2*ladr_h2 );
233 HUD_deque.insert( HUD_deque.begin(), HIptr);
235 // case 4: // GYRO COMPASS
236 HIptr = (instr_item *) new hud_card( cen_x-(compass_w/2),
249 HUD_deque.insert( HUD_deque.begin(), HIptr);
252 HIptr = (instr_item *) new hud_card( max_x - 35 -15, // 15 to balance speed card
257 // HUDS_RIGHT | HUDS_VERT,
258 HUDS_LEFT | HUDS_VERT,
266 HUD_deque.insert( HUD_deque.begin(), HIptr);
269 HIptr = (instr_item *) new guage_instr( cen_x-50, // x
270 cen_y + ladr_h2 -20, // y
273 get_aileronval, // data source
274 HUDS_BOTTOM | HUDS_NOTEXT,
278 HUD_deque.insert( HUD_deque.begin(), HIptr);
280 // case 3: // Radio Altimeter
281 HIptr = (instr_item *) new hud_card( cen_x + ladr_w2 + gap + 13 + ladr_t,
286 HUDS_LEFT | HUDS_VERT,
294 HUD_deque.insert( HUD_deque.begin(), HIptr);
297 HIptr = (instr_item *) new guage_instr( cen_x -ladr_w2 -gap -13 -20 -ladr_t,
301 get_elevatorval, // data source
302 HUDS_RIGHT | HUDS_VERT | HUDS_NOTEXT,
303 -100.0, // Scale data
306 HUD_deque.insert( HUD_deque.begin(), HIptr);
309 HIptr = (instr_item *) new guage_instr( cen_x-50, // x
310 cen_y -gap -ladr_w2 -20, //-85 // y
313 get_rudderval, // data source
314 HUDS_TOP | HUDS_NOTEXT,
318 HUD_deque.insert( HUD_deque.begin(), HIptr);
321 HIptr = (instr_item *) new hud_card( min_x +10 +5, //min_x +18,
326 // HUDS_LEFT | HUDS_VERT,
327 HUDS_RIGHT | HUDS_VERT,
336 HUD_deque.insert( HUD_deque.begin(), HIptr);
339 HIptr = (instr_item *) new guage_instr( min_x-10, // x
343 get_throttleval, // data source
344 // HUDS_VERT | HUDS_RIGHT | HUDS_NOTEXT,
345 HUDS_VERT | HUDS_LEFT | HUDS_NOTEXT, 100.0,
349 HUD_deque.insert( HUD_deque.begin(), HIptr);
350 // Remove this when below uncommented
352 HIptr = (instr_item *) new instr_label( 10,
366 HUD_deque.insert( HUD_deque.begin(), HIptr);
368 HIptr = (instr_item *) new lat_label( (cen_x - (compass_w/2))/2,
382 HUD_deque.insert( HUD_deque.begin(), HIptr);
384 HIptr = (instr_item *) new lon_label(((cen_x+compass_w/2)+(2*cen_x))/2,
397 HUD_deque.insert( HUD_deque.begin(), HIptr);
400 // case 10: // Digital KIAS
401 HIptr = (instr_item *) new instr_label ( 110,
415 HUD_deque.insert( HUD_deque.begin(), HIptr);
417 // case 11: // Digital Rate of Climb
418 HIptr = (instr_item *) new instr_label ( 110,
432 HUD_deque.insert( HUD_deque.begin(), HIptr);
434 // case 12: // Roll indication diagnostic
435 HIptr = (instr_item *) new instr_label ( 110,
449 HUD_deque.insert( HUD_deque.begin(), HIptr);
451 // case 13: // Angle of attack diagnostic
452 HIptr = (instr_item *) new instr_label ( 440,
466 HUD_deque.insert( HUD_deque.begin(), HIptr);
469 HIptr = (instr_item *) new instr_label ( 440,
483 HUD_deque.insert( HUD_deque.begin(), HIptr);
486 HIptr = (instr_item *) new instr_label ( 440,
500 HUD_deque.insert( HUD_deque.begin(), HIptr);
503 HIptr = (instr_item *) new instr_label( 440,
517 HUD_deque.insert( HUD_deque.begin(), HIptr);
520 HIptr = (instr_item *) new instr_label( 440,
534 HUD_deque.insert( HUD_deque.begin(), HIptr);
537 HIptr = (instr_item *) new instr_label( 440,
551 HUD_deque.insert( HUD_deque.begin(), HIptr);
554 HIptr = (instr_item *) new instr_label( 10,
568 HUD_deque.insert( HUD_deque.begin(), HIptr);
571 switch( current_options.get_tris_or_culled() ) {
573 HIptr = (instr_item *) new instr_label( 10,
589 HIptr = (instr_item *) new instr_label( 10,
608 HIptr = (instr_item *) new instr_label( 10,
622 HUD_deque.insert( HUD_deque.begin(), HIptr);
627 // if( HIptr ) { // Anything to install?
628 // HUD_deque.insert( HUD_deque.begin(), HIptr);
634 return 0; // For now. Later we may use this for an error code.
638 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
640 // instr_item *HIptr;
646 // int max_x = 640-off;
658 font_size = (current_options.get_xsize() > 1000) ? LARGE : SMALL;
662 FG_LOG( FG_COCKPIT, FG_INFO, "Initializing current aircraft HUD" );
664 // deque < instr_item * > :: iterator first = HUD_deque.begin();
665 // deque < instr_item * > :: iterator last = HUD_deque.end();
666 // HUD_deque.erase( first, last); // empty the HUD deque
667 HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
672 // For now lets just hardcode the hud here.
673 // In the future, hud information has to come from the same place
674 // aircraft information came from.
676 // fgHUDSetTimeMode( hud, NIGHT );
677 // fgHUDSetBrightness( hud, BRT_LIGHT );
684 p = new HudLadder( cen_x-ladr_w2, cen_y-ladr_h2, 2*ladr_w2, 2*ladr_h2, 1 );
685 HUD_deque.push_front( p );
687 // case 4: // GYRO COMPASS
688 p =new hud_card( cen_x-(compass_w/2),
701 HUD_deque.push_front( p );
703 p = new lat_label( (cen_x - compass_w/2)/2,
716 HUD_deque.push_front( p );
718 // p = new instr_label( 140, 450, 60, 10,
729 // HUD_deque.push_front( p );
731 p = new lon_label(((cen_x+compass_w/2)+(2*cen_x))/2,
744 HUD_deque.push_front( p );
748 p = new instr_label( x_pos, 25, 60, 10,
759 HUD_deque.push_front( p );
761 p = new instr_label( x_pos, 40, 120, 10,
772 HUD_deque.push_front( p );
774 p = new instr_label( x_pos, 55, 120, 10,
785 HUD_deque.push_front( p );
788 // p = new instr_label( x_pos, 70, 90, 10,
789 p = new instr_label( x_pos, 40, 90, 10,
800 HUD_deque.push_front( p );
804 p = new instr_label ( x_pos,
818 HUD_deque.push_front( p );
820 p = new instr_label( x_pos, 55, 40, 30,
831 HUD_deque.push_front( p );
833 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
834 strcpy(units, " ft");
838 p = new instr_label( x_pos, 40, 40, 10,
849 HUD_deque.push_front( p );
851 p = new instr_label( x_pos, 25, 40, 10,
862 HUD_deque.push_front( p );
864 p = new instr_label( x_pos, 10, 60, 10,
875 HUD_deque.push_front( p );
877 p = new fgTBI_instr( 290, 55, 60, 10 ); // 270
878 HUD_deque.push_front( p );
880 p = new guage_instr( 270, //250, // x
881 390, //360, //400, //45, //420, // y
884 get_aileronval, // data source
885 HUDS_BOTTOM | HUDS_NOTEXT,
889 HUD_deque.push_front( p );
891 p = new guage_instr( 20, // x
895 get_elevatorval, // data source
896 HUDS_RIGHT | HUDS_VERT | HUDS_NOTEXT,
897 -100.0, // Scale data
900 HUD_deque.push_front( p );
902 p = new guage_instr( 270, //250, // x
906 get_rudderval, // data source
907 HUDS_TOP | HUDS_NOTEXT,
911 HUD_deque.push_front( p );
913 p = new guage_instr( 600, // x
917 get_throttleval, // data source
918 HUDS_VERT | HUDS_LEFT | HUDS_NOTEXT,
922 HUD_deque.push_front( p );
924 return 0; // For now. Later we may use this for an error code.
927 int global_day_night_switch = DAY;
929 void HUD_brightkey( bool incr_bright )
931 instr_item *pHUDInstr = HUD_deque[0];
932 int brightness = pHUDInstr->get_brightness();
934 if( current_options.get_hud_status() ) {
936 switch (brightness) {
938 current_options.set_hud_status(0);
942 brightness = BRT_LIGHT;
946 brightness = BRT_MEDIUM;
950 brightness = BRT_DARK;
954 brightness = BRT_BLACK;
958 switch (brightness) {
960 brightness = BRT_MEDIUM;
964 brightness = BRT_DARK;
968 brightness = BRT_BLACK;
973 current_options.set_hud_status(0);
978 current_options.set_hud_status(1);
980 if( DAY == global_day_night_switch ) {
981 brightness = BRT_BLACK;
984 brightness = BRT_DARK;
985 global_day_night_switch = DAY;
989 if( NIGHT == global_day_night_switch ) {
990 brightness = BRT_DARK;
993 brightness = BRT_MEDIUM;
994 global_day_night_switch = NIGHT;
998 pHUDInstr->SetBrightness( brightness );
1004 // Performs a once around the list of calls to instruments installed in
1005 // the HUD object with requests for redraw. Kinda. It will when this is
1008 void fgUpdateHUD( void ) {
1010 // int day_night_sw = current_aircraft.controls->day_night_switch;
1011 int day_night_sw = global_day_night_switch;
1012 int hud_displays = HUD_deque.size();
1013 instr_item *pHUDInstr;
1016 if( !hud_displays ) { // Trust everyone, but ALWAYS cut the cards!
1020 HUD_TextList.erase();
1021 HUD_LineList.erase();
1022 // HUD_StippleLineList.erase();
1024 pHUDInstr = HUD_deque[0];
1025 brightness = pHUDInstr->get_brightness();
1026 // brightness = HUD_deque.at(0)->get_brightness();
1028 glMatrixMode(GL_PROJECTION);
1032 gluOrtho2D(0, 640, 0, 480);
1033 glMatrixMode(GL_MODELVIEW);
1037 glColor3f(1.0, 1.0, 1.0);
1040 glDisable(GL_DEPTH_TEST);
1041 glDisable(GL_LIGHTING);
1043 // We can do translucency, so why not. :-)
1044 // glEnable ( GL_BLEND ) ;
1045 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
1047 if( day_night_sw == DAY) {
1048 switch (brightness) {
1050 // glColor4f (0.1, 0.9, 0.1, 0.75);
1051 glColor3f (0.1, 0.9, 0.1);
1055 // glColor4f (0.1, 0.7, 0.0, 0.75);
1056 glColor3f (0.1, 0.7, 0.0);
1060 // glColor4f (0.0, 0.6, 0.0, 0.75);
1061 glColor3f(0.0, 0.6, 0.0);
1065 // glColor4f( 0.0, 0.0, 0.0, 0.75);
1066 glColor3f( 0.0, 0.0, 0.0);
1073 if( day_night_sw == NIGHT) {
1074 switch (brightness) {
1076 // glColor4f (0.9, 0.1, 0.1, 0.75);
1077 glColor3f (0.9, 0.1, 0.1);
1081 // glColor4f (0.7, 0.0, 0.1, 0.75);
1082 glColor3f (0.7, 0.0, 0.1);
1087 // glColor4f (0.6, 0.0, 0.0, 0.75);
1088 glColor3f (0.6, 0.0, 0.0);
1091 else { // Just in case default
1092 // glColor4f (0.1, 0.9, 0.1, 0.75);
1093 glColor3f (0.1, 0.9, 0.1);
1097 deque < instr_item * > :: iterator current = HUD_deque.begin();
1098 deque < instr_item * > :: iterator last = HUD_deque.end();
1100 for ( ; current != last; ++current ) {
1101 pHUDInstr = *current;
1103 if( pHUDInstr->enabled()) {
1104 // fgPrintf( FG_COCKPIT, FG_DEBUG, "HUD Code %d Status %d\n",
1105 // hud->code, hud->status );
1107 // HUD_deque.at(i)->draw(); // Responsible for broken or fixed variants.
1108 // No broken displays honored just now.
1112 char *gmt_str = get_formated_gmt_time();
1113 HUD_TextList.add( fgText( 40, 10, gmt_str) );
1115 #ifdef FG_NETWORK_OLK
1116 if ( net_hud_display ) {
1121 HUD_TextList.draw();
1123 line_width = (current_options.get_xsize() > 1000) ? 1.0 : 0.5;
1124 glLineWidth(line_width);
1125 HUD_LineList.draw();
1127 // glEnable(GL_LINE_STIPPLE);
1128 // glLineStipple( 1, 0x00FF );
1129 // HUD_StippleLineList.draw();
1130 // glDisable(GL_LINE_STIPPLE);
1132 // glDisable( GL_BLEND );
1134 glEnable(GL_DEPTH_TEST);
1135 glEnable(GL_LIGHTING);
1136 glMatrixMode(GL_PROJECTION);
1138 glMatrixMode(GL_MODELVIEW);
1146 // Performs a once around the list of calls to instruments installed in
1147 // the HUD object with requests for redraw. Kinda. It will when this is
1150 void fgUpdateHUD( void ) {
1152 // int day_night_sw = current_aircraft.controls->day_night_switch;
1153 int day_night_sw = global_day_night_switch;
1154 int hud_displays = HUD_deque.size();
1155 instr_item *pHUDInstr;
1158 if( !hud_displays ) { // Trust everyone, but ALWAYS cut the cards!
1162 HUD_TextList.erase();
1163 HUD_LineList.erase();
1164 // HUD_StippleLineList.erase();
1166 pHUDInstr = HUD_deque[0];
1167 brightness = pHUDInstr->get_brightness();
1168 // brightness = HUD_deque.at(0)->get_brightness();
1170 glMatrixMode(GL_PROJECTION);
1174 gluOrtho2D(0, 640, 0, 480);
1175 glMatrixMode(GL_MODELVIEW);
1179 glColor3f(1.0, 1.0, 1.0);
1182 glDisable(GL_DEPTH_TEST);
1183 glDisable(GL_LIGHTING);
1185 // We can do translucency, so why not. :-)
1186 // glEnable ( GL_BLEND ) ;
1187 // glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
1189 if( day_night_sw == DAY) {
1190 switch (brightness) {
1192 // glColor4f (0.1, 0.9, 0.1, 0.75);
1193 glColor3f (0.1, 0.9, 0.1);
1197 // glColor4f (0.1, 0.7, 0.0, 0.75);
1198 glColor3f (0.1, 0.7, 0.0);
1202 // glColor4f (0.0, 0.6, 0.0, 0.75);
1203 glColor3f(0.0, 0.6, 0.0);
1207 // glColor4f( 0.0, 0.0, 0.0, 0.75);
1208 glColor3f( 0.0, 0.0, 0.0);
1215 if( day_night_sw == NIGHT) {
1216 switch (brightness) {
1218 // glColor4f (0.9, 0.1, 0.1, 0.75);
1219 glColor3f (0.9, 0.1, 0.1);
1223 // glColor4f (0.7, 0.0, 0.1, 0.75);
1224 glColor3f (0.7, 0.0, 0.1);
1229 // glColor4f (0.6, 0.0, 0.0, 0.75);
1230 glColor3f (0.6, 0.0, 0.0);
1233 else { // Just in case default
1234 // glColor4f (0.1, 0.9, 0.1, 0.75);
1235 glColor3f (0.1, 0.9, 0.1);
1239 deque < instr_item * > :: iterator current = HUD_deque.begin();
1240 deque < instr_item * > :: iterator last = HUD_deque.end();
1242 for ( ; current != last; ++current ) {
1243 pHUDInstr = *current;
1245 if( pHUDInstr->enabled()) {
1246 // fgPrintf( FG_COCKPIT, FG_DEBUG, "HUD Code %d Status %d\n",
1247 // hud->code, hud->status );
1249 // HUD_deque.at(i)->draw(); // Responsible for broken or fixed variants.
1250 // No broken displays honored just now.
1254 char *gmt_str = get_formated_gmt_time();
1255 HUD_TextList.add( fgText(40, 10, gmt_str) );
1258 extern bool fgAPAltitudeEnabled( void );
1259 extern bool fgAPHeadingEnabled( void );
1260 extern bool fgAPWayPointEnabled( void );
1261 extern char *fgAPget_TargetDistanceStr( void );
1262 extern char *fgAPget_TargetHeadingStr( void );
1263 extern char *fgAPget_TargetAltitudeStr( void );
1264 extern char *fgAPget_TargetLatLonStr( void );
1267 // char scratch[128];
1268 // HUD_TextList.add( fgText( "AUTOPILOT", 20, apY) );
1270 if( fgAPHeadingEnabled() ) {
1271 HUD_TextList.add( fgText( 40, apY, fgAPget_TargetHeadingStr()) );
1274 if( fgAPAltitudeEnabled() ) {
1275 HUD_TextList.add( fgText( 40, apY, fgAPget_TargetAltitudeStr()) );
1278 if( fgAPWayPointEnabled() ) {
1279 HUD_TextList.add( fgText( 40, apY, fgAPget_TargetLatLonStr()) );
1281 HUD_TextList.add( fgText( 40, apY, fgAPget_TargetDistanceStr() ) );
1285 HUD_TextList.draw();
1287 line_width = (current_options.get_xsize() > 1000) ? 1.0 : 0.5;
1288 glLineWidth(line_width);
1289 HUD_LineList.draw();
1291 // glEnable(GL_LINE_STIPPLE);
1292 // glLineStipple( 1, 0x00FF );
1293 // HUD_StippleLineList.draw();
1294 // glDisable(GL_LINE_STIPPLE);
1296 // glDisable( GL_BLEND );
1298 glEnable(GL_DEPTH_TEST);
1299 glEnable(GL_LIGHTING);
1300 glMatrixMode(GL_PROJECTION);
1302 glMatrixMode(GL_MODELVIEW);