From 00d4fea91cc581a4410667331c69dfdda81f20b9 Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 22 Feb 2017 15:22:40 +0100 Subject: [PATCH] Derive Animation from osg::Referenced to allow the UnrefQueue to delete it --- apps/openmw/mwrender/animation.cpp | 5 ----- apps/openmw/mwrender/animation.hpp | 7 +++---- apps/openmw/mwrender/characterpreview.cpp | 6 +++--- apps/openmw/mwrender/characterpreview.hpp | 2 +- apps/openmw/mwrender/npcanimation.cpp | 6 ------ apps/openmw/mwrender/objects.cpp | 24 ++++++++++------------- apps/openmw/mwrender/objects.hpp | 2 +- apps/openmw/mwrender/renderingmanager.cpp | 4 ++-- apps/openmw/mwrender/renderingmanager.hpp | 2 +- 9 files changed, 21 insertions(+), 37 deletions(-) diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index 6f0f3426c..0a1d5eb09 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -1673,9 +1673,4 @@ namespace MWRender } } - void PartHolder::unlink() - { - mNode = NULL; - } - } diff --git a/apps/openmw/mwrender/animation.hpp b/apps/openmw/mwrender/animation.hpp index d04760368..27572544f 100644 --- a/apps/openmw/mwrender/animation.hpp +++ b/apps/openmw/mwrender/animation.hpp @@ -58,9 +58,6 @@ public: ~PartHolder(); - /// Unreferences mNode *without* detaching it from the graph. Only use if you know what you are doing. - void unlink(); - osg::ref_ptr getNode() { return mNode; @@ -74,7 +71,7 @@ private: }; typedef boost::shared_ptr PartHolderPtr; -class Animation +class Animation : public osg::Referenced { public: enum BoneGroup { @@ -339,6 +336,8 @@ protected: public: Animation(const MWWorld::Ptr &ptr, osg::ref_ptr parentNode, Resource::ResourceSystem* resourceSystem); + + /// Must be thread safe virtual ~Animation(); MWWorld::ConstPtr getPtr() const; diff --git a/apps/openmw/mwrender/characterpreview.cpp b/apps/openmw/mwrender/characterpreview.cpp index 68b5bbad8..f412c1fac 100644 --- a/apps/openmw/mwrender/characterpreview.cpp +++ b/apps/openmw/mwrender/characterpreview.cpp @@ -218,10 +218,10 @@ namespace MWRender void CharacterPreview::rebuild() { - mAnimation.reset(NULL); + mAnimation = NULL; - mAnimation.reset(new NpcAnimation(mCharacter, mNode, mResourceSystem, true, - (renderHeadOnly() ? NpcAnimation::VM_HeadOnly : NpcAnimation::VM_Normal))); + mAnimation = new NpcAnimation(mCharacter, mNode, mResourceSystem, true, + (renderHeadOnly() ? NpcAnimation::VM_HeadOnly : NpcAnimation::VM_Normal)); onSetup(); diff --git a/apps/openmw/mwrender/characterpreview.hpp b/apps/openmw/mwrender/characterpreview.hpp index e6480e0b3..273bacedb 100644 --- a/apps/openmw/mwrender/characterpreview.hpp +++ b/apps/openmw/mwrender/characterpreview.hpp @@ -61,7 +61,7 @@ namespace MWRender MWWorld::Ptr mCharacter; - std::auto_ptr mAnimation; + osg::ref_ptr mAnimation; osg::ref_ptr mNode; std::string mCurrentAnimGroup; diff --git a/apps/openmw/mwrender/npcanimation.cpp b/apps/openmw/mwrender/npcanimation.cpp index b23edbe1c..e95626c82 100644 --- a/apps/openmw/mwrender/npcanimation.cpp +++ b/apps/openmw/mwrender/npcanimation.cpp @@ -269,12 +269,6 @@ const NpcAnimation::PartBoneMap NpcAnimation::sPartList = createPartListMap(); NpcAnimation::~NpcAnimation() { - // do not detach (delete) parts yet, this is done so the background thread can handle the deletion - for(size_t i = 0;i < ESM::PRT_Count;i++) - { - if (mObjectParts[i].get()) - mObjectParts[i]->unlink(); - } } NpcAnimation::NpcAnimation(const MWWorld::Ptr& ptr, osg::ref_ptr parentNode, Resource::ResourceSystem* resourceSystem, diff --git a/apps/openmw/mwrender/objects.cpp b/apps/openmw/mwrender/objects.cpp index 23a342735..3b9317ea9 100644 --- a/apps/openmw/mwrender/objects.cpp +++ b/apps/openmw/mwrender/objects.cpp @@ -29,8 +29,6 @@ Objects::Objects(Resource::ResourceSystem* resourceSystem, osg::ref_ptrsecond; mObjects.clear(); for (CellMap::iterator iter = mCellSceneNodes.begin(); iter != mCellSceneNodes.end(); ++iter) @@ -74,9 +72,9 @@ void Objects::insertModel(const MWWorld::Ptr &ptr, const std::string &mesh, bool { insertBegin(ptr); - std::auto_ptr anim (new ObjectAnimation(ptr, mesh, mResourceSystem, animated, allowLight)); + osg::ref_ptr anim (new ObjectAnimation(ptr, mesh, mResourceSystem, animated, allowLight)); - mObjects.insert(std::make_pair(ptr, anim.release())); + mObjects.insert(std::make_pair(ptr, anim)); } void Objects::insertCreature(const MWWorld::Ptr &ptr, const std::string &mesh, bool weaponsShields) @@ -85,16 +83,16 @@ void Objects::insertCreature(const MWWorld::Ptr &ptr, const std::string &mesh, b ptr.getRefData().getBaseNode()->setNodeMask(Mask_Actor); // CreatureAnimation - std::auto_ptr anim; + osg::ref_ptr anim; if (weaponsShields) - anim.reset(new CreatureWeaponAnimation(ptr, mesh, mResourceSystem)); + anim = new CreatureWeaponAnimation(ptr, mesh, mResourceSystem); else - anim.reset(new CreatureAnimation(ptr, mesh, mResourceSystem)); + anim = new CreatureAnimation(ptr, mesh, mResourceSystem); ptr.getClass().getContainerStore(ptr).setContListener(static_cast(anim.get())); - mObjects.insert(std::make_pair(ptr, anim.release())); + mObjects.insert(std::make_pair(ptr, anim)); } void Objects::insertNPC(const MWWorld::Ptr &ptr) @@ -102,12 +100,12 @@ void Objects::insertNPC(const MWWorld::Ptr &ptr) insertBegin(ptr); ptr.getRefData().getBaseNode()->setNodeMask(Mask_Actor); - std::auto_ptr anim (new NpcAnimation(ptr, osg::ref_ptr(ptr.getRefData().getBaseNode()), mResourceSystem)); + osg::ref_ptr anim (new NpcAnimation(ptr, osg::ref_ptr(ptr.getRefData().getBaseNode()), mResourceSystem)); ptr.getClass().getInventoryStore(ptr).setInvListener(anim.get(), ptr); ptr.getClass().getInventoryStore(ptr).setContListener(anim.get()); - mObjects.insert(std::make_pair(ptr, anim.release())); + mObjects.insert(std::make_pair(ptr, anim)); } bool Objects::removeObject (const MWWorld::Ptr& ptr) @@ -119,9 +117,8 @@ bool Objects::removeObject (const MWWorld::Ptr& ptr) if(iter != mObjects.end()) { if (mUnrefQueue.get()) - mUnrefQueue->push(iter->second->getObjectRoot()); + mUnrefQueue->push(iter->second); - delete iter->second; mObjects.erase(iter); if (ptr.getClass().isNpc()) @@ -148,7 +145,7 @@ void Objects::removeCell(const MWWorld::CellStore* store) if(ptr.getCell() == store) { if (mUnrefQueue.get()) - mUnrefQueue->push(iter->second->getObjectRoot()); + mUnrefQueue->push(iter->second); if (ptr.getClass().isNpc() && ptr.getRefData().getCustomData()) { @@ -157,7 +154,6 @@ void Objects::removeCell(const MWWorld::CellStore* store) store.setContListener(NULL); } - delete iter->second; mObjects.erase(iter++); } else diff --git a/apps/openmw/mwrender/objects.hpp b/apps/openmw/mwrender/objects.hpp index 5b91abea4..cc66ae20d 100644 --- a/apps/openmw/mwrender/objects.hpp +++ b/apps/openmw/mwrender/objects.hpp @@ -62,7 +62,7 @@ public: }; class Objects{ - typedef std::map PtrAnimationMap; + typedef std::map > PtrAnimationMap; typedef std::map > CellMap; CellMap mCellSceneNodes; diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index afdb50926..5879c14c6 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -821,8 +821,8 @@ namespace MWRender void RenderingManager::renderPlayer(const MWWorld::Ptr &player) { - mPlayerAnimation.reset(new NpcAnimation(player, player.getRefData().getBaseNode(), mResourceSystem, 0, NpcAnimation::VM_Normal, - mFirstPersonFieldOfView)); + mPlayerAnimation = new NpcAnimation(player, player.getRefData().getBaseNode(), mResourceSystem, 0, NpcAnimation::VM_Normal, + mFirstPersonFieldOfView); mCamera->setAnimation(mPlayerAnimation.get()); mCamera->attachTo(player); diff --git a/apps/openmw/mwrender/renderingmanager.hpp b/apps/openmw/mwrender/renderingmanager.hpp index fc72eb279..b1bb9fe0d 100644 --- a/apps/openmw/mwrender/renderingmanager.hpp +++ b/apps/openmw/mwrender/renderingmanager.hpp @@ -214,7 +214,7 @@ namespace MWRender std::auto_ptr mTerrain; std::auto_ptr mSky; std::auto_ptr mEffectManager; - std::auto_ptr mPlayerAnimation; + osg::ref_ptr mPlayerAnimation; osg::ref_ptr mPlayerNode; std::auto_ptr mCamera; osg::Vec3f mCurrentCameraPos;