Esempio n. 1
0
//------------------------------------------------------------
/*static*/ void SceneFactoryInterface::destroySceneNodeAndObjects(Ogre::SceneNode *&osn)
{
	if( osn == 0 )return;
	unsigned short objectCount = osn->numAttachedObjects();
	for( unsigned short i = 0; i < objectCount; ++i )
	{
		Ogre::MovableObject* obj = osn->detachObject(i);
		obj->_getManager()->destroyMovableObject(obj);
	}

	Ogre::Node::ChildNodeIterator iter = osn->getChildIterator();
	while( iter.hasMoreElements() )
	{
		Ogre::SceneNode* node = static_cast< Ogre::SceneNode* >( iter.getNext() );
		destroySceneNodeAndObjects(node);
	}

	osn->removeAndDestroyAllChildren();
	osn->getCreator()->destroySceneNode(osn);
	osn = 0;
}
Esempio n. 2
0
//!
//! Deletes a whole scene node tree (including attached objects).
//!
//! \param sceneNode The scene node containing the tree to delete.
//! \param sceneManager The scene manager to use for destroying the scene node.
//! \param deleteRoot Flag to control whether to delete the given scene node.
//!
void OgreTools::deepDeleteSceneNode ( Ogre::SceneNode *sceneNode, Ogre::SceneManager *sceneManager /* = 0 */, bool deleteRoot /* = false */ )
{
    // make sure the given scene node is valid
    if (!sceneNode) {
        Log::error("The given scene node is invalid.", "OgreTools::deepDeleteSceneNode");
        return;
    }

    // make sure a valid scene manager is available
    if (!sceneManager)
        sceneManager = sceneNode->getCreator();
    if (!sceneManager) {
        Log::error("No valid scene manager available.", "OgreTools::deepDeleteSceneNode");
        return;
    }

    // iterate over the list of attached objects
    Ogre::SceneNode::ObjectIterator objectIterator = sceneNode->getAttachedObjectIterator();
    while (objectIterator.hasMoreElements()) {
        Ogre::MovableObject *movableObject = objectIterator.getNext();
        sceneNode->detachObject(movableObject);
        Ogre::SceneManager *movableSceneManager = movableObject->_getManager();
        if (!movableSceneManager) {
            Log::error("The Ogre scene manager could not be obtained.", "OgreTools::deepDeleteSceneNode");
            return;
        }
        Ogre::Any customData = movableObject->getUserAny();
        if (!customData.isEmpty()) {
            OgreContainer *ogreContainer = Ogre::any_cast<OgreContainer *>(customData);
            if (ogreContainer) {
                delete ogreContainer;
                ogreContainer = 0;
            }
        }
        movableSceneManager->destroyMovableObject(movableObject);
    }

    // iterate over the list of child nodes
    Ogre::SceneNode::ChildNodeIterator childNodeIterator = sceneNode->getChildIterator();
    while (childNodeIterator.hasMoreElements()) {
        Ogre::SceneNode *childSceneNode = dynamic_cast<Ogre::SceneNode *>(childNodeIterator.getNext());
        if (childSceneNode) {
            Ogre::Any customData = childSceneNode->getUserAny();
            if (!customData.isEmpty()) {
                OgreContainer *ogreContainer = Ogre::any_cast<OgreContainer *>(customData);
                if (ogreContainer) {
                    delete ogreContainer;
                    ogreContainer = 0;
                } else {
                    CameraInfo *cameraInfo = Ogre::any_cast<CameraInfo *>(customData);
                    if (cameraInfo) {
                        delete cameraInfo;
                        cameraInfo = 0;
                    }
                }
            }
            deepDeleteSceneNode(childSceneNode, sceneManager);
        }
    }

    // destroy all child nodes of the given scene node
    sceneNode->removeAndDestroyAllChildren();

    // check if the given scene node should be destroyed as well
    if (deleteRoot) {
        Ogre::Any customData = sceneNode->getUserAny();
        if (!customData.isEmpty()) {
            OgreContainer *ogreContainer = Ogre::any_cast<OgreContainer *>(customData);
            if (ogreContainer) {
                delete ogreContainer;
                ogreContainer = 0;
            } else {
                CameraInfo *cameraInfo = Ogre::any_cast<CameraInfo *>(customData);
                if (cameraInfo) {
                    delete cameraInfo;
                    cameraInfo = 0;
                }
            }
        }
        sceneManager->destroySceneNode(sceneNode);
    }
}