1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-10 21:40:15 +00:00

Move functions independent from BulletShape into anonymous namespace

This commit is contained in:
elsid 2021-10-30 02:58:40 +02:00
parent 29a772c33f
commit ca8584f6f6
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40
2 changed files with 53 additions and 65 deletions

View File

@ -10,6 +10,53 @@
namespace Resource
{
namespace
{
btCollisionShape* duplicateCollisionShape(const btCollisionShape *shape)
{
if (shape == nullptr)
return nullptr;
if (shape->isCompound())
{
const btCompoundShape *comp = static_cast<const btCompoundShape*>(shape);
btCompoundShape* newShape = new btCompoundShape;
for (int i = 0, n = comp->getNumChildShapes(); i < n; ++i)
{
btCollisionShape* child = duplicateCollisionShape(comp->getChildShape(i));
const btTransform& trans = comp->getChildTransform(i);
newShape->addChildShape(trans, child);
}
return newShape;
}
if (const btBvhTriangleMeshShape* trishape = dynamic_cast<const btBvhTriangleMeshShape*>(shape))
return new btScaledBvhTriangleMeshShape(const_cast<btBvhTriangleMeshShape*>(trishape), btVector3(1.f, 1.f, 1.f));
if (const btBoxShape* boxshape = dynamic_cast<const btBoxShape*>(shape))
return new btBoxShape(*boxshape);
if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
return new btHeightfieldTerrainShape(static_cast<const btHeightfieldTerrainShape&>(*shape));
throw std::logic_error(std::string("Unhandled Bullet shape duplication: ") + shape->getName());
}
void deleteShape(btCollisionShape* shape)
{
if (shape->isCompound())
{
btCompoundShape* compound = static_cast<btCompoundShape*>(shape);
for (int i = 0, n = compound->getNumChildShapes(); i < n; i++)
if (btCollisionShape* child = compound->getChildShape(i))
deleteShape(child);
}
delete shape;
}
}
BulletShape::BulletShape()
: mCollisionShape(nullptr)
@ -28,60 +75,12 @@ BulletShape::BulletShape(const BulletShape &copy, const osg::CopyOp &copyop)
BulletShape::~BulletShape()
{
if (mAvoidCollisionShape != nullptr)
deleteShape(mAvoidCollisionShape);
if (mCollisionShape != nullptr)
deleteShape(mCollisionShape);
}
void BulletShape::deleteShape(btCollisionShape* shape)
{
if(shape!=nullptr)
{
if(shape->isCompound())
{
btCompoundShape* ms = static_cast<btCompoundShape*>(shape);
int a = ms->getNumChildShapes();
for(int i=0; i <a;i++)
deleteShape(ms->getChildShape(i));
}
delete shape;
}
}
btCollisionShape* BulletShape::duplicateCollisionShape(const btCollisionShape *shape) const
{
if(shape->isCompound())
{
const btCompoundShape *comp = static_cast<const btCompoundShape*>(shape);
btCompoundShape *newShape = new btCompoundShape;
int numShapes = comp->getNumChildShapes();
for(int i = 0;i < numShapes;++i)
{
btCollisionShape *child = duplicateCollisionShape(comp->getChildShape(i));
const btTransform& trans = comp->getChildTransform(i);
newShape->addChildShape(trans, child);
}
return newShape;
}
if(const btBvhTriangleMeshShape* trishape = dynamic_cast<const btBvhTriangleMeshShape*>(shape))
{
btScaledBvhTriangleMeshShape* newShape = new btScaledBvhTriangleMeshShape(const_cast<btBvhTriangleMeshShape*>(trishape), btVector3(1.f, 1.f, 1.f));
return newShape;
}
if (const btBoxShape* boxshape = dynamic_cast<const btBoxShape*>(shape))
{
return new btBoxShape(*boxshape);
}
if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
return new btHeightfieldTerrainShape(static_cast<const btHeightfieldTerrainShape&>(*shape));
throw std::logic_error(std::string("Unhandled Bullet shape duplication: ")+shape->getName());
}
btCollisionShape *BulletShape::getCollisionShape() const
{
return mCollisionShape;
@ -115,13 +114,8 @@ BulletShapeInstance::BulletShapeInstance(osg::ref_ptr<const BulletShape> source)
, mSource(source)
{
mCollisionBox = source->mCollisionBox;
mAnimatedShapes = source->mAnimatedShapes;
if (source->mCollisionShape)
mCollisionShape = duplicateCollisionShape(source->mCollisionShape);
if (source->mAvoidCollisionShape)
mAvoidCollisionShape = duplicateCollisionShape(source->mAvoidCollisionShape);
}

View File

@ -44,8 +44,6 @@ namespace Resource
osg::ref_ptr<BulletShapeInstance> makeInstance() const;
btCollisionShape* duplicateCollisionShape(const btCollisionShape* shape) const;
btCollisionShape* getCollisionShape() const;
btCollisionShape* getAvoidCollisionShape() const;
@ -53,10 +51,6 @@ namespace Resource
void setLocalScaling(const btVector3& scale);
bool isAnimated() const;
private:
void deleteShape(btCollisionShape* shape);
};