diff --git a/components/resource/bulletshape.cpp b/components/resource/bulletshape.cpp index 0cbc63a22..baff86a79 100644 --- a/components/resource/bulletshape.cpp +++ b/components/resource/bulletshape.cpp @@ -17,6 +17,14 @@ BulletShape::BulletShape() } +BulletShape::BulletShape(const BulletShape ©, const osg::CopyOp ©op) + : mCollisionShape(duplicateCollisionShape(copy.mCollisionShape)) + , mCollisionBoxHalfExtents(copy.mCollisionBoxHalfExtents) + , mCollisionBoxTranslate(copy.mCollisionBoxTranslate) + , mAnimatedShapes(copy.mAnimatedShapes) +{ +} + BulletShape::~BulletShape() { deleteShape(mCollisionShape); diff --git a/components/resource/bulletshape.hpp b/components/resource/bulletshape.hpp index cfae27eac..a007bad31 100644 --- a/components/resource/bulletshape.hpp +++ b/components/resource/bulletshape.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include #include @@ -15,12 +15,15 @@ namespace Resource { class BulletShapeInstance; - class BulletShape : public osg::Referenced + class BulletShape : public osg::Object { public: BulletShape(); + BulletShape(const BulletShape& copy, const osg::CopyOp& copyop); virtual ~BulletShape(); + META_Object(Resource, BulletShape) + btCollisionShape* mCollisionShape; // Used for actors. Note, ideally actors would use a separate loader - as it is @@ -42,6 +45,7 @@ namespace Resource btCollisionShape* getCollisionShape(); private: + void deleteShape(btCollisionShape* shape); }; diff --git a/components/resource/bulletshapemanager.cpp b/components/resource/bulletshapemanager.cpp index 4cbe62f3c..2ab7b243a 100644 --- a/components/resource/bulletshapemanager.cpp +++ b/components/resource/bulletshapemanager.cpp @@ -13,6 +13,7 @@ #include "bulletshape.hpp" #include "scenemanager.hpp" #include "niffilemanager.hpp" +#include "objectcache.hpp" namespace Resource @@ -99,6 +100,7 @@ BulletShapeManager::BulletShapeManager(const VFS::Manager* vfs, SceneManager* sc : mVFS(vfs) , mSceneManager(sceneMgr) , mNifFileManager(nifFileManager) + , mCache(new osgDB::ObjectCache) { } @@ -114,8 +116,10 @@ osg::ref_ptr BulletShapeManager::createInstance(const std:: mVFS->normalizeFilename(normalized); osg::ref_ptr shape; - Index::iterator it = mIndex.find(normalized); - if (it == mIndex.end()) + osg::ref_ptr obj = mCache->getRefFromObjectCache(normalized); + if (obj) + shape = osg::ref_ptr(static_cast(obj.get())); + else { size_t extPos = normalized.find_last_of('.'); std::string ext; @@ -137,16 +141,19 @@ osg::ref_ptr BulletShapeManager::createInstance(const std:: node->accept(visitor); shape = visitor.getShape(); if (!shape) + { + mCache->addEntryToObjectCache(normalized, NULL); return osg::ref_ptr(); + } } - mIndex[normalized] = shape; + mCache->addEntryToObjectCache(normalized, shape); } - else - shape = it->second; - osg::ref_ptr instance = shape->makeInstance(); - return instance; + if (shape) + return shape->makeInstance(); + else + return osg::ref_ptr(); } } diff --git a/components/resource/bulletshapemanager.hpp b/components/resource/bulletshapemanager.hpp index ac1523495..d9293896b 100644 --- a/components/resource/bulletshapemanager.hpp +++ b/components/resource/bulletshapemanager.hpp @@ -13,6 +13,11 @@ namespace VFS class Manager; } +namespace osgDB +{ + class ObjectCache; +} + namespace Resource { class SceneManager; @@ -34,8 +39,7 @@ namespace Resource SceneManager* mSceneManager; NifFileManager* mNifFileManager; - typedef std::map > Index; - Index mIndex; + osg::ref_ptr mCache; }; }