added cleanup memory for BasicDemo, and another pragma warning ignore

This commit is contained in:
ejcoumans
2006-10-13 23:24:09 +00:00
parent ccb7a311b2
commit 2c66be25fc
3 changed files with 99 additions and 16 deletions

View File

@@ -34,10 +34,14 @@ GLDebugDrawer debugDrawer;
int main(int argc,char** argv) int main(int argc,char** argv)
{ {
BasicDemo* ccdDemo = new BasicDemo(); BasicDemo ccdDemo;
ccdDemo->initPhysics(); ccdDemo.initPhysics();
ccdDemo->setCameraDistance(50.f); ccdDemo.setCameraDistance(50.f);
return glutmain(argc, argv,640,480,"Bullet Physics Demo. http://bullet.sf.net",ccdDemo); ccdDemo.exitPhysics();
//return glutmain(argc, argv,640,480,"Bullet Physics Demo. http://bullet.sf.net",&ccdDemo);
return 0;
} }
@@ -74,6 +78,7 @@ void BasicDemo::displayCallback(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (m_dynamicsWorld)
m_dynamicsWorld->updateAabbs(); m_dynamicsWorld->updateAabbs();
renderme(); renderme();
@@ -95,27 +100,31 @@ void BasicDemo::clientResetScene()
void BasicDemo::initPhysics() void BasicDemo::initPhysics()
{ {
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(true);
m_dispatcher = new btCollisionDispatcher(true);
#ifdef USE_SWEEP_AND_PRUNE #ifdef USE_SWEEP_AND_PRUNE
btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000); btVector3 worldAabbMax(10000,10000,10000);
btOverlappingPairCache* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies); m_overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
#else #else
btOverlappingPairCache* broadphase = new btSimpleBroadphase; m_overlappingPairCache = new btSimpleBroadphase;
#endif //USE_SWEEP_AND_PRUNE #endif //USE_SWEEP_AND_PRUNE
dispatcher->registerCollisionCreateFunc(SPHERE_SHAPE_PROXYTYPE,SPHERE_SHAPE_PROXYTYPE,new btSphereSphereCollisionAlgorithm::CreateFunc); m_sphereSphereCF = new btSphereSphereCollisionAlgorithm::CreateFunc;
m_dispatcher->registerCollisionCreateFunc(SPHERE_SHAPE_PROXYTYPE,SPHERE_SHAPE_PROXYTYPE,m_sphereSphereCF);
#ifdef USE_GROUND_BOX #ifdef USE_GROUND_BOX
dispatcher->registerCollisionCreateFunc(SPHERE_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,new btSphereBoxCollisionAlgorithm::CreateFunc); m_sphereBoxCF = new btSphereBoxCollisionAlgorithm::CreateFunc;
btCollisionAlgorithmCreateFunc* swappedSphereBox = new btSphereBoxCollisionAlgorithm::CreateFunc; m_boxSphereCF = new btSphereBoxCollisionAlgorithm::CreateFunc;
swappedSphereBox->m_swapped = true; m_boxSphereCF->m_swapped = true;
dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,SPHERE_SHAPE_PROXYTYPE,swappedSphereBox); m_dispatcher->registerCollisionCreateFunc(SPHERE_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,m_sphereBoxCF);
m_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,SPHERE_SHAPE_PROXYTYPE,m_boxSphereCF);
#endif //USE_GROUND_BOX #endif //USE_GROUND_BOX
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver; m_solver = new btSequentialImpulseConstraintSolver;
m_dynamicsWorld = new btSimpleDynamicsWorld(dispatcher,broadphase,solver); m_dynamicsWorld = new btSimpleDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver);
m_dynamicsWorld->setGravity(btVector3(0,-10,0)); m_dynamicsWorld->setGravity(btVector3(0,-10,0));
m_dynamicsWorld->setDebugDrawer(&debugDrawer); m_dynamicsWorld->setDebugDrawer(&debugDrawer);
@@ -130,6 +139,8 @@ void BasicDemo::initPhysics()
btCollisionShape* groundShape = new btSphereShape(50.f); btCollisionShape* groundShape = new btSphereShape(50.f);
#endif//USE_GROUND_BOX #endif//USE_GROUND_BOX
m_collisionShapes.push_back(groundShape);
btTransform groundTransform; btTransform groundTransform;
groundTransform.setIdentity(); groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0,-50,0)); groundTransform.setOrigin(btVector3(0,-50,0));
@@ -137,6 +148,8 @@ void BasicDemo::initPhysics()
//create a few dynamic sphere rigidbodies (re-using the same sphere shape) //create a few dynamic sphere rigidbodies (re-using the same sphere shape)
btCollisionShape* sphereShape = new btSphereShape(1.f); btCollisionShape* sphereShape = new btSphereShape(1.f);
m_collisionShapes.push_back(sphereShape);
int i; int i;
for (i=0;i<gNumObjects;i++) for (i=0;i<gNumObjects;i++)
{ {
@@ -160,5 +173,50 @@ void BasicDemo::initPhysics()
} }
void BasicDemo::exitPhysics()
{
//cleanup in the reverse order of creation/initialization
//remove the rigidbodies from the dynamics world and delete them
int i;
for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
m_dynamicsWorld->removeCollisionObject( obj );
delete obj;
}
//delete collision shapes
for (i=0;i<m_collisionShapes.size();i++)
{
btCollisionShape* shape = m_collisionShapes[i];
delete shape;
}
//delete dynamics world
delete m_dynamicsWorld;
//delete collision algorithms creation functions
delete m_sphereSphereCF;
#ifdef USE_GROUND_BOX
delete m_sphereBoxCF;
delete m_boxSphereCF;
#endif// USE_GROUND_BOX
//delete solver
delete m_solver;
//delete broadphase
delete m_overlappingPairCache;
//delete dispatcher
delete m_dispatcher;
}

View File

@@ -17,13 +17,37 @@ subject to the following restrictions:
#include "DemoApplication.h" #include "DemoApplication.h"
#include <vector>
class btCollisionShape;
class btOverlappingPairCache;
class btCollisionDispatcher;
class btConstraintSolver;
struct btCollisionAlgorithmCreateFunc;
///BasicDemo is good starting point for learning the code base and porting. ///BasicDemo is good starting point for learning the code base and porting.
class BasicDemo : public DemoApplication class BasicDemo : public DemoApplication
{ {
//keep the collision shapes, for deletion/cleanup
std::vector<btCollisionShape*> m_collisionShapes;
btOverlappingPairCache* m_overlappingPairCache;
btCollisionDispatcher* m_dispatcher;
btConstraintSolver* m_solver;
btCollisionAlgorithmCreateFunc* m_sphereSphereCF;
btCollisionAlgorithmCreateFunc* m_sphereBoxCF;
btCollisionAlgorithmCreateFunc* m_boxSphereCF;
public: public:
void initPhysics(); void initPhysics();
void exitPhysics();
virtual void clientMoveAndDisplay(); virtual void clientMoveAndDisplay();
virtual void displayCallback(); virtual void displayCallback();

View File

@@ -30,6 +30,7 @@ subject to the following restrictions:
#else #else
#pragma warning(disable:4530) #pragma warning(disable:4530)
#pragma warning(disable:4996) #pragma warning(disable:4996)
#pragma warning(disable:4786)
#define SIMD_FORCE_INLINE __forceinline #define SIMD_FORCE_INLINE __forceinline
#endif //__MINGW32__ #endif //__MINGW32__