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("model-manager", new FGModelMgr, SGSubsystemMgr::DISPLAY);
globals->add_new_subsystem<FGModelMgr>(SGSubsystemMgr::DISPLAY);
globals->add_new_subsystem<FGViewMgr>(SGSubsystemMgr::DISPLAY);
}

View file

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

View file

@ -28,46 +28,45 @@
#include "modelmgr.hxx"
using std::vector;
using namespace simgear;
// OSGFIXME
// extern SGShadowVolume *shadows;
FGModelMgr::FGModelMgr ()
: _models(fgGetNode("/models", true)),
_listener(new Listener(this))
{
_models->addChangeListener(_listener);
}
#include <stdio.h>
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
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++)
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
FGModelMgr::add_model (SGPropertyNode * node)
{
@ -141,11 +140,18 @@ FGModelMgr::add_model (SGPropertyNode * node)
void
FGModelMgr::bind ()
{
_models = fgGetNode("/models", true);
_listener.reset(new Listener(this));
_models->addChangeListener(_listener.get());
}
void
FGModelMgr::unbind ()
{
_models->removeChangeListener(_listener.get());
_listener.reset();
_models.clear();
}
namespace
@ -219,7 +225,7 @@ FGModelMgr::add_instance (Instance * instance)
void
FGModelMgr::remove_instance (Instance * instance)
{
vector<Instance *>::iterator it;
std::vector<Instance *>::iterator it;
for (it = _instances.begin(); it != _instances.end(); it++) {
if (*it == instance) {
_instances.erase(it);
@ -274,8 +280,8 @@ FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * chi
return;
// search instance by node and remove it from scenegraph
vector<Instance *>::iterator it = _mgr->_instances.begin();
vector<Instance *>::iterator end = _mgr->_instances.end();
std::vector<Instance *>::iterator it = _mgr->_instances.begin();
std::vector<Instance *>::iterator end = _mgr->_instances.end();
for (; it != end; ++it) {
Instance *instance = *it;
@ -287,7 +293,10 @@ FGModelMgr::Listener::childRemoved(SGPropertyNode * parent, SGPropertyNode * chi
// OSGFIXME
// if (shadows && instance->shadow)
// 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;
break;

View file

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