]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/HUD/HUD.cxx
Remove remaining use of fabsf() from the code, avoid an OSG header issue on OS-X.
[flightgear.git] / src / Instrumentation / HUD / HUD.cxx
index 5ff411be3d6e870e1b3ed26d4f00c284ec50e27a..eace97cc00e4a2a6746b093c298e607a5459b8d6 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <Main/globals.hxx>
 #include <Main/viewmgr.hxx>
+#include <Main/viewer.hxx>
 
 #include "HUD.hxx"
 
@@ -46,6 +47,7 @@ static float clamp(float f)
 
 
 HUD::HUD() :
+    _path(fgGetNode("/sim/hud/path[1]", "Huds/default.xml")),
     _current(fgGetNode("/sim/hud/current-color", true)),
     _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
     _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
@@ -72,10 +74,12 @@ HUD::HUD() :
     _font(0),
     _font_size(0.0),
     _style(0),
+    _listener_active(false),
     _clip_box(0)
 {
     SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
 
+    _path->addChangeListener(this);
     _visibility->addChangeListener(this);
     _3DenabledN->addChangeListener(this);
     _antialiasing->addChangeListener(this);
@@ -95,6 +99,7 @@ HUD::HUD() :
 
 HUD::~HUD()
 {
+    _path->removeChangeListener(this);
     _visibility->removeChangeListener(this);
     _3DenabledN->removeChangeListener(this);
     _antialiasing->removeChangeListener(this);
@@ -110,33 +115,54 @@ HUD::~HUD()
     _scr_heightN->removeChangeListener(this);
     _unitsN->removeChangeListener(this);
     delete _font_renderer;
-    delete _clip_box;
-
-    deque<Item *>::const_iterator it, end = _items.end();
-    for (it = _items.begin(); it != end; ++it)
-        delete *it;
-    end = _ladders.end();
-    for (it = _ladders.begin(); it != end; ++it)
-        delete *it;
+    
+    deinit();
 }
 
 
 void HUD::init()
 {
+    const char* fontName = 0;
     _font_cache = globals->get_fontcache();
+    if (!_font) {
+        fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
+        _font = _font_cache->getTexFont(fontName);
+    }
     if (!_font)
-        _font = _font_cache->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
-    if (!_font)
-        throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
+        throw sg_io_exception("/sim/hud/font/name is not a texture font",
+                              sg_location(fontName));
 
     _font_size = fgGetFloat("/sim/hud/font/size", 8);
     _font_renderer->setFont(_font);
     _font_renderer->setPointSize(_font_size);
     _text_list.setFont(_font_renderer);
 
-    load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
+    currentColorChanged();
+    
+    _path->fireValueChanged();
 }
 
+void HUD::deinit()
+{
+  deque<Item *>::const_iterator it, end = _items.end();
+    for (it = _items.begin(); it != end; ++it)
+        delete *it;
+    end = _ladders.end();
+    for (it = _ladders.begin(); it != end; ++it)
+        delete *it;
+        
+  _items.clear();
+  _ladders.clear();
+  
+  delete _clip_box;
+  _clip_box = NULL;
+}
+
+void HUD::reinit()
+{
+    deinit();
+    _path->fireValueChanged();
+}
 
 void HUD::update(double dt)
 {
@@ -177,27 +203,31 @@ void HUD::draw(osg::State&)
 
 void HUD::draw3D()
 {
+    using namespace osg;
     FGViewer* view = globals->get_current_view();
 
     // Standard fgfs projection, with essentially meaningless clip
     // planes (we'll map the whole HUD plane to z=-1)
     glMatrixMode(GL_PROJECTION);
     glPushMatrix();
-    glLoadIdentity();
-    gluPerspective(view->get_v_fov(), 1.0 / view->get_aspect_ratio(), 0.1, 10);
+    Matrixf proj
+        = Matrixf::perspective(view->get_v_fov(), 1/view->get_aspect_ratio(),
+                               0.1, 10);
+    glLoadMatrix(proj.ptr());
 
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();
-    glLoadIdentity();
 
     // Standard fgfs view direction computation
-    float lookat[3];
+    Vec3f lookat;
     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
     if (fabs(lookat[1]) > 9999)
         lookat[1] = 9999; // FPU sanity
-    gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
+    Matrixf mv = Matrixf::lookAt(Vec3f(0.0, 0.0, 0.0), lookat,
+                                 Vec3f(0.0, 1.0, 0.0));
+    glLoadMatrix(mv.ptr());
 
     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
     // This is the default fgfs field of view, which the HUD files are
@@ -227,10 +257,11 @@ void HUD::draw3D()
 
 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
 {
+    using namespace osg;
     glMatrixMode(GL_PROJECTION);
     glPushMatrix();
-    glLoadIdentity();
-    gluOrtho2D(x_start, x_end, y_start, y_end);
+    Matrixf proj = Matrixf::ortho2D(x_start, x_end, y_start, y_end);
+    glLoadMatrix(proj.ptr());
 
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();
@@ -293,6 +324,8 @@ void HUD::common_draw()
         if ((*it)->isEnabled())
             (*it)->draw();
 
+    _clip_box->unset();
+
     if (isAntialiased()) {
         glDisable(GL_ALPHA_TEST);
         glDisable(GL_LINE_SMOOTH);
@@ -312,8 +345,7 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
     const sgDebugPriority TREE = SG_INFO;
     const int MAXNEST = 10;
 
-    SGPath path(globals->get_fg_root());
-    path.append(file);
+    SGPath path(globals->resolve_maybe_aircraft_path(file));
 
     if (!level) {
         SG_LOG(SG_INPUT, TREE, endl << "load " << file);
@@ -344,7 +376,7 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
     }
 
     delete _clip_box;
-    _clip_box = new ClipBox(fgGetNode("/sim/hud/clip"), x, y);
+    _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
 
     for (int i = 0; i < root.nChildren(); i++) {
         SGPropertyNode *n = root.getChild(i);
@@ -409,30 +441,16 @@ int HUD::load(const char *file, float x, float y, int level, const string& inden
 
 void HUD::valueChanged(SGPropertyNode *node)
 {
+    if (_listener_active)
+        return;
+    _listener_active = true;
+    if (!strcmp(node->getName(), "path"))
+        load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
+
     if (!strcmp(node->getName(), "current-color")) {
-        int i = node->getIntValue();
-        if (i < 0)
-            i = 0;
-        SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
-        if ((n = n->getChild("color", i, false))) {
-            if (n->hasValue("red"))
-                _red->setFloatValue(n->getFloatValue("red", 1.0));
-            if (n->hasValue("green"))
-                _green->setFloatValue(n->getFloatValue("green", 1.0));
-            if (n->hasValue("blue"))
-                _blue->setFloatValue(n->getFloatValue("blue", 1.0));
-            if (n->hasValue("alpha"))
-                _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
-            if (n->hasValue("alpha-clamp"))
-                _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
-            if (n->hasValue("brightness"))
-                _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
-            if (n->hasValue("antialiased"))
-                _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
-            if (n->hasValue("transparent"))
-                _transparency->setBoolValue(n->getBoolValue("transparent", false));
-        }
+        currentColorChanged();
     }
+    
     _scr_width = _scr_widthN->getIntValue();
     _scr_height = _scr_heightN->getIntValue();
 
@@ -448,8 +466,39 @@ void HUD::valueChanged(SGPropertyNode *node)
     _cl = clamp(_alpha_clamp->getFloatValue());
 
     _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
+    _listener_active = false;
 }
 
+void HUD::currentColorChanged()
+{
+  SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
+  int index = _current->getIntValue();
+  if (index < 0) {
+    index = 0;
+  }
+  
+  n = n->getChild("color", index, false);
+  if (!n) {
+    return;
+  }
+  
+  if (n->hasValue("red"))
+      _red->setFloatValue(n->getFloatValue("red", 1.0));
+  if (n->hasValue("green"))
+      _green->setFloatValue(n->getFloatValue("green", 1.0));
+  if (n->hasValue("blue"))
+      _blue->setFloatValue(n->getFloatValue("blue", 1.0));
+  if (n->hasValue("alpha"))
+      _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
+  if (n->hasValue("alpha-clamp"))
+      _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
+  if (n->hasValue("brightness"))
+      _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
+  if (n->hasValue("antialiased"))
+      _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
+  if (n->hasValue("transparent"))
+      _transparency->setBoolValue(n->getBoolValue("transparent", false));
+}
 
 void HUD::setColor() const
 {
@@ -460,10 +509,8 @@ void HUD::setColor() const
 }
 
 
-
-
 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
-        float *x, float *y, float *l, float *r, float *t, float *b)
+        float *x, float *y, float *l, float *r, float *b, float *t)
 {
     fntFont *font = rend->getFont();
     float gap = font->getGap();
@@ -636,3 +683,13 @@ void ClipBox::set()
     glEnable(GL_CLIP_PLANE3);
 }
 
+
+void ClipBox::unset()
+{
+    if (_active) {
+        glDisable(GL_CLIP_PLANE0);
+        glDisable(GL_CLIP_PLANE1);
+        glDisable(GL_CLIP_PLANE2);
+        glDisable(GL_CLIP_PLANE3);
+    }
+}