Remove various hacks and make magvar work like a normal subsystem, as part of the environment manager. Fix the remaining users of the globals->get_mag accessor, and hence kill off the global pointer.
terrainsampler.cxx
presets.cxx
gravity.cxx
+ magvarmanager.cxx
)
set(HEADERS
terrainsampler.hxx
presets.hxx
gravity.hxx
+ magvarmanager.hxx
)
flightgear_component(Environment "${SOURCES}" "${HEADERS}")
#include "terrainsampler.hxx"
#include "Airports/simple.hxx"
#include "gravity.hxx"
+#include "magvarmanager.hxx"
class FG3DCloudsListener : public SGPropertyChangeListener {
public:
set_subsystem("precipitation", new FGPrecipitationMgr);
set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) ));
set_subsystem("ridgelift", new FGRidgeLift);
+
+ set_subsystem("magvar", new FGMagVarManager);
}
FGEnvironmentMgr::~FGEnvironmentMgr ()
remove_subsystem("controller");
delete subsys;
+ subsys = get_subsystem("magvar");
+ remove_subsystem("magvar");
+ delete subsys;
+
delete fgClouds;
delete _environment;
--- /dev/null
+// magvarmanager.cxx -- Wraps the SimGear SGMagVar in a subsystem
+//
+// Copyright (C) 2012 James Turner - zakalawe@mac.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "magvarmanager.hxx"
+
+#include <simgear/sg_inlines.h>
+#include <simgear/magvar/magvar.hxx>
+#include <simgear/timing/sg_time.hxx>
+
+#include <Main/globals.hxx>
+#include <Main/fg_props.hxx>
+
+FGMagVarManager::FGMagVarManager() :
+ _magVar(new SGMagVar)
+{
+}
+
+FGMagVarManager::~FGMagVarManager()
+{
+}
+
+void FGMagVarManager::init()
+{
+ update(0.0); // force an immediate update
+}
+
+void FGMagVarManager::bind()
+{
+ _magVarNode = fgGetNode("/environment/magnetic-variation-deg", true);
+ _magDipNode = fgGetNode("/environment/magnetic-dip-deg", true);
+}
+
+void FGMagVarManager::unbind()
+{
+ _magVarNode = SGPropertyNode_ptr();
+ _magDipNode = SGPropertyNode_ptr();
+}
+
+void FGMagVarManager::update(double dt)
+{
+ SG_UNUSED(dt);
+
+ // update magvar model
+ _magVar->update( globals->get_aircraft_position(),
+ globals->get_time_params()->getJD() );
+
+ _magVarNode->setDoubleValue(_magVar->get_magvar() * SG_RADIANS_TO_DEGREES);
+ _magDipNode->setDoubleValue(_magVar->get_magdip() * SG_RADIANS_TO_DEGREES);
+
+}
--- /dev/null
+// magvarmanager.hxx -- Wraps the SimGear SGMagVar in a subsystem
+//
+// Copyright (C) 2012 James Turner - zakalawe@mac.com
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+
+#ifndef FG_MAGVAR_MANAGER
+#define FG_MAGVAR_MANAGER 1
+
+#include <memory>
+
+#include <simgear/structure/subsystem_mgr.hxx>
+#include <simgear/props/propsfwd.hxx>
+
+// forward decls
+class SGMagVar;
+
+class FGMagVarManager : public SGSubsystem
+{
+public:
+ FGMagVarManager();
+ virtual ~FGMagVarManager();
+
+ virtual void init();
+ virtual void bind();
+ virtual void unbind();
+
+ virtual void update(double dt);
+
+private:
+ std::auto_ptr<SGMagVar> _magVar;
+
+ SGPropertyNode_ptr _magVarNode, _magDipNode;
+};
+
+#endif // of FG_MAGVAR_MANAGER
#include <Main/fg_props.hxx>
#include <Main/util.hxx>
-#include <simgear/magvar/magvar.hxx>
-
HeadingIndicator::HeadingIndicator ( SGPropertyNode *node )
:
_name(node->getStringValue("name", "heading-indicator")),
SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
_offset_node = node->getChild("offset-deg", 0, true);
- _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
+ _offset_node->setDoubleValue( -fgGetDouble("/environment/magnetic-variation-deg") );
}
_heading_in_node = fgGetNode("/orientation/heading-deg", true);
_suction_node = fgGetNode(_suction.c_str(), true);
_heading_out_node = node->getChild("indicated-heading-deg", 0, true);
_heading_bug_error_node = node->getChild("heading-bug-error-deg", 0, true);
_heading_bug_node = node->getChild("heading-bug-deg", 0, true);
-
+
reinit();
}
#include <string>
#include <sstream>
-#include <simgear/magvar/magvar.hxx>
#include <simgear/math/SGMath.hxx>
#include <Main/fg_props.hxx>
SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
if( NULL == (_offset_node = node->getChild("offset-deg", 0, false)) ) {
_offset_node = node->getChild("offset-deg", 0, true);
- _offset_node->setDoubleValue( -globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES );
+ _offset_node->setDoubleValue( -fgGetDouble("/environment/magnetic-variation-deg") );
}
_serviceable_node = node->getChild("serviceable", 0, true);
_error_node = node->getChild("heading-bug-error-deg", 0, true);
#include <simgear/structure/exception.hxx>
#include <simgear/props/props_io.hxx>
-#include <simgear/magvar/magvar.hxx>
#include <simgear/timing/sg_time.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/scene/model/particles.hxx>
return buf;
}
-/**
- * Return the magnetic variation
- */
-static double
-getMagVar ()
-{
- return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
-}
-
-
-/**
- * Return the magnetic dip
- */
-static double
-getMagDip ()
-{
- return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
-}
-
-
/**
* Return the current heading in degrees.
*/
static double
getHeadingMag ()
{
- double magheading = fgGetDouble("/orientation/heading-deg") - getMagVar();
+ double magheading = fgGetDouble("/orientation/heading-deg") -
+ fgGetDouble("/environment/magnetic-variation-deg");
return SGMiscd::normalizePeriodic(0, 360, magheading );
}
static double
getTrackMag ()
{
- double magtrack = fgGetDouble("/orientation/track-deg") - getMagVar();
+ double magtrack = fgGetDouble("/orientation/track-deg") -
+ fgGetDouble("/environment/magnetic-variation-deg");
return SGMiscd::normalizePeriodic(0, 360, magtrack );
}
_tiedProperties.Tie("/orientation/heading-magnetic-deg", getHeadingMag);
_tiedProperties.Tie("/orientation/track-magnetic-deg", getTrackMag);
- _tiedProperties.Tie("/environment/magnetic-variation-deg", getMagVar);
- _tiedProperties.Tie("/environment/magnetic-dip-deg", getMagDip);
-
// Misc. Temporary junk.
_tiedProperties.Tie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
}
#include <simgear/misc/sg_dir.hxx>
#include <simgear/timing/sg_time.hxx>
#include <simgear/ephemeris/ephemeris.hxx>
-#include <simgear/magvar/magvar.hxx>
#include <simgear/scene/material/matlib.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
#include <simgear/structure/event_mgr.hxx>
fg_home( "" ),
time_params( NULL ),
ephem( NULL ),
- mag( NULL ),
matlib( NULL ),
route_mgr( NULL ),
ATIS_mgr( NULL ),
renderer = NULL;
delete time_params;
- delete mag;
delete matlib;
delete route_mgr;
return event_mgr;
}
-const SGGeod &
+SGGeod
FGGlobals::get_aircraft_position() const
{
- if( acmodel != NULL ) {
- SGModelPlacement * mp = acmodel->get3DModel();
- if( mp != NULL )
- return mp->getPosition();
- }
- throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" );
+ if( acmodel != NULL ) {
+ SGModelPlacement * mp = acmodel->get3DModel();
+ if( mp != NULL )
+ return mp->getPosition();
+ }
+
+ // fall back to reading the property tree. this can occur during
+ // startup before the acmodel is initialised
+
+ return SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
+ fgGetDouble("/position/latitude-deg"),
+ fgGetDouble("/position/altitude-ft"));
}
SGVec3d
class SGEphemeris;
class SGCommandMgr;
-class SGMagVar;
class SGMaterialLib;
class SGPropertyNode;
class SGTime;
// Sky structures
SGEphemeris *ephem;
- // Magnetic Variation
- SGMagVar *mag;
-
// Material properties library
SGMaterialLib *matlib;
inline SGEphemeris *get_ephem() const { return ephem; }
inline void set_ephem( SGEphemeris *e ) { ephem = e; }
- inline SGMagVar *get_mag() const { return mag; }
- inline void set_mag( SGMagVar *m ) { mag = m; }
-
inline SGMaterialLib *get_matlib() const { return matlib; }
inline void set_matlib( SGMaterialLib *m ) { matlib = m; }
acmodel = model;
}
- const SGGeod & get_aircraft_position() const;
+ SGGeod get_aircraft_position() const;
SGVec3d get_aircraft_positon_cart() const;
#include <simgear/props/AtomicChangeListener.hxx>
#include <simgear/props/props.hxx>
#include <simgear/timing/sg_time.hxx>
-#include <simgear/magvar/magvar.hxx>
#include <simgear/io/raw_socket.hxx>
#include <simgear/scene/tsync/terrasync.hxx>
#include <simgear/math/SGMath.hxx>
static TimeManager* timeMgr = (TimeManager*) globals->get_subsystem("time");
timeMgr->computeTimeDeltas(sim_dt, real_dt);
- // update magvar model
- globals->get_mag()->update( globals->get_aircraft_position(),
- globals->get_time_params()->getJD() );
-
// update all subsystems
globals->get_subsystem_mgr()->update(sim_dt);
} else if ( idle_state == 6 ) {
idle_state++;
-
- // Initialize MagVar model
- SGMagVar *magvar = new SGMagVar();
- globals->set_mag( magvar );
-
- // kludge to initialize mag compass
- // (should only be done for in-flight startup)
- // update magvar model
- globals->get_mag()->update( fgGetDouble("/position/longitude-deg")
- * SGD_DEGREES_TO_RADIANS,
- fgGetDouble("/position/latitude-deg")
- * SGD_DEGREES_TO_RADIANS,
- fgGetDouble("/position/altitude-ft")
- * SG_FEET_TO_METER,
- globals->get_time_params()->getJD() );
-
fgSplashProgress("initializing subsystems");
} else if ( idle_state == 7 ) {