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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <simgear/compiler.h>
24 #include <simgear/structure/exception.hxx>
38 # define exception c_exception
44 #include <stdio.h> // char related functions
45 #include <string.h> // strcmp()
49 #include <simgear/constants.h>
50 #include <simgear/debug/logstream.hxx>
51 #include <simgear/misc/sg_path.hxx>
53 #include <Aircraft/aircraft.hxx>
54 //#include <Autopilot/xmlauto.hxx>
55 #include <GUI/new_gui.hxx> // FGFontCache
56 #include <Main/globals.hxx>
57 #include <Scenery/scenery.hxx>
62 static HUD_Properties *HUDprop = 0;
66 deque<instr_item *> HUD_deque;
68 fgTextList HUD_TextList;
69 fgLineList HUD_LineList;
70 fgLineList HUD_StippleLineList;
72 fntRenderer *HUDtext = 0;
73 fntTexFont *HUD_Font = 0;
74 float HUD_TextSize = 0;
79 int readHud( istream &input );
80 int readInstrument ( const SGPropertyNode * node);
82 static void drawHUD();
83 static void fgUpdateHUDVirtual();
90 locRECT( UINT left, UINT top, UINT right, UINT bottom);
91 RECT get_rect(void) { return rect; }
94 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
107 int readInstrument(const SGPropertyNode * node)
109 static const SGPropertyNode *startup_units_node
110 = fgGetNode("/sim/startup/units");
114 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
115 strcpy(units, " ft");
120 const SGPropertyNode * ladder_group = node->getNode("ladders");
122 if (ladder_group != 0) {
123 int nLadders = ladder_group->nChildren();
124 for (int j = 0; j < nLadders; j++) {
125 HIptr = static_cast<instr_item *>(new HudLadder(ladder_group->getChild(j)));
126 HUD_deque.insert(HUD_deque.begin(), HIptr);
130 const SGPropertyNode * card_group = node->getNode("cards");
131 if (card_group != 0) {
132 int nCards = card_group->nChildren();
133 for (int j = 0; j < nCards; j++) {
134 const char *type = card_group->getChild(j)->getStringValue("type", "gauge");
136 if (!strcmp(type, "gauge"))
137 HIptr = static_cast<instr_item *>(new gauge_instr(card_group->getChild(j)));
138 else if (!strcmp(type, "dial") || !strcmp(type, "tape"))
139 HIptr = static_cast<instr_item *>(new hud_card(card_group->getChild(j)));
141 SG_LOG(SG_INPUT, SG_WARN, "HUD: unknown 'card' type: " << type);
144 HUD_deque.insert(HUD_deque.begin(), HIptr);
148 const SGPropertyNode * label_group = node->getNode("labels");
149 if (label_group != 0) {
150 int nLabels = label_group->nChildren();
151 for (int j = 0; j < nLabels; j++) {
152 HIptr = static_cast<instr_item *>(new instr_label(label_group->getChild(j)));
153 HUD_deque.insert(HUD_deque.begin(), HIptr);
157 const SGPropertyNode * tbi_group = node->getNode("tbis");
158 if (tbi_group != 0) {
159 int nTbis = tbi_group->nChildren();
160 for (int j = 0; j < nTbis; j++) {
161 HIptr = static_cast<instr_item *>(new fgTBI_instr(tbi_group->getChild(j)));
162 HUD_deque.insert(HUD_deque.begin(), HIptr);
166 const SGPropertyNode * rwy_group = node->getNode("runways");
167 if (rwy_group != 0) {
168 int nRwy = rwy_group->nChildren();
169 for (int j = 0; j < nRwy; j++) {
170 HIptr = static_cast<instr_item *>(new runway_instr(rwy_group->getChild(j)));
171 HUD_deque.insert(HUD_deque.begin(), HIptr);
175 } //end readinstrument
178 int readHud( istream &input )
184 readProperties(input, &root);
185 } catch (const sg_exception &e) {
186 guiErrorMessage("Error reading HUD: ", e);
191 SG_LOG(SG_INPUT, SG_INFO, "Read properties for " <<
192 root.getStringValue("name"));
195 HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
198 SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
200 const SGPropertyNode * instrument_group = root.getChild("instruments");
201 int nInstruments = instrument_group->nChildren();
203 for (int i = 0; i < nInstruments; i++) {
205 const SGPropertyNode * node = instrument_group->getChild(i);
207 SGPath path( globals->get_fg_root() );
208 path.append(node->getStringValue("path"));
210 SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
215 SGPropertyNode root2;
217 readProperties(path.str(), &root2);
218 } catch (const sg_exception &e) {
219 guiErrorMessage("Error reading HUD instrument: ", e);
222 readInstrument(&root2);
231 // Constructs a HUD object and then adds in instruments. At the present
232 // the instruments are hard coded into the routine. Ultimately these need
233 // to be defined by the aircraft's instrumentation records so that the
234 // display for a Piper Cub doesn't show the speed range of a North American
235 // mustange and the engine readouts of a B36!
237 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
242 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
245 fgGetString("/sim/hud/path", "Huds/Default/default.xml");
246 SGPath path(globals->get_fg_root());
247 path.append(hud_path);
249 ifstream input(path.c_str());
251 SG_LOG(SG_INPUT, SG_ALERT,
252 "Cannot read Hud configuration from " << path.str());
261 // this chunk of code is not necessarily thread safe if the
262 // compiler optimizer reorders these statements. Note that
263 // "delete ptr" does not set "ptr = NULL". We have to do that
265 fntRenderer *tmp = HUDtext;
270 FGFontCache *fc = globals->get_fontcache();
271 HUD_Font = fc->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
273 throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
275 HUD_TextSize = fgGetFloat("/sim/hud/font/size", 10);
277 HUDtext = new fntRenderer();
278 HUDtext->setFont(HUD_Font);
279 HUDtext->setPointSize(HUD_TextSize);
280 HUD_TextList.setFont( HUDtext );
283 HUDprop = new HUD_Properties;
284 return 0; // For now. Later we may use this for an error code.
289 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
294 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
296 SGPath path(globals->get_fg_root());
297 path.append("Huds/Minimal/default.xml");
300 ifstream input(path.c_str());
302 SG_LOG(SG_INPUT, SG_ALERT,
303 "Cannot read Hud configuration from " << path.str());
310 HUDprop = new HUD_Properties;
311 return 0; // For now. Later we may use this for an error code.
314 //$$$ End - added, Neetha, 28 Nov 2k
317 void fgHUDReshape(void) {
320 // this chunk of code is not necessarily thread safe if the
321 // compiler optimizer reorders these statements. Note that
322 // "delete ptr" does not set "ptr = NULL". We have to do that
324 fntRenderer *tmp = HUDtext;
329 HUD_TextSize = fgGetInt("/sim/startup/xsize") / 60;
331 HUDtext = new fntRenderer();
332 HUDtext -> setFont ( guiFntHandle ) ;
333 HUDtext -> setPointSize ( HUD_TextSize ) ;
334 HUD_TextList.setFont( HUDtext );
341 // Performs a once around the list of calls to instruments installed in
342 // the HUD object with requests for redraw. Kinda. It will when this is
345 void fgUpdateHUD( void ) {
347 static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
348 if ( HUD_style == 1 && enable3d_node->getBoolValue() ) {
349 fgUpdateHUDVirtual();
353 static const float normal_aspect = float(640) / float(480);
354 // note: aspect_ratio is Y/X
355 float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
356 if ( current_aspect > normal_aspect ) {
357 float aspect_adjust = current_aspect / normal_aspect;
358 float adjust = 320.0f*aspect_adjust - 320.0f;
359 fgUpdateHUD( -adjust, 0.0f, 640.0f+adjust, 480.0f );
361 float aspect_adjust = normal_aspect / current_aspect;
362 float adjust = 240.0f*aspect_adjust - 240.0f;
363 fgUpdateHUD( 0.0f, -adjust, 640.0f, 480.0f+adjust );
367 void fgUpdateHUDVirtual()
369 FGViewer* view = globals->get_current_view();
371 // Standard fgfs projection, with essentially meaningless clip
372 // planes (we'll map the whole HUD plane to z=-1)
373 glMatrixMode(GL_PROJECTION);
376 gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
378 glMatrixMode(GL_MODELVIEW);
382 // Standard fgfs view direction computation
384 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
385 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
386 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
387 if (fabs(lookat[1]) > 9999)
388 lookat[1] = 9999; // FPU sanity
389 gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
391 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
392 // This is the default fgfs field of view, which the HUD files are
393 // written to assume.
394 float dx = 0.52056705; // tan(55/2)
395 float dy = dx * 0.75; // assumes 4:3 aspect ratio
397 m[0] = dx; m[4] = 0; m[ 8] = 0; m[12] = 0;
398 m[1] = 0; m[5] = dy; m[ 9] = 0; m[13] = 0;
399 m[2] = 0; m[6] = 0; m[10] = 1; m[14] = 0;
400 m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1;
403 // Convert the 640x480 "HUD standard" coordinate space to a square
404 // about the origin in the range [-1:1] at depth of -1
405 glScalef(1./320, 1./240, 1);
406 glTranslatef(-320, -240, -1);
412 glMatrixMode(GL_PROJECTION);
414 glMatrixMode(GL_MODELVIEW);
419 void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
420 GLfloat x_end, GLfloat y_end )
422 glMatrixMode(GL_PROJECTION);
425 gluOrtho2D(x_start, x_end, y_start, y_end);
427 glMatrixMode(GL_MODELVIEW);
433 glMatrixMode(GL_PROJECTION);
435 glMatrixMode(GL_MODELVIEW);
442 if ( !HUD_deque.size() ) // Trust everyone, but ALWAYS cut the cards!
445 HUD_TextList.erase();
446 HUD_LineList.erase();
447 // HUD_StippleLineList.erase();
449 glDisable(GL_DEPTH_TEST);
450 glDisable(GL_LIGHTING);
452 static const SGPropertyNode *heading_enabled
453 = fgGetNode("/autopilot/locks/heading", true);
454 static const SGPropertyNode *altitude_enabled
455 = fgGetNode("/autopilot/locks/altitude", true);
457 static char hud_hdg_text[256];
458 static char hud_wp0_text[256];
459 static char hud_wp1_text[256];
460 static char hud_wp2_text[256];
461 static char hud_alt_text[256];
464 if (HUDprop->isTransparent())
465 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
467 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
469 if (HUDprop->isAntialiased()) {
470 glEnable(GL_LINE_SMOOTH);
471 glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
472 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
479 for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
481 //HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
487 if (strcmp( heading_enabled->getStringValue(), "dg-heading-hold") == 0 ) {
488 snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
489 fgGetDouble("/autopilot/settings/heading-bug-deg") );
490 HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
492 } else if ( strcmp(heading_enabled->getStringValue(), "true-heading-hold") == 0 ) {
493 snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
494 fgGetDouble("/autopilot/settings/true-heading-deg") );
495 HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
498 string wp0_id = fgGetString( "/autopilot/route-manager/wp[0]/id" );
499 if ( wp0_id.length() > 0 ) {
500 snprintf( hud_wp0_text, 256, "%5s %6.1fnm %s", wp0_id.c_str(),
501 fgGetDouble( "/autopilot/route-manager/wp[0]/dist" ),
502 fgGetString( "/autopilot/route-manager/wp[0]/eta" ) );
503 HUD_TextList.add( fgText( 40, apY, hud_wp0_text ) );
506 string wp1_id = fgGetString( "/autopilot/route-manager/wp[1]/id" );
507 if ( wp1_id.length() > 0 ) {
508 snprintf( hud_wp1_text, 256, "%5s %6.1fnm %s", wp1_id.c_str(),
509 fgGetDouble( "/autopilot/route-manager/wp[1]/dist" ),
510 fgGetString( "/autopilot/route-manager/wp[1]/eta" ) );
511 HUD_TextList.add( fgText( 40, apY, hud_wp1_text ) );
514 string wp2_id = fgGetString( "/autopilot/route-manager/wp-last/id" );
515 if ( wp2_id.length() > 0 ) {
516 snprintf( hud_wp2_text, 256, "%5s %6.1fnm %s", wp2_id.c_str(),
517 fgGetDouble( "/autopilot/route-manager/wp-last/dist" ),
518 fgGetString( "/autopilot/route-manager/wp-last/eta" ) );
519 HUD_TextList.add( fgText( 40, apY, hud_wp2_text ) );
524 if ( strcmp( altitude_enabled->getStringValue(), "altitude-hold" ) == 0 ) {
525 snprintf( hud_alt_text, 256, "alt = %.0f\n",
526 fgGetDouble("/autopilot/settings/target-altitude-ft") );
527 HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
529 } else if ( strcmp( altitude_enabled->getStringValue(), "agl-hold" ) == 0 ){
530 snprintf( hud_alt_text, 256, "agl = %.0f\n",
531 fgGetDouble("/autopilot/settings/target-agl-ft") );
532 HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
539 // glEnable(GL_LINE_STIPPLE);
540 // glLineStipple( 1, 0x00FF );
541 // HUD_StippleLineList.draw();
542 // glDisable(GL_LINE_STIPPLE);
544 if (HUDprop->isAntialiased()) {
545 glDisable(GL_ALPHA_TEST);
546 glDisable(GL_LINE_SMOOTH);
550 if (HUDprop->isTransparent())
551 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
553 glEnable(GL_DEPTH_TEST);
554 glEnable(GL_LIGHTING);
558 void fgTextList::draw()
563 vector<fgText>::iterator curString = List.begin();
564 vector<fgText>::iterator lastString = List.end();
566 glPushAttrib(GL_COLOR_BUFFER_BIT);
568 if (HUDprop->isTransparent())
569 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
571 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
573 if (HUDprop->isAntialiased()) {
574 glEnable(GL_ALPHA_TEST);
575 glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
579 for (; curString != lastString; curString++)
580 curString->Draw(Font,curString->digit);
583 glDisable(GL_TEXTURE_2D);
588 // HUD property listener class
590 HUD_Properties::HUD_Properties() :
591 _current(fgGetNode("/sim/hud/current-color", true)),
592 _visibility(fgGetNode("/sim/hud/visibility", true)),
593 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
594 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
595 _red(fgGetNode("/sim/hud/color/red", true)),
596 _green(fgGetNode("/sim/hud/color/green", true)),
597 _blue(fgGetNode("/sim/hud/color/blue", true)),
598 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
599 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
600 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
607 _visibility->addChangeListener(this);
608 _antialiasing->addChangeListener(this);
609 _transparency->addChangeListener(this);
610 _red->addChangeListener(this);
611 _green->addChangeListener(this);
612 _blue->addChangeListener(this);
613 _alpha->addChangeListener(this);
614 _alpha_clamp->addChangeListener(this);
615 _brightness->addChangeListener(this);
616 _current->addChangeListener(this, true);
620 void HUD_Properties::valueChanged(SGPropertyNode *node)
622 if (!strcmp(node->getName(), "current-color")) {
623 int i = node->getIntValue();
626 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
627 if ((n = n->getChild("color", i, false))) {
628 if (n->hasValue("red"))
629 _red->setFloatValue(n->getFloatValue("red", 1.0));
630 if (n->hasValue("green"))
631 _green->setFloatValue(n->getFloatValue("green", 1.0));
632 if (n->hasValue("blue"))
633 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
634 if (n->hasValue("alpha"))
635 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
636 if (n->hasValue("alpha-clamp"))
637 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
638 if (n->hasValue("brightness"))
639 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
640 if (n->hasValue("antialiased"))
641 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
642 if (n->hasValue("transparent"))
643 _transparency->setBoolValue(n->getBoolValue("transparent", false));
646 _visible = _visibility->getBoolValue();
647 _transparent = _transparency->getBoolValue();
648 _antialiased = _antialiasing->getBoolValue();
649 float brt = _brightness->getFloatValue();
650 _r = clamp(brt * _red->getFloatValue());
651 _g = clamp(brt * _green->getFloatValue());
652 _b = clamp(brt * _blue->getFloatValue());
653 _a = clamp(_alpha->getFloatValue());
654 _cl = clamp(_alpha_clamp->getFloatValue());
658 void HUD_Properties::setColor() const
661 glColor4f(_r, _g, _b, _a);
663 glColor3f(_r, _g, _b);