Hack code in Box2D to notify the destruction of native object.

This commit is contained in:
James Chen 2017-08-16 15:43:29 +08:00
parent cd9b2c75e4
commit 5c90ba54d3
6 changed files with 32 additions and 0 deletions

View File

@ -65,4 +65,6 @@ For discussion please visit http://box2d.org/forum
#include <Box2D/Dynamics/Joints/b2WeldJoint.h>
#include <Box2D/Dynamics/Joints/b2WheelJoint.h>
#include <Box2D/b2ObjectDestroyNotifier.h>
#endif

View File

@ -33,6 +33,7 @@
#include <Box2D/Dynamics/b2Body.h>
#include <Box2D/Dynamics/b2Fixture.h>
#include <Box2D/Dynamics/b2World.h>
#include <Box2D/b2ObjectDestroyNotifier.h>
b2ContactRegister b2Contact::s_registers[b2Shape::e_typeCount][b2Shape::e_typeCount];
bool b2Contact::s_initialized = false;
@ -119,6 +120,7 @@ void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
b2NotifyObjectDestroyed(contact, typeid(*contact).name());
b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
destroyFcn(contact, allocator);
}

View File

@ -31,6 +31,7 @@
#include <Box2D/Dynamics/b2Body.h>
#include <Box2D/Dynamics/b2World.h>
#include <Box2D/Common/b2BlockAllocator.h>
#include <Box2D/b2ObjectDestroyNotifier.h>
#include <new>
@ -127,6 +128,7 @@ b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
{
b2NotifyObjectDestroyed(joint, typeid(*joint).name());
joint->~b2Joint();
switch (joint->m_type)
{

View File

@ -32,6 +32,7 @@
#include <Box2D/Collision/b2TimeOfImpact.h>
#include <Box2D/Common/b2Draw.h>
#include <Box2D/Common/b2Timer.h>
#include <Box2D/b2ObjectDestroyNotifier.h>
#include <new>
b2World::b2World(const b2Vec2& gravity)
@ -205,6 +206,7 @@ void b2World::DestroyBody(b2Body* b)
}
--m_bodyCount;
b2NotifyObjectDestroyed(b, "b2Body");
b->~b2Body();
m_blockAllocator.Free(b, sizeof(b2Body));
}

View File

@ -0,0 +1,16 @@
#include "b2ObjectDestroyNotifier.h"
static b2ObjectDestroyNotifer __objectDestroyNotifier = NULL;
void b2SetObjectDestroyNotifier(b2ObjectDestroyNotifer notifier)
{
__objectDestroyNotifier = notifier;
}
void b2NotifyObjectDestroyed(void* obj, const char* typeName /* = NULL */)
{
if (__objectDestroyNotifier != NULL)
{
__objectDestroyNotifier(obj, typeName);
}
}

View File

@ -0,0 +1,8 @@
#pragma once
#include <stdlib.h>
typedef void (*b2ObjectDestroyNotifer)(void*, const char*);
void b2SetObjectDestroyNotifier(b2ObjectDestroyNotifer notifier);
void b2NotifyObjectDestroyed(void* obj, const char* typeName = NULL);