From c1eca1ff6b717c977eb75bf1be41026916e53869 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 5 Nov 2011 13:18:36 +0000 Subject: [PATCH] Refactor SGSky handling and ownership - sink into Renderer, remove global variable 'the sky', and hence clean up main loop and subsystem creation a little more. --- src/Environment/environment_mgr.cxx | 55 +++++++++-------- src/Environment/environment_mgr.hxx | 3 + src/Environment/ephemeris.cxx | 13 ++-- src/Environment/fgclouds.cxx | 12 ++-- src/Environment/precipitation_mgr.cxx | 6 +- src/Main/fg_init.cxx | 15 ++--- src/Main/globals.cxx | 8 +-- src/Main/main.cxx | 87 +++++---------------------- src/Main/renderer.cxx | 51 ++++++++++++---- src/Main/renderer.hxx | 4 +- src/Time/light.cxx | 3 + 11 files changed, 113 insertions(+), 144 deletions(-) diff --git a/src/Environment/environment_mgr.cxx b/src/Environment/environment_mgr.cxx index fd1804f2f..18015d6cf 100644 --- a/src/Environment/environment_mgr.cxx +++ b/src/Environment/environment_mgr.cxx @@ -31,6 +31,7 @@ #include #include
+#include
#include
#include @@ -45,9 +46,6 @@ #include "Airports/simple.hxx" #include "gravity.hxx" -class SGSky; -extern SGSky *thesky; - class FG3DCloudsListener : public SGPropertyChangeListener { public: FG3DCloudsListener( FGClouds * fgClouds ); @@ -91,7 +89,8 @@ FGEnvironmentMgr::FGEnvironmentMgr () : _altitude_n(fgGetNode("/position/altitude-ft", true)), _longitude_n(fgGetNode( "/position/longitude-deg", true )), _latitude_n( fgGetNode( "/position/latitude-deg", true )), - _3dCloudsEnableListener(new FG3DCloudsListener(fgClouds) ) + _3dCloudsEnableListener(new FG3DCloudsListener(fgClouds) ), + _sky(globals->get_renderer()->getSky()) { set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) )); set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 ); @@ -171,7 +170,7 @@ FGEnvironmentMgr::bind () _tiedProperties.setRoot( fgGetNode( "/environment", true ) ); - _tiedProperties.Tie( "effective-visibility-m", thesky, + _tiedProperties.Tie( "effective-visibility-m", _sky, &SGSky::get_visibility ); _tiedProperties.Tie("rebuild-layers", fgClouds, @@ -220,15 +219,15 @@ FGEnvironmentMgr::bind () _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) ); - _tiedProperties.Tie( "clouds3d-density", thesky, + _tiedProperties.Tie( "clouds3d-density", _sky, &SGSky::get_3dCloudDensity, &SGSky::set_3dCloudDensity); - _tiedProperties.Tie("clouds3d-vis-range", thesky, + _tiedProperties.Tie("clouds3d-vis-range", _sky, &SGSky::get_3dCloudVisRange, &SGSky::set_3dCloudVisRange); - _tiedProperties.Tie("clouds3d-wrap", thesky, + _tiedProperties.Tie("clouds3d-wrap", _sky, &SGSky::get_3dCloudWrap, &SGSky::set_3dCloudWrap); @@ -325,19 +324,19 @@ FGEnvironmentMgr::getEnvironment(const SGGeod& aPos) const double FGEnvironmentMgr::get_cloud_layer_span_m (int index) const { - return thesky->get_cloud_layer(index)->getSpan_m(); + return _sky->get_cloud_layer(index)->getSpan_m(); } void FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m) { - thesky->get_cloud_layer(index)->setSpan_m(span_m); + _sky->get_cloud_layer(index)->setSpan_m(span_m); } double FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const { - return thesky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET; + return _sky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET; } void @@ -346,88 +345,88 @@ FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft) FGEnvironment env = *_environment; env.set_elevation_ft(elevation_ft); - thesky->get_cloud_layer(index) + _sky->get_cloud_layer(index) ->setElevation_m(elevation_ft * SG_FEET_TO_METER); - thesky->get_cloud_layer(index) + _sky->get_cloud_layer(index) ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s - thesky->get_cloud_layer(index) + _sky->get_cloud_layer(index) ->setDirection(env.get_wind_from_heading_deg()); } double FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const { - return thesky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET; + return _sky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET; } void FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft) { - thesky->get_cloud_layer(index) + _sky->get_cloud_layer(index) ->setThickness_m(thickness_ft * SG_FEET_TO_METER); } double FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const { - return thesky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET; + return _sky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET; } void FGEnvironmentMgr::set_cloud_layer_transition_ft (int index, double transition_ft) { - thesky->get_cloud_layer(index) + _sky->get_cloud_layer(index) ->setTransition_m(transition_ft * SG_FEET_TO_METER); } const char * FGEnvironmentMgr::get_cloud_layer_coverage (int index) const { - return thesky->get_cloud_layer(index)->getCoverageString().c_str(); + return _sky->get_cloud_layer(index)->getCoverageString().c_str(); } void FGEnvironmentMgr::set_cloud_layer_coverage (int index, const char * coverage_name) { - if( thesky->get_cloud_layer(index)->getCoverageString() == coverage_name ) + if( _sky->get_cloud_layer(index)->getCoverageString() == coverage_name ) return; - thesky->get_cloud_layer(index)->setCoverageString(coverage_name); + _sky->get_cloud_layer(index)->setCoverageString(coverage_name); _cloudLayersDirty = true; } int FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const { - return thesky->get_cloud_layer(index)->getCoverage(); + return _sky->get_cloud_layer(index)->getCoverage(); } double FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const { - return thesky->get_cloud_layer(index)->getVisibility_m(); + return _sky->get_cloud_layer(index)->getVisibility_m(); } void FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m) { - thesky->get_cloud_layer(index)->setVisibility_m(visibility_m); + _sky->get_cloud_layer(index)->setVisibility_m(visibility_m); } double FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const { - return thesky->get_cloud_layer(index)->getMaxAlpha(); + return _sky->get_cloud_layer(index)->getMaxAlpha(); } void FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha) { - thesky->get_cloud_layer(index)->setMaxAlpha(maxalpha); + _sky->get_cloud_layer(index)->setMaxAlpha(maxalpha); } void @@ -438,10 +437,10 @@ FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type ) return; } - if( static_cast(type) == thesky->get_cloud_layer(index)->getCoverage() ) + if( static_cast(type) == _sky->get_cloud_layer(index)->getCoverage() ) return; - thesky->get_cloud_layer(index)->setCoverage(static_cast(type)); + _sky->get_cloud_layer(index)->setCoverage(static_cast(type)); _cloudLayersDirty = true; } diff --git a/src/Environment/environment_mgr.hxx b/src/Environment/environment_mgr.hxx index dab6e6169..1c99327a1 100644 --- a/src/Environment/environment_mgr.hxx +++ b/src/Environment/environment_mgr.hxx @@ -38,6 +38,7 @@ class FGMetarCtrl; class FGMetarFetcher; class FGClouds; class FGPrecipitationMgr; +class SGSky; /** * Manage environment information. @@ -102,6 +103,8 @@ private: SGPropertyNode_ptr _latitude_n; simgear::TiedPropertyList _tiedProperties; SGPropertyChangeListener * _3dCloudsEnableListener; + SGSky* _sky; + }; #endif // _ENVIRONMENT_MGR_HXX diff --git a/src/Environment/ephemeris.cxx b/src/Environment/ephemeris.cxx index 874617fd2..21ff38b4d 100644 --- a/src/Environment/ephemeris.cxx +++ b/src/Environment/ephemeris.cxx @@ -32,6 +32,10 @@ Ephemeris::Ephemeris() : _impl(NULL), _latProp(NULL) { + SGPath ephem_data_path(globals->get_fg_root()); + ephem_data_path.append("Astro"); + _impl = new SGEphemeris(ephem_data_path.c_str()); + globals->set_ephem(_impl); } Ephemeris::~Ephemeris() @@ -41,15 +45,6 @@ Ephemeris::~Ephemeris() void Ephemeris::init() { - if (_impl) { - return; - } - - SGPath ephem_data_path(globals->get_fg_root()); - ephem_data_path.append("Astro"); - _impl = new SGEphemeris(ephem_data_path.c_str()); - globals->set_ephem(_impl); - _latProp = fgGetNode("/position/latitude-deg", true); update(0.0); } diff --git a/src/Environment/fgclouds.cxx b/src/Environment/fgclouds.cxx index e7273a714..a6fd2c669 100644 --- a/src/Environment/fgclouds.cxx +++ b/src/Environment/fgclouds.cxx @@ -38,13 +38,12 @@ #include #include
+#include
#include #include
#include "fgclouds.hxx" -extern SGSky *thesky; - static bool do_delete_3Dcloud (const SGPropertyNode *arg); static bool do_move_3Dcloud (const SGPropertyNode *arg); static bool do_add_3Dcloud (const SGPropertyNode *arg); @@ -182,6 +181,8 @@ void FGClouds::buildLayer(int iLayer, const string& name, double coverage) { int CloudVarietyCount = 0; double totalCount = 0.0; + SGSky* thesky = globals->get_renderer()->getSky(); + SGPropertyNode *cloud_def_root = fgGetNode("/environment/cloudlayers/clouds", false); SGPropertyNode *box_def_root = fgGetNode("/environment/cloudlayers/boxes", false); SGPropertyNode *layer_def_root = fgGetNode("/environment/cloudlayers/layers", false); @@ -274,6 +275,7 @@ void FGClouds::buildCloudLayers(void) { double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc); double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER; + SGSky* thesky = globals->get_renderer()->getSky(); for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) { SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true); @@ -366,7 +368,7 @@ bool FGClouds::get_3dClouds() const float x = arg->getFloatValue("x-offset-m", 0.0f); float y = arg->getFloatValue("y-offset-m", 0.0f); - + SGSky* thesky = globals->get_renderer()->getSky(); SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D(); SGNewCloud cld = SGNewCloud(texture_root, arg); bool success = layer->addCloud(lon, lat, alt, x, y, index, cld.genCloud()); @@ -392,6 +394,7 @@ bool FGClouds::get_3dClouds() const int l = arg->getIntValue("layer", 0); int i = arg->getIntValue("index", 0); + SGSky* thesky = globals->get_renderer()->getSky(); SGCloudField *layer = thesky->get_cloud_layer(l)->get_layer3D(); return layer->deleteCloud(i); } @@ -410,7 +413,8 @@ bool FGClouds::get_3dClouds() const { int l = arg->getIntValue("layer", 0); int i = arg->getIntValue("index", 0); - + SGSky* thesky = globals->get_renderer()->getSky(); + float lon = arg->getFloatValue("lon-deg", 0.0f); float lat = arg->getFloatValue("lat-deg", 0.0f); float alt = arg->getFloatValue("alt-ft", 0.0f); diff --git a/src/Environment/precipitation_mgr.cxx b/src/Environment/precipitation_mgr.cxx index 1f3140547..1be206dc4 100644 --- a/src/Environment/precipitation_mgr.cxx +++ b/src/Environment/precipitation_mgr.cxx @@ -39,13 +39,11 @@ #include
#include
+#include
#include #include "precipitation_mgr.hxx" -extern SGSky *thesky; - - /** * @brief FGPrecipitation Manager constructor * @@ -141,6 +139,8 @@ float FGPrecipitationMgr::getPrecipitationAtAltitudeMax(void) max = SGCloudLayer::SG_MAX_CLOUD_COVERAGES; result = 0; + SGSky* thesky = globals->get_renderer()->getSky(); + // To avoid messing up if (thesky == NULL) return result; diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index ad85151c3..4a1881da9 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -106,7 +106,7 @@ #include #include #include - +#include #include #include "fg_init.hxx" @@ -1176,12 +1176,6 @@ bool fgInitGeneral() { // gear, its initialization call should located in this routine. // Returns non-zero if a problem encountered. bool fgInitSubsystems() { - // static const SGPropertyNode *longitude - // = fgGetNode("/sim/presets/longitude-deg"); - // static const SGPropertyNode *latitude - // = fgGetNode("/sim/presets/latitude-deg"); - // static const SGPropertyNode *altitude - // = fgGetNode("/sim/presets/altitude-ft"); SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems"); SG_LOG( SG_GENERAL, SG_INFO, "========== =========="); @@ -1239,7 +1233,8 @@ bool fgInitSubsystems() { // Initialize the weather modeling subsystem globals->add_subsystem("environment", new FGEnvironmentMgr); - + globals->add_subsystem("ephemeris", new Ephemeris); + //////////////////////////////////////////////////////////////////// // Initialize the aircraft systems and instrumentation (before the // autopilot.) @@ -1287,9 +1282,7 @@ bool fgInitSubsystems() { // sub system infrastructure. //////////////////////////////////////////////////////////////////// - SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager"); - globals->set_ATC_mgr(new FGATCMgr); - globals->get_ATC_mgr()->init(); + globals->add_subsystem("Old ATC", new FGATCMgr, SGSubsystemMgr::INIT); //////////////////////////////////////////////////////////////////// // Initialize the ATC subsystem diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index bf302987c..5f5fa60a3 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -160,10 +160,7 @@ FGGlobals::FGGlobals() : // Destructor FGGlobals::~FGGlobals() -{ - delete renderer; - renderer = NULL; - +{ // The AIModels manager performs a number of actions upon // Shutdown that implicitly assume that other subsystems // are still operational (Due to the dynamic allocation and @@ -180,6 +177,9 @@ FGGlobals::~FGGlobals() subsystem_mgr->unbind(); delete subsystem_mgr; + delete renderer; + renderer = NULL; + delete time_params; delete mag; delete matlib; diff --git a/src/Main/main.cxx b/src/Main/main.cxx index aed183c37..3532bcb5e 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -67,7 +67,6 @@ #include #include