diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 4c0f580b0..efbb4a971 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -31,6 +31,7 @@ #include <GUI/dialog.hxx> #include <Aircraft/replay.hxx> #include <Scenery/scenery.hxx> +#include <Scenery/tilemgr.hxx> #include <Scripting/NasalSys.hxx> #include <Sound/sample_queue.hxx> #include <Airports/xmlloader.hxx> @@ -500,26 +501,29 @@ do_tile_cache_reload (const SGPropertyNode * arg) /** * Reload the materials definition */ - static bool - do_materials_reload (const SGPropertyNode * arg) - { - SG_LOG(SG_INPUT, SG_INFO, "Reloading Materials"); - SGMaterialLib* new_matlib = new SGMaterialLib; - SGPath mpath( globals->get_fg_root() ); - mpath.append( fgGetString("/sim/rendering/materials-file") ); - bool loaded = new_matlib->load(globals->get_fg_root(), +static bool +do_materials_reload (const SGPropertyNode * arg) +{ + SG_LOG(SG_INPUT, SG_INFO, "Reloading Materials"); + SGMaterialLib* new_matlib = new SGMaterialLib; + SGPath mpath( globals->get_fg_root() ); + mpath.append( fgGetString("/sim/rendering/materials-file") ); + bool loaded = new_matlib->load(globals->get_fg_root(), mpath.str(), globals->get_props()); - - if ( ! loaded ) { + + if ( ! loaded ) { SG_LOG( SG_GENERAL, SG_ALERT, "Error loading materials file " << mpath.str() ); return false; - } - - globals->set_matlib(new_matlib); - return true; - } + } + + globals->set_matlib(new_matlib); + FGTileMgr* tileManager = static_cast<FGTileMgr*>(globals->get_subsystem("tile-manager")); + tileManager->materialLibChanged(); + + return true; +} #if 0 diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index d0a3d102e..e21e5b591 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -145,7 +145,6 @@ FGGlobals::FGGlobals() : fg_home( "" ), time_params( NULL ), ephem( NULL ), - matlib( NULL ), route_mgr( NULL ), ATIS_mgr( NULL ), controls( NULL ), @@ -764,8 +763,6 @@ void FGGlobals::set_tile_mgr ( FGTileMgr *t ) void FGGlobals::set_matlib( SGMaterialLib *m ) { - if (matlib) - delete matlib; matlib = m; } diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 8f1736f14..d36090846 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -114,7 +114,7 @@ private: SGEphemeris *ephem; // Material properties library - SGMaterialLib *matlib; + SGSharedPtr<SGMaterialLib> matlib; // Global autopilot "route" FGRouteMgr *route_mgr; diff --git a/src/Scenery/tilemgr.cxx b/src/Scenery/tilemgr.cxx index 3df77c0e5..ce3fd7742 100644 --- a/src/Scenery/tilemgr.cxx +++ b/src/Scenery/tilemgr.cxx @@ -86,7 +86,8 @@ void FGTileMgr::init() { SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." ); _options = new simgear::SGReaderWriterOptions; - _options->setMaterialLib(globals->get_matlib()); + + materialLibChanged(); _options->setPropertyNode(globals->get_props()); osgDB::FilePathList &fp = _options->getDatabasePathList(); @@ -116,8 +117,7 @@ void FGTileMgr::reinit() fgSetBool("/sim/sceneryloaded",false); fgSetDouble("/sim/startup/splash-alpha", 1.0); - // Reload the materials definitions - _options->setMaterialLib(globals->get_matlib()); + materialLibChanged(); // remove all old scenery nodes from scenegraph and clear cache osg::Group* group = globals->get_scenery()->get_terrain_branch(); @@ -141,6 +141,12 @@ void FGTileMgr::reinit() update(0.0); } +void FGTileMgr::materialLibChanged() +{ + _options->setMaterialLib(globals->get_matlib()); + _options->getMaterialLib()->refreshActiveMaterials(); +} + /* schedule a tile for loading, keep request for given amount of time. * Returns true if tile is already loaded. */ bool FGTileMgr::sched_tile( const SGBucket& b, double priority, bool current_view, double duration) diff --git a/src/Scenery/tilemgr.hxx b/src/Scenery/tilemgr.hxx index 2c676f73e..323f7d8b3 100644 --- a/src/Scenery/tilemgr.hxx +++ b/src/Scenery/tilemgr.hxx @@ -110,6 +110,10 @@ public: // Returns true if tiles around current view position have been loaded bool isSceneryLoaded(); + + // notify the tile manahger the material library was reloaded, + // so it can pass this through to its options object + void materialLibChanged(); }; diff --git a/utils/fgelev/fgelev.cxx b/utils/fgelev/fgelev.cxx index 90d7eb4f5..3ad0b5850 100644 --- a/utils/fgelev/fgelev.cxx +++ b/utils/fgelev/fgelev.cxx @@ -132,7 +132,7 @@ main(int argc, char** argv) simgear::ModelRegistry::instance(); sgUserDataInit(props.get()); - SGMaterialLib* ml = new SGMaterialLib; + SGMaterialLibPtr ml = new SGMaterialLib; SGPath mpath(fg_root); mpath.append("Materials/default/materials.xml"); try { diff --git a/utils/fgviewer/fgviewer.cxx b/utils/fgviewer/fgviewer.cxx index f8cbdaccc..f5bb8e944 100644 --- a/utils/fgviewer/fgviewer.cxx +++ b/utils/fgviewer/fgviewer.cxx @@ -179,7 +179,7 @@ main(int argc, char** argv) // FIXME Ok, replace this by querying the root of the property tree sgUserDataInit(props.get()); SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression); - SGMaterialLib* ml = new SGMaterialLib; + SGMaterialLibPtr ml = new SGMaterialLib; SGPath mpath(fg_root); mpath.append("Materials/default/materials.xml"); try {