1
0
Fork 0

Proper shutdown() for the model manager

- also move property setup to bind/unbind
This commit is contained in:
James Turner 2015-12-18 19:56:56 -08:00
parent c76d106e64
commit 89065ea5c2
4 changed files with 39 additions and 34 deletions

View file

@ -895,7 +895,7 @@ void fgCreateSubsystems(bool duringReset) {
} }
globals->add_subsystem("aircraft-model", new FGAircraftModel, SGSubsystemMgr::DISPLAY); globals->add_subsystem("aircraft-model", new FGAircraftModel, SGSubsystemMgr::DISPLAY);
globals->add_subsystem("model-manager", new FGModelMgr, SGSubsystemMgr::DISPLAY); globals->add_new_subsystem<FGModelMgr>(SGSubsystemMgr::DISPLAY);
globals->add_new_subsystem<FGViewMgr>(SGSubsystemMgr::DISPLAY); globals->add_new_subsystem<FGViewMgr>(SGSubsystemMgr::DISPLAY);
} }

View file

@ -216,7 +216,6 @@ FGGlobals::~FGGlobals()
subsystem_mgr->unbind(); subsystem_mgr->unbind();
subsystem_mgr->remove("aircraft-model"); subsystem_mgr->remove("aircraft-model");
subsystem_mgr->remove("model-manager");
subsystem_mgr->remove(FGTileMgr::subsystemName()); subsystem_mgr->remove(FGTileMgr::subsystemName());

View file

@ -28,46 +28,45 @@
#include "modelmgr.hxx" #include "modelmgr.hxx"
using std::vector;
using namespace simgear; using namespace simgear;
// OSGFIXME // OSGFIXME
// extern SGShadowVolume *shadows; // extern SGShadowVolume *shadows;
FGModelMgr::FGModelMgr () FGModelMgr::FGModelMgr ()
: _models(fgGetNode("/models", true)),
_listener(new Listener(this))
{ {
_models->addChangeListener(_listener);
} }
#include <stdio.h>
FGModelMgr::~FGModelMgr () FGModelMgr::~FGModelMgr ()
{ {
_models->removeChangeListener(_listener);
delete _listener;
osg::Group *scene_graph = globals->get_scenery()->get_scene_graph();
if (!scene_graph)
SG_LOG(SG_AIRCRAFT, SG_ALERT, "Warning: The scenegraph wass deleted before the models could be removed");
for (unsigned int i = 0; i < _instances.size(); i++) {
if (scene_graph)
scene_graph->removeChild(_instances[i]->model->getSceneGraph());
delete _instances[i];
}
} }
void void
FGModelMgr::init () FGModelMgr::init ()
{ {
vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model"); std::vector<SGPropertyNode_ptr> model_nodes = _models->getChildren("model");
for (unsigned int i = 0; i < model_nodes.size(); i++) for (unsigned int i = 0; i < model_nodes.size(); i++)
add_model(model_nodes[i]); add_model(model_nodes[i]);
} }
void FGModelMgr::shutdown()
{
osg::Group *scene_graph = NULL;
if (globals->get_scenery()) {
scene_graph = globals->get_scenery()->get_scene_graph();
}
// always delete instances, even if the scene-graph is gone
for (unsigned int i = 0; i < _instances.size(); i++) {
if (scene_graph) {
scene_graph->removeChild(_instances[i]->model->getSceneGraph());
}
delete _instances[i];
}
}
void void
FGModelMgr::add_model (SGPropertyNode * node) FGModelMgr::add_model (SGPropertyNode * node)
{ {
@ -141,11 +140,18 @@ FGModelMgr::add_model (SGPropertyNode * node)
void void
FGModelMgr::bind () FGModelMgr::bind ()
{ {
_models = fgGetNode("/models", true);
_listener.reset(new Listener(this));
_models->addChangeListener(_listener.get());
} }
void void
FGModelMgr::unbind () FGModelMgr::unbind ()
{ {
_models->removeChangeListener(_listener.get());
_listener.reset();
_models.clear();
} }
namespace namespace
@ -219,7 +225,7 @@ FGModelMgr::add_instance (Instance * instance)
void void
FGModelMgr::remove_instance (Instance * instance) FGModelMgr::remove_instance (Instance * instance)
{ {
vector<Instance *>::iterator it; std::vector<Instance *>::iterator it;
for (it = _instances.begin(); it != _instances.end(); it++) { for (it = _instances.begin(); it != _instances.end(); it++) {
if (*it == instance) { if (*it == instance) {
_instances.erase(it); _instances.erase(it);
@ -274,8 +280,8 @@ FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * chi
return; return;
// search instance by node and remove it from scenegraph // search instance by node and remove it from scenegraph
vector<Instance *>::iterator it = _mgr->_instances.begin(); std::vector<Instance *>::iterator it = _mgr->_instances.begin();
vector<Instance *>::iterator end = _mgr->_instances.end(); std::vector<Instance *>::iterator end = _mgr->_instances.end();
for (; it != end; ++it) { for (; it != end; ++it) {
Instance *instance = *it; Instance *instance = *it;
@ -287,7 +293,10 @@ FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * chi
// OSGFIXME // OSGFIXME
// if (shadows && instance->shadow) // if (shadows && instance->shadow)
// shadows->deleteOccluder(branch); // shadows->deleteOccluder(branch);
globals->get_scenery()->get_scene_graph()->removeChild(branch);
if (globals->get_scenery() && globals->get_scenery()->get_scene_graph()) {
globals->get_scenery()->get_scene_graph()->removeChild(branch);
}
delete instance; delete instance;
break; break;

View file

@ -6,17 +6,12 @@
#ifndef __MODELMGR_HXX #ifndef __MODELMGR_HXX
#define __MODELMGR_HXX 1 #define __MODELMGR_HXX 1
#ifndef __cplusplus
# error This library requires C++
#endif
#include <vector> #include <vector>
#include <memory>
#include <simgear/compiler.h> // for SG_USING_STD #include <simgear/compiler.h> // for SG_USING_STD
#include <simgear/structure/subsystem_mgr.hxx> #include <simgear/structure/subsystem_mgr.hxx>
using std::vector;
// Don't pull in headers, since we don't need them here. // Don't pull in headers, since we don't need them here.
class SGPropertyNode; class SGPropertyNode;
class SGModelPlacement; class SGModelPlacement;
@ -61,6 +56,8 @@ public:
virtual ~FGModelMgr (); virtual ~FGModelMgr ();
virtual void init (); virtual void init ();
virtual void shutdown ();
virtual void bind (); virtual void bind ();
virtual void unbind (); virtual void unbind ();
virtual void update (double dt); virtual void update (double dt);
@ -87,7 +84,7 @@ public:
*/ */
virtual void remove_instance (Instance * instance); virtual void remove_instance (Instance * instance);
static const char* subsystemName() { return "model-manager"; }
private: private:
/** /**
* Listener class that adds models at runtime. * Listener class that adds models at runtime.
@ -104,9 +101,9 @@ private:
}; };
SGPropertyNode_ptr _models; SGPropertyNode_ptr _models;
Listener * _listener; std::auto_ptr<Listener> _listener;
vector<Instance *> _instances; std::vector<Instance *> _instances;
}; };