]> git.mxchange.org Git - flightgear.git/blobdiff - src/Cockpit/hud.cxx
Port over remaining Point3D usage to the more type and unit safe SG* classes.
[flightgear.git] / src / Cockpit / hud.cxx
index a8b7f5dd6ffd58611ef08da794846310f0bf3b92..e6903caabbdb287910d472112eaefee5586bfb58 100644 (file)
 #include <simgear/compiler.h>
 #include <simgear/structure/exception.hxx>
 
-#include STL_STRING
-#include STL_FSTREAM
+#include <string>
+#include <fstream>
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
-#ifdef HAVE_WINDOWS_H
-#  include <windows.h>
-#endif
-
-#ifdef __BORLANDC__
-#  define exception c_exception
-#endif
-
 #include <math.h>
-
 #include <stdlib.h>
 #include <stdio.h>              // char related functions
 #include <string.h>             // strcmp()
 
-#include SG_GLU_H
-
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
-#include <simgear/props/props.hxx>
 #include <simgear/misc/sg_path.hxx>
+#include <simgear/props/props_io.hxx>
+
+#include <osg/GLU>
 
 #include <Aircraft/aircraft.hxx>
-#include <Autopilot/xmlauto.hxx>
-#include <GUI/new_gui.hxx>
+//#include <Autopilot/xmlauto.hxx>
+#include <GUI/new_gui.hxx>           // FGFontCache
 #include <Main/globals.hxx>
-#include <Main/fg_props.hxx>
 #include <Scenery/scenery.hxx>
+#include <Airports/runways.hxx>
 
 #include "hud.hxx"
 
 
-static HUD_Properties *HUD = 0;
+static HUD_Properties *HUDprop = 0;
 
 static char units[5];
 
-// The following routines obtain information concerning the aircraft's
-// current state and return it to calling instrument display routines.
-// They should eventually be member functions of the aircraft.
-//
-
-deque< instr_item * > HUD_deque;
+deque<SGSharedPtr<instr_item> > HUD_deque;
 
-fgTextList         HUD_TextList;
-fgLineList         HUD_LineList;
-fgLineList         HUD_StippleLineList;
+fgTextList HUD_TextList;
+fgLineList HUD_LineList;
+fgLineList HUD_StippleLineList;
 
 fntRenderer *HUDtext = 0;
 fntTexFont *HUD_Font = 0;
@@ -83,59 +69,12 @@ int HUD_style = 0;
 
 float HUD_matrix[16];
 
-
-static string  name;
-static int     x;
-static int     y;
-static UINT    width;
-static UINT    height;
-static float   span_units;
-static bool    working;
-static string  loadfn;
-static UINT    options;
-static float   maxValue;
-static float   minValue;
-static float   scaling;
-static UINT    major_divs;
-static UINT    minor_divs;
-static UINT    modulator;
-static int     dp_showing = 0;
-static string  label_format;
-static string  prelabel;
-static string  postlabel;
-static float   maxBankAngle;
-static float   maxSlipAngle;
-static UINT    gap_width;
-static bool    tick_bottom;
-static bool    tick_top;
-static bool    tick_right;
-static bool    tick_left;
-static bool    cap_bottom;
-static bool    cap_top;
-static bool    cap_right;
-static bool    cap_left;
-static float   marker_off;
-static string  type;
-static bool    enable_pointer;
-static string  type_pointer;
-static string  type_tick;
-static string  length_tick;
-static float   radius;
-static int     divisions;
-static int     zoom;
-static bool    tsi;
-static float   rad;
-
-
-static FLTFNPTR load_fn;
-
 int readHud( istream &input );
 int readInstrument ( const SGPropertyNode * node);
-static instr_item * readCard ( const SGPropertyNode * node);
-static instr_item * readTBI( const SGPropertyNode * node);
 
-static void drawHUD();
-static void fgUpdateHUDVirtual();
+static void drawHUD(osg::State*);
+static void fgUpdateHUDVirtual(osg::State*);
+
 
 class locRECT {
 public:
@@ -155,200 +94,7 @@ locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
 }
 // #define DEBUG
 
-//========================= End of Class Implementations===================
-// fgHUDInit
-//
-// Constructs a HUD object and then adds in instruments. At the present
-// the instruments are hard coded into the routine. Ultimately these need
-// to be defined by the aircraft's instrumentation records so that the
-// display for a Piper Cub doesn't show the speed range of a North American
-// mustange and the engine readouts of a B36!
-//
 
-static instr_item *
-readCard(const SGPropertyNode * node)
-{
-
-    instr_item *p;
-
-    name               = node->getStringValue("name");
-    x                  = node->getIntValue("x");
-    y                  = node->getIntValue("y");
-    width              = node->getIntValue("width");
-    height             = node->getIntValue("height");
-    loadfn             = node->getStringValue("loadfn");
-    options            = node->getIntValue("options");
-    maxValue           = node->getFloatValue("maxValue");
-    minValue           = node->getFloatValue("minValue");
-    scaling            = node->getFloatValue("disp_scaling");
-    major_divs         = node->getIntValue("major_divs");
-    minor_divs         = node->getIntValue("minor_divs");
-    modulator          = node->getIntValue("modulator");
-    span_units         = node->getFloatValue("value_span");
-    type               = node->getStringValue("type");
-    tick_bottom        = node->getBoolValue("tick_bottom",false);
-    tick_top           = node->getBoolValue("tick_top",false);
-    tick_right         = node->getBoolValue("tick_right",false);
-    tick_left          = node->getBoolValue("tick_left",false);
-    cap_bottom         = node->getBoolValue("cap_bottom",false);
-    cap_top            = node->getBoolValue("cap_top",false);
-    cap_right          = node->getBoolValue("cap_right",false);
-    cap_left           = node->getBoolValue("cap_left",false);
-    marker_off         = node->getFloatValue("marker_offset",0.0);
-    enable_pointer     = node->getBoolValue("enable_pointer",true);
-    type_pointer       = node->getStringValue("pointer_type");
-    type_tick          = node->getStringValue("tick_type"); // 'circle' or 'line'
-    length_tick        = node->getStringValue("tick_length"); // for variable length
-    working            = node->getBoolValue("working");
-    radius             = node->getFloatValue("radius");
-    divisions          = node->getIntValue("divisions");
-    zoom               = node->getIntValue("zoom");
-
-    SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
-
-
-    if (type=="gauge") {
-        span_units = maxValue - minValue;
-    }
-
-    if (loadfn=="anzg") {
-        load_fn = get_anzg;
-    } else if (loadfn=="heading") {
-        load_fn = get_heading;
-    } else if (loadfn=="aoa") {
-        load_fn = get_aoa;
-    } else if (loadfn=="climb") {
-        load_fn = get_climb_rate;
-    } else if (loadfn=="altitude") {
-        load_fn = get_altitude;
-    } else if (loadfn=="agl") {
-        load_fn = get_agl;
-    } else if (loadfn=="speed") {
-        load_fn = get_speed;
-    } else if (loadfn=="view_direction") {
-        load_fn = get_view_direction;
-    } else if (loadfn=="aileronval") {
-        load_fn = get_aileronval;
-    } else if (loadfn=="elevatorval") {
-        load_fn = get_elevatorval;
-    } else if (loadfn=="elevatortrimval") {
-        load_fn = get_elev_trimval;
-    } else if (loadfn=="rudderval") {
-        load_fn = get_rudderval;
-    } else if (loadfn=="throttleval") {
-        load_fn = get_throttleval;
-    }
-
-
-    if ( (type == "dial") | (type == "tape") ) {
-        p = (instr_item *) new hud_card( x,
-                                         y,
-                                         width,
-                                         height,
-                                         load_fn,
-                                         options,
-                                         maxValue, minValue,
-                                         scaling,
-                                         major_divs, minor_divs,
-                                         modulator,
-                                         dp_showing,
-                                         span_units,
-                                         type,
-                                         tick_bottom,
-                                         tick_top,
-                                         tick_right,
-                                         tick_left,
-                                         cap_bottom,
-                                         cap_top,
-                                         cap_right,
-                                         cap_left,
-                                         marker_off,
-                                         enable_pointer,
-                                         type_pointer,
-                                         type_tick,
-                                         length_tick,
-                                         working,
-                                         radius,
-                                         divisions,
-                                         zoom
-                                         );
-    } else {
-        p = (instr_item *) new  gauge_instr( x,       // x
-                                             y,       // y
-                                             width,   // width
-                                             height,  // height
-                                             load_fn, // data source
-                                             options,
-                                             scaling,
-                                             maxValue,minValue,
-                                             major_divs, minor_divs,
-                                             dp_showing,
-                                             modulator,
-                                             working);
-    }
-
-    return p;
-}// end readCard
-
-static instr_item *
-readTBI(const SGPropertyNode * node)
-{
-
-    instr_item *p;
-
-    name           = node->getStringValue("name");
-    x              = node->getIntValue("x");
-    y              = node->getIntValue("y");
-    width          = node->getIntValue("width");
-    height         = node->getIntValue("height");
-    maxBankAngle   = node->getFloatValue("maxBankAngle");
-    maxSlipAngle   = node->getFloatValue("maxSlipAngle");
-    gap_width      = node->getIntValue("gap_width");
-    working        = node->getBoolValue("working");
-    tsi            = node->getBoolValue("tsi");
-    rad            = node->getFloatValue("rad");
-
-    SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
-
-
-    p = (instr_item *) new fgTBI_instr( x,
-                                        y,
-                                        width,
-                                        height,
-                                        get_roll,
-                                        get_sideslip,
-                                        maxBankAngle,
-                                        maxSlipAngle,
-                                        gap_width,
-                                        working,
-                                        tsi,
-                                        rad);
-
-    return p;
-} //end readTBI
-
-static instr_item *
-readRunway(const SGPropertyNode * node) {
-        name     = node->getStringValue("name");
-        x        = node->getIntValue("x");
-        y        = node->getIntValue("y");
-        width    = node->getIntValue("width");
-        height   = node->getIntValue("height");
-        scaling  = node->getDoubleValue("scale");
-        working  = node->getBoolValue("working",true);
-        runway_instr *ri = new runway_instr(x,y,width,height,scaling,working);
-        double scale = node->getDoubleValue("arrow_scale",1.0);
-        ri->setDrawArrow((scale>0)?true:false);
-        ri->setDrawArrowAlways((scale>0)?node->getBoolValue("arrow_always"):false);
-        ri->setStippleOutline(node->getIntValue("outer_stipple",0xFFFF));
-        ri->setStippleCenterline(node->getIntValue("center_stipple",0xFFFF));
-        ri->setArrowRotationRadius(node->getDoubleValue("arrow_radius"));
-        ri->setArrowScale(scale);
-        ri->setLineScale(node->getDoubleValue("line_scale",1.0));
-        ri->setScaleDist(node->getDoubleValue("scale_dist_nm"));
-        SG_LOG(SG_INPUT, SG_INFO, "Done reading instrument " << name);
-        return (instr_item *) ri;
-}
 
 
 int readInstrument(const SGPropertyNode * node)
@@ -378,7 +124,16 @@ int readInstrument(const SGPropertyNode * node)
     if (card_group != 0) {
         int nCards = card_group->nChildren();
         for (int j = 0; j < nCards; j++) {
-            HIptr = readCard(card_group->getChild(j));
+            const char *type = card_group->getChild(j)->getStringValue("type", "gauge");
+
+            if (!strcmp(type, "gauge"))
+                HIptr = static_cast<instr_item *>(new gauge_instr(card_group->getChild(j)));
+            else if (!strcmp(type, "dial") || !strcmp(type, "tape"))
+                HIptr = static_cast<instr_item *>(new hud_card(card_group->getChild(j)));
+            else {
+                SG_LOG(SG_INPUT, SG_WARN, "HUD: unknown 'card' type: " << type);
+                continue;
+            }
             HUD_deque.insert(HUD_deque.begin(), HIptr);
         }
     }
@@ -396,8 +151,8 @@ int readInstrument(const SGPropertyNode * node)
     if (tbi_group != 0) {
         int nTbis = tbi_group->nChildren();
         for (int j = 0; j < nTbis; j++) {
-            HIptr = readTBI(tbi_group->getChild(j));
-            HUD_deque.insert( HUD_deque.begin(), HIptr);
+            HIptr = static_cast<instr_item *>(new fgTBI_instr(tbi_group->getChild(j)));
+            HUD_deque.insert(HUD_deque.begin(), HIptr);
         }
     }
 
@@ -405,13 +160,12 @@ int readInstrument(const SGPropertyNode * node)
     if (rwy_group != 0) {
         int nRwy = rwy_group->nChildren();
         for (int j = 0; j < nRwy; j++) {
-            SG_LOG(SG_COCKPIT, SG_DEBUG, "***  Reading runway properties ***");
-            HIptr = readRunway(rwy_group->getChild(j));
-            HUD_deque.insert( HUD_deque.begin(), HIptr);
+            HIptr = static_cast<instr_item *>(new runway_instr(rwy_group->getChild(j)));
+            HUD_deque.insert(HUD_deque.begin(), HIptr);
         }
     }
     return 0;
-}//end readinstrument
+} //end readinstrument
 
 
 int readHud( istream &input )
@@ -430,6 +184,8 @@ int readHud( istream &input )
     SG_LOG(SG_INPUT, SG_INFO, "Read properties for  " <<
            root.getStringValue("name"));
 
+    if (!root.getNode("depreciated"))
+        SG_LOG(SG_INPUT, SG_ALERT, "WARNING: use of depreciated old HUD");
 
     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
 
@@ -465,6 +221,14 @@ int readHud( istream &input )
 }
 
 
+// fgHUDInit
+//
+// Constructs a HUD object and then adds in instruments. At the present
+// the instruments are hard coded into the routine. Ultimately these need
+// to be defined by the aircraft's instrumentation records so that the
+// display for a Piper Cub doesn't show the speed range of a North American
+// mustange and the engine readouts of a B36!
+//
 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
 {
 
@@ -486,8 +250,6 @@ int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
         input.close();
     }
 
-    fgHUDReshape();
-
     if ( HUDtext ) {
         // this chunk of code is not necessarily thread safe if the
         // compiler optimizer reorders these statements.  Note that
@@ -510,12 +272,13 @@ int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
     HUDtext->setPointSize(HUD_TextSize);
     HUD_TextList.setFont( HUDtext );
 
-    if (!HUD)
-        HUD = new HUD_Properties;
+    if (!HUDprop)
+        HUDprop = new HUD_Properties;
     return 0;  // For now. Later we may use this for an error code.
 
 }
 
+
 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
 {
 
@@ -536,47 +299,25 @@ int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
         input.close();
     }
 
-    if (!HUD)
-        HUD = new HUD_Properties;
+    if (!HUDprop)
+        HUDprop = new HUD_Properties;
     return 0;  // For now. Later we may use this for an error code.
 
 }
 //$$$ End - added, Neetha, 28 Nov 2k
 
 
-void fgHUDReshape(void) {
-#if 0
-    if ( HUDtext ) {
-        // this chunk of code is not necessarily thread safe if the
-        // compiler optimizer reorders these statements.  Note that
-        // "delete ptr" does not set "ptr = NULL".  We have to do that
-        // ourselves.
-        fntRenderer *tmp = HUDtext;
-        HUDtext = NULL;
-        delete tmp;
-    }
-
-    HUD_TextSize = fgGetInt("/sim/startup/xsize") / 60;
-    HUD_TextSize = 10;
-    HUDtext = new fntRenderer();
-    HUDtext -> setFont      ( guiFntHandle ) ;
-    HUDtext -> setPointSize ( HUD_TextSize ) ;
-    HUD_TextList.setFont( HUDtext );
-#endif
-}
-
-
 // fgUpdateHUD
 //
 // Performs a once around the list of calls to instruments installed in
 // the HUD object with requests for redraw. Kinda. It will when this is
 // all C++.
 //
-void fgUpdateHUD( void ) {
+void fgUpdateHUD( osg::State* state ) {
 
     static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
     if ( HUD_style == 1 && enable3d_node->getBoolValue() ) {
-        fgUpdateHUDVirtual();
+        fgUpdateHUDVirtual(state);
         return;
     }
 
@@ -586,15 +327,15 @@ void fgUpdateHUD( void ) {
     if ( current_aspect > normal_aspect ) {
         float aspect_adjust = current_aspect / normal_aspect;
         float adjust = 320.0f*aspect_adjust - 320.0f;
-        fgUpdateHUD( -adjust, 0.0f, 640.0f+adjust, 480.0f );
+        fgUpdateHUD( state, -adjust, 0.0f, 640.0f+adjust, 480.0f );
     } else {
         float aspect_adjust = normal_aspect / current_aspect;
         float adjust = 240.0f*aspect_adjust - 240.0f;
-        fgUpdateHUD( 0.0f, -adjust, 640.0f, 480.0f+adjust );
+        fgUpdateHUD( state, 0.0f, -adjust, 640.0f, 480.0f+adjust );
     }
 }
 
-void fgUpdateHUDVirtual()
+void fgUpdateHUDVirtual(osg::State* state)
 {
     FGViewer* view = globals->get_current_view();
 
@@ -636,7 +377,7 @@ void fgUpdateHUDVirtual()
     glTranslatef(-320, -240, -1);
 
     // Do the deed
-    drawHUD();
+    drawHUD(state);
 
     // Clean up our mess
     glMatrixMode(GL_PROJECTION);
@@ -645,7 +386,8 @@ void fgUpdateHUDVirtual()
     glPopMatrix();
 }
 
-void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
+
+void fgUpdateHUD( osg::State* state, GLfloat x_start, GLfloat y_start,
                   GLfloat x_end, GLfloat y_end )
 {
     glMatrixMode(GL_PROJECTION);
@@ -657,7 +399,7 @@ void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
     glPushMatrix();
     glLoadIdentity();
 
-    drawHUD();
+    drawHUD(state);
 
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();
@@ -665,7 +407,8 @@ void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
     glPopMatrix();
 }
 
-void drawHUD()
+
+void drawHUD(osg::State* state)
 {
     if ( !HUD_deque.size() ) // Trust everyone, but ALWAYS cut the cards!
         return;
@@ -689,21 +432,21 @@ void drawHUD()
     static char hud_alt_text[256];
 
     glEnable(GL_BLEND);
-    if (HUD->isTransparent())
+    if (HUDprop->isTransparent())
         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
     else
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-    if (HUD->isAntialiased()) {
+    if (HUDprop->isAntialiased()) {
         glEnable(GL_LINE_SMOOTH);
-        glAlphaFunc(GL_GREATER, HUD->alphaClamp());
+        glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
         glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
         //glLineWidth(1.5);
     } else {
         //glLineWidth(1.0);
     }
 
-    HUD->setColor();
+    HUDprop->setColor();
     for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
 
     //HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
@@ -769,18 +512,20 @@ void drawHUD()
     // HUD_StippleLineList.draw();
     // glDisable(GL_LINE_STIPPLE);
 
-    if (HUD->isAntialiased()) {
+    if (HUDprop->isAntialiased()) {
         glDisable(GL_ALPHA_TEST);
         glDisable(GL_LINE_SMOOTH);
         //glLineWidth(1.0);
     }
 
+    if (HUDprop->isTransparent())
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_LIGHTING);
 }
 
 
-
 void fgTextList::draw()
 {
     if (!Font)
@@ -790,20 +535,21 @@ void fgTextList::draw()
     vector<fgText>::iterator lastString = List.end();
 
     glPushAttrib(GL_COLOR_BUFFER_BIT);
+    glEnable(GL_TEXTURE_2D);
     glEnable(GL_BLEND);
-    if (HUD->isTransparent())
+    if (HUDprop->isTransparent())
         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
     else
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-    if (HUD->isAntialiased()) {
+    if (HUDprop->isAntialiased()) {
         glEnable(GL_ALPHA_TEST);
-        glAlphaFunc(GL_GREATER, HUD->alphaClamp());
+        glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
     }
 
     Font->begin();
     for (; curString != lastString; curString++)
-        curString->Draw(Font,curString->digit);
+        curString->Draw(Font);
     Font->end();
 
     glDisable(GL_TEXTURE_2D);
@@ -811,7 +557,6 @@ void fgTextList::draw()
 }
 
 
-
 // HUD property listener class
 //
 HUD_Properties::HUD_Properties() :
@@ -890,3 +635,4 @@ void HUD_Properties::setColor() const
         glColor3f(_r, _g, _b);
 }
 
+