Hack box2d for jsb2.0

This commit is contained in:
James Chen 2017-08-18 14:02:10 +08:00
parent 5c90ba54d3
commit 306402354e
6 changed files with 27 additions and 10 deletions

View File

@ -120,7 +120,7 @@ void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount); b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount); b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
b2NotifyObjectDestroyed(contact, typeid(*contact).name()); b2NotifyObjectDestroyed(contact, b2ObjectType::CONTACT, typeid(*contact).name());
b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn; b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
destroyFcn(contact, allocator); destroyFcn(contact, allocator);
} }

View File

@ -128,7 +128,7 @@ b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)
void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator) void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
{ {
b2NotifyObjectDestroyed(joint, typeid(*joint).name()); b2NotifyObjectDestroyed(joint, b2ObjectType::JOIN, typeid(*joint).name());
joint->~b2Joint(); joint->~b2Joint();
switch (joint->m_type) switch (joint->m_type)
{ {

View File

@ -26,6 +26,7 @@
#include <Box2D/Collision/b2BroadPhase.h> #include <Box2D/Collision/b2BroadPhase.h>
#include <Box2D/Collision/b2Collision.h> #include <Box2D/Collision/b2Collision.h>
#include <Box2D/Common/b2BlockAllocator.h> #include <Box2D/Common/b2BlockAllocator.h>
#include <Box2D/b2ObjectDestroyNotifier.h>
b2Fixture::b2Fixture() b2Fixture::b2Fixture()
{ {
@ -82,6 +83,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_circle: case b2Shape::e_circle:
{ {
b2CircleShape* s = (b2CircleShape*)m_shape; b2CircleShape* s = (b2CircleShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::CIRCLE_SHAPE, "b2CircleShape");
s->~b2CircleShape(); s->~b2CircleShape();
allocator->Free(s, sizeof(b2CircleShape)); allocator->Free(s, sizeof(b2CircleShape));
} }
@ -90,6 +92,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_edge: case b2Shape::e_edge:
{ {
b2EdgeShape* s = (b2EdgeShape*)m_shape; b2EdgeShape* s = (b2EdgeShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::EDGE_SHAPE, "b2EdgeShape");
s->~b2EdgeShape(); s->~b2EdgeShape();
allocator->Free(s, sizeof(b2EdgeShape)); allocator->Free(s, sizeof(b2EdgeShape));
} }
@ -98,6 +101,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_polygon: case b2Shape::e_polygon:
{ {
b2PolygonShape* s = (b2PolygonShape*)m_shape; b2PolygonShape* s = (b2PolygonShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::POLYGON_SHAPE, "b2PolygonShape");
s->~b2PolygonShape(); s->~b2PolygonShape();
allocator->Free(s, sizeof(b2PolygonShape)); allocator->Free(s, sizeof(b2PolygonShape));
} }
@ -106,6 +110,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_chain: case b2Shape::e_chain:
{ {
b2ChainShape* s = (b2ChainShape*)m_shape; b2ChainShape* s = (b2ChainShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::CHAIN_SHAPE, "b2ChainShape");
s->~b2ChainShape(); s->~b2ChainShape();
allocator->Free(s, sizeof(b2ChainShape)); allocator->Free(s, sizeof(b2ChainShape));
} }
@ -117,6 +122,8 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
} }
m_shape = NULL; m_shape = NULL;
b2NotifyObjectDestroyed(this, b2ObjectType::FIXTURE, "b2Fixture");
} }
void b2Fixture::CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf) void b2Fixture::CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf)

View File

@ -206,7 +206,7 @@ void b2World::DestroyBody(b2Body* b)
} }
--m_bodyCount; --m_bodyCount;
b2NotifyObjectDestroyed(b, "b2Body"); b2NotifyObjectDestroyed(b, b2ObjectType::BODY, "b2Body");
b->~b2Body(); b->~b2Body();
m_blockAllocator.Free(b, sizeof(b2Body)); m_blockAllocator.Free(b, sizeof(b2Body));
} }

View File

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

View File

@ -1,8 +1,18 @@
#pragma once #pragma once
#include <stdlib.h> enum class b2ObjectType
{
CONTACT,
CIRCLE_SHAPE,
EDGE_SHAPE,
POLYGON_SHAPE,
CHAIN_SHAPE,
FIXTURE,
JOIN,
BODY
};
typedef void (*b2ObjectDestroyNotifer)(void*, const char*); typedef void (*b2ObjectDestroyNotifer)(void*, b2ObjectType, const char*);
void b2SetObjectDestroyNotifier(b2ObjectDestroyNotifer notifier); void b2SetObjectDestroyNotifier(b2ObjectDestroyNotifer notifier);
void b2NotifyObjectDestroyed(void* obj, const char* typeName = NULL); void b2NotifyObjectDestroyed(void* obj, b2ObjectType type, const char* typeName);