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>
35 #include <stdio.h> // char related functions
36 #include <string.h> // strcmp()
38 #include <simgear/constants.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/props/props_io.hxx>
45 #include <Aircraft/aircraft.hxx>
46 //#include <Autopilot/xmlauto.hxx>
47 #include <GUI/new_gui.hxx> // FGFontCache
48 #include <Main/globals.hxx>
49 #include <Scenery/scenery.hxx>
54 static HUD_Properties *HUDprop = 0;
58 deque<SGSharedPtr<instr_item> > HUD_deque;
60 fgTextList HUD_TextList;
61 fgLineList HUD_LineList;
62 fgLineList HUD_StippleLineList;
64 fntRenderer *HUDtext = 0;
65 fntTexFont *HUD_Font = 0;
66 float HUD_TextSize = 0;
71 int readHud( istream &input );
72 int readInstrument ( const SGPropertyNode * node);
74 static void drawHUD(osg::State*);
75 static void fgUpdateHUDVirtual(osg::State*);
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)
99 int readInstrument(const SGPropertyNode * node)
101 static const SGPropertyNode *startup_units_node
102 = fgGetNode("/sim/startup/units");
106 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
107 strcpy(units, " ft");
112 const SGPropertyNode * ladder_group = node->getNode("ladders");
114 if (ladder_group != 0) {
115 int nLadders = ladder_group->nChildren();
116 for (int j = 0; j < nLadders; j++) {
117 HIptr = static_cast<instr_item *>(new HudLadder(ladder_group->getChild(j)));
118 HUD_deque.insert(HUD_deque.begin(), HIptr);
122 const SGPropertyNode * card_group = node->getNode("cards");
123 if (card_group != 0) {
124 int nCards = card_group->nChildren();
125 for (int j = 0; j < nCards; j++) {
126 const char *type = card_group->getChild(j)->getStringValue("type", "gauge");
128 if (!strcmp(type, "gauge"))
129 HIptr = static_cast<instr_item *>(new gauge_instr(card_group->getChild(j)));
130 else if (!strcmp(type, "dial") || !strcmp(type, "tape"))
131 HIptr = static_cast<instr_item *>(new hud_card(card_group->getChild(j)));
133 SG_LOG(SG_INPUT, SG_WARN, "HUD: unknown 'card' type: " << type);
136 HUD_deque.insert(HUD_deque.begin(), HIptr);
140 const SGPropertyNode * label_group = node->getNode("labels");
141 if (label_group != 0) {
142 int nLabels = label_group->nChildren();
143 for (int j = 0; j < nLabels; j++) {
144 HIptr = static_cast<instr_item *>(new instr_label(label_group->getChild(j)));
145 HUD_deque.insert(HUD_deque.begin(), HIptr);
149 const SGPropertyNode * tbi_group = node->getNode("tbis");
150 if (tbi_group != 0) {
151 int nTbis = tbi_group->nChildren();
152 for (int j = 0; j < nTbis; j++) {
153 HIptr = static_cast<instr_item *>(new fgTBI_instr(tbi_group->getChild(j)));
154 HUD_deque.insert(HUD_deque.begin(), HIptr);
158 const SGPropertyNode * rwy_group = node->getNode("runways");
159 if (rwy_group != 0) {
160 int nRwy = rwy_group->nChildren();
161 for (int j = 0; j < nRwy; j++) {
162 HIptr = static_cast<instr_item *>(new runway_instr(rwy_group->getChild(j)));
163 HUD_deque.insert(HUD_deque.begin(), HIptr);
167 } //end readinstrument
170 int readHud( istream &input )
176 readProperties(input, &root);
177 } catch (const sg_exception &e) {
178 guiErrorMessage("Error reading HUD: ", e);
183 SG_LOG(SG_INPUT, SG_INFO, "Read properties for " <<
184 root.getStringValue("name"));
186 if (!root.getNode("depreciated"))
187 SG_LOG(SG_INPUT, SG_ALERT, "WARNING: use of depreciated old HUD");
189 HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
192 SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
194 const SGPropertyNode * instrument_group = root.getChild("instruments");
195 int nInstruments = instrument_group->nChildren();
197 for (int i = 0; i < nInstruments; i++) {
199 const SGPropertyNode * node = instrument_group->getChild(i);
201 SGPath path( globals->get_fg_root() );
202 path.append(node->getStringValue("path"));
204 SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
209 SGPropertyNode root2;
211 readProperties(path.str(), &root2);
212 } catch (const sg_exception &e) {
213 guiErrorMessage("Error reading HUD instrument: ", e);
216 readInstrument(&root2);
225 // Constructs a HUD object and then adds in instruments. At the present
226 // the instruments are hard coded into the routine. Ultimately these need
227 // to be defined by the aircraft's instrumentation records so that the
228 // display for a Piper Cub doesn't show the speed range of a North American
229 // mustange and the engine readouts of a B36!
231 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
236 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
239 fgGetString("/sim/hud/path", "Huds/Default/default.xml");
240 SGPath path(globals->get_fg_root());
241 path.append(hud_path);
243 ifstream input(path.c_str());
245 SG_LOG(SG_INPUT, SG_ALERT,
246 "Cannot read Hud configuration from " << path.str());
253 // this chunk of code is not necessarily thread safe if the
254 // compiler optimizer reorders these statements. Note that
255 // "delete ptr" does not set "ptr = NULL". We have to do that
257 fntRenderer *tmp = HUDtext;
262 FGFontCache *fc = globals->get_fontcache();
263 HUD_Font = fc->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
265 throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
267 HUD_TextSize = fgGetFloat("/sim/hud/font/size", 10);
269 HUDtext = new fntRenderer();
270 HUDtext->setFont(HUD_Font);
271 HUDtext->setPointSize(HUD_TextSize);
272 HUD_TextList.setFont( HUDtext );
275 HUDprop = new HUD_Properties;
276 return 0; // For now. Later we may use this for an error code.
281 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
286 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
288 SGPath path(globals->get_fg_root());
289 path.append("Huds/Minimal/default.xml");
292 ifstream input(path.c_str());
294 SG_LOG(SG_INPUT, SG_ALERT,
295 "Cannot read Hud configuration from " << path.str());
302 HUDprop = new HUD_Properties;
303 return 0; // For now. Later we may use this for an error code.
306 //$$$ End - added, Neetha, 28 Nov 2k
311 // Performs a once around the list of calls to instruments installed in
312 // the HUD object with requests for redraw. Kinda. It will when this is
315 void fgUpdateHUD( osg::State* state ) {
317 static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
318 if ( HUD_style == 1 && enable3d_node->getBoolValue() ) {
319 fgUpdateHUDVirtual(state);
323 static const float normal_aspect = float(640) / float(480);
324 // note: aspect_ratio is Y/X
325 float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
326 if ( current_aspect > normal_aspect ) {
327 float aspect_adjust = current_aspect / normal_aspect;
328 float adjust = 320.0f*aspect_adjust - 320.0f;
329 fgUpdateHUD( state, -adjust, 0.0f, 640.0f+adjust, 480.0f );
331 float aspect_adjust = normal_aspect / current_aspect;
332 float adjust = 240.0f*aspect_adjust - 240.0f;
333 fgUpdateHUD( state, 0.0f, -adjust, 640.0f, 480.0f+adjust );
337 void fgUpdateHUDVirtual(osg::State* state)
339 FGViewer* view = globals->get_current_view();
341 // Standard fgfs projection, with essentially meaningless clip
342 // planes (we'll map the whole HUD plane to z=-1)
343 glMatrixMode(GL_PROJECTION);
346 gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
348 glMatrixMode(GL_MODELVIEW);
352 // Standard fgfs view direction computation
354 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
355 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
356 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
357 if (fabs(lookat[1]) > 9999)
358 lookat[1] = 9999; // FPU sanity
359 gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
361 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
362 // This is the default fgfs field of view, which the HUD files are
363 // written to assume.
364 float dx = 0.52056705; // tan(55/2)
365 float dy = dx * 0.75; // assumes 4:3 aspect ratio
367 m[0] = dx; m[4] = 0; m[ 8] = 0; m[12] = 0;
368 m[1] = 0; m[5] = dy; m[ 9] = 0; m[13] = 0;
369 m[2] = 0; m[6] = 0; m[10] = 1; m[14] = 0;
370 m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1;
373 // Convert the 640x480 "HUD standard" coordinate space to a square
374 // about the origin in the range [-1:1] at depth of -1
375 glScalef(1./320, 1./240, 1);
376 glTranslatef(-320, -240, -1);
382 glMatrixMode(GL_PROJECTION);
384 glMatrixMode(GL_MODELVIEW);
389 void fgUpdateHUD( osg::State* state, GLfloat x_start, GLfloat y_start,
390 GLfloat x_end, GLfloat y_end )
392 glMatrixMode(GL_PROJECTION);
395 gluOrtho2D(x_start, x_end, y_start, y_end);
397 glMatrixMode(GL_MODELVIEW);
403 glMatrixMode(GL_PROJECTION);
405 glMatrixMode(GL_MODELVIEW);
410 void drawHUD(osg::State* state)
412 if ( !HUD_deque.size() ) // Trust everyone, but ALWAYS cut the cards!
415 HUD_TextList.erase();
416 HUD_LineList.erase();
417 // HUD_StippleLineList.erase();
419 glDisable(GL_DEPTH_TEST);
420 glDisable(GL_LIGHTING);
422 static const SGPropertyNode *heading_enabled
423 = fgGetNode("/autopilot/locks/heading", true);
424 static const SGPropertyNode *altitude_enabled
425 = fgGetNode("/autopilot/locks/altitude", true);
427 static char hud_hdg_text[256];
428 static char hud_wp0_text[256];
429 static char hud_wp1_text[256];
430 static char hud_wp2_text[256];
431 static char hud_alt_text[256];
434 if (HUDprop->isTransparent())
435 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
437 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
439 if (HUDprop->isAntialiased()) {
440 glEnable(GL_LINE_SMOOTH);
441 glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
442 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
449 for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
451 //HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
457 if (strcmp( heading_enabled->getStringValue(), "dg-heading-hold") == 0 ) {
458 snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
459 fgGetDouble("/autopilot/settings/heading-bug-deg") );
460 HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
462 } else if ( strcmp(heading_enabled->getStringValue(), "true-heading-hold") == 0 ) {
463 snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
464 fgGetDouble("/autopilot/settings/true-heading-deg") );
465 HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
468 string wp0_id = fgGetString( "/autopilot/route-manager/wp[0]/id" );
469 if ( wp0_id.length() > 0 ) {
470 snprintf( hud_wp0_text, 256, "%5s %6.1fnm %s", wp0_id.c_str(),
471 fgGetDouble( "/autopilot/route-manager/wp[0]/dist" ),
472 fgGetString( "/autopilot/route-manager/wp[0]/eta" ) );
473 HUD_TextList.add( fgText( 40, apY, hud_wp0_text ) );
476 string wp1_id = fgGetString( "/autopilot/route-manager/wp[1]/id" );
477 if ( wp1_id.length() > 0 ) {
478 snprintf( hud_wp1_text, 256, "%5s %6.1fnm %s", wp1_id.c_str(),
479 fgGetDouble( "/autopilot/route-manager/wp[1]/dist" ),
480 fgGetString( "/autopilot/route-manager/wp[1]/eta" ) );
481 HUD_TextList.add( fgText( 40, apY, hud_wp1_text ) );
484 string wp2_id = fgGetString( "/autopilot/route-manager/wp-last/id" );
485 if ( wp2_id.length() > 0 ) {
486 snprintf( hud_wp2_text, 256, "%5s %6.1fnm %s", wp2_id.c_str(),
487 fgGetDouble( "/autopilot/route-manager/wp-last/dist" ),
488 fgGetString( "/autopilot/route-manager/wp-last/eta" ) );
489 HUD_TextList.add( fgText( 40, apY, hud_wp2_text ) );
494 if ( strcmp( altitude_enabled->getStringValue(), "altitude-hold" ) == 0 ) {
495 snprintf( hud_alt_text, 256, "alt = %.0f\n",
496 fgGetDouble("/autopilot/settings/target-altitude-ft") );
497 HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
499 } else if ( strcmp( altitude_enabled->getStringValue(), "agl-hold" ) == 0 ){
500 snprintf( hud_alt_text, 256, "agl = %.0f\n",
501 fgGetDouble("/autopilot/settings/target-agl-ft") );
502 HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
509 // glEnable(GL_LINE_STIPPLE);
510 // glLineStipple( 1, 0x00FF );
511 // HUD_StippleLineList.draw();
512 // glDisable(GL_LINE_STIPPLE);
514 if (HUDprop->isAntialiased()) {
515 glDisable(GL_ALPHA_TEST);
516 glDisable(GL_LINE_SMOOTH);
520 if (HUDprop->isTransparent())
521 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
523 glEnable(GL_DEPTH_TEST);
524 glEnable(GL_LIGHTING);
528 void fgTextList::draw()
533 vector<fgText>::iterator curString = List.begin();
534 vector<fgText>::iterator lastString = List.end();
536 glPushAttrib(GL_COLOR_BUFFER_BIT);
537 glEnable(GL_TEXTURE_2D);
539 if (HUDprop->isTransparent())
540 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
542 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
544 if (HUDprop->isAntialiased()) {
545 glEnable(GL_ALPHA_TEST);
546 glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
550 for (; curString != lastString; curString++)
551 curString->Draw(Font);
554 glDisable(GL_TEXTURE_2D);
559 // HUD property listener class
561 HUD_Properties::HUD_Properties() :
562 _current(fgGetNode("/sim/hud/current-color", true)),
563 _visibility(fgGetNode("/sim/hud/visibility", true)),
564 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
565 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
566 _red(fgGetNode("/sim/hud/color/red", true)),
567 _green(fgGetNode("/sim/hud/color/green", true)),
568 _blue(fgGetNode("/sim/hud/color/blue", true)),
569 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
570 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
571 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
578 _visibility->addChangeListener(this);
579 _antialiasing->addChangeListener(this);
580 _transparency->addChangeListener(this);
581 _red->addChangeListener(this);
582 _green->addChangeListener(this);
583 _blue->addChangeListener(this);
584 _alpha->addChangeListener(this);
585 _alpha_clamp->addChangeListener(this);
586 _brightness->addChangeListener(this);
587 _current->addChangeListener(this, true);
591 void HUD_Properties::valueChanged(SGPropertyNode *node)
593 if (!strcmp(node->getName(), "current-color")) {
594 int i = node->getIntValue();
597 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
598 if ((n = n->getChild("color", i, false))) {
599 if (n->hasValue("red"))
600 _red->setFloatValue(n->getFloatValue("red", 1.0));
601 if (n->hasValue("green"))
602 _green->setFloatValue(n->getFloatValue("green", 1.0));
603 if (n->hasValue("blue"))
604 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
605 if (n->hasValue("alpha"))
606 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
607 if (n->hasValue("alpha-clamp"))
608 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
609 if (n->hasValue("brightness"))
610 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
611 if (n->hasValue("antialiased"))
612 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
613 if (n->hasValue("transparent"))
614 _transparency->setBoolValue(n->getBoolValue("transparent", false));
617 _visible = _visibility->getBoolValue();
618 _transparent = _transparency->getBoolValue();
619 _antialiased = _antialiasing->getBoolValue();
620 float brt = _brightness->getFloatValue();
621 _r = clamp(brt * _red->getFloatValue());
622 _g = clamp(brt * _green->getFloatValue());
623 _b = clamp(brt * _blue->getFloatValue());
624 _a = clamp(_alpha->getFloatValue());
625 _cl = clamp(_alpha_clamp->getFloatValue());
629 void HUD_Properties::setColor() const
632 glColor4f(_r, _g, _b, _a);
634 glColor3f(_r, _g, _b);