Skip to content

Commit

Permalink
Merge pull request #38 from dumganhar/jsb2.0-box2d
Browse files Browse the repository at this point in the history
Hack box2d for jsb2.0
  • Loading branch information
James Chen authored Aug 18, 2017
2 parents cd9b2c7 + 3064023 commit 4ea54c1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sources/Box2D/Box2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions sources/Box2D/Dynamics/Contacts/b2Contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, b2ObjectType::CONTACT, typeid(*contact).name());
b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
destroyFcn(contact, allocator);
}
Expand Down
2 changes: 2 additions & 0 deletions sources/Box2D/Dynamics/Joints/b2Joint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -127,6 +128,7 @@ b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator)

void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator)
{
b2NotifyObjectDestroyed(joint, b2ObjectType::JOIN, typeid(*joint).name());
joint->~b2Joint();
switch (joint->m_type)
{
Expand Down
7 changes: 7 additions & 0 deletions sources/Box2D/Dynamics/b2Fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <Box2D/Collision/b2BroadPhase.h>
#include <Box2D/Collision/b2Collision.h>
#include <Box2D/Common/b2BlockAllocator.h>
#include <Box2D/b2ObjectDestroyNotifier.h>

b2Fixture::b2Fixture()
{
Expand Down Expand Up @@ -82,6 +83,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_circle:
{
b2CircleShape* s = (b2CircleShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::CIRCLE_SHAPE, "b2CircleShape");
s->~b2CircleShape();
allocator->Free(s, sizeof(b2CircleShape));
}
Expand All @@ -90,6 +92,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_edge:
{
b2EdgeShape* s = (b2EdgeShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::EDGE_SHAPE, "b2EdgeShape");
s->~b2EdgeShape();
allocator->Free(s, sizeof(b2EdgeShape));
}
Expand All @@ -98,6 +101,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_polygon:
{
b2PolygonShape* s = (b2PolygonShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::POLYGON_SHAPE, "b2PolygonShape");
s->~b2PolygonShape();
allocator->Free(s, sizeof(b2PolygonShape));
}
Expand All @@ -106,6 +110,7 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
case b2Shape::e_chain:
{
b2ChainShape* s = (b2ChainShape*)m_shape;
b2NotifyObjectDestroyed(s, b2ObjectType::CHAIN_SHAPE, "b2ChainShape");
s->~b2ChainShape();
allocator->Free(s, sizeof(b2ChainShape));
}
Expand All @@ -117,6 +122,8 @@ void b2Fixture::Destroy(b2BlockAllocator* allocator)
}

m_shape = NULL;

b2NotifyObjectDestroyed(this, b2ObjectType::FIXTURE, "b2Fixture");
}

void b2Fixture::CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf)
Expand Down
2 changes: 2 additions & 0 deletions sources/Box2D/Dynamics/b2World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -205,6 +206,7 @@ void b2World::DestroyBody(b2Body* b)
}

--m_bodyCount;
b2NotifyObjectDestroyed(b, b2ObjectType::BODY, "b2Body");
b->~b2Body();
m_blockAllocator.Free(b, sizeof(b2Body));
}
Expand Down
16 changes: 16 additions & 0 deletions sources/Box2D/b2ObjectDestroyNotifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "b2ObjectDestroyNotifier.h"

static b2ObjectDestroyNotifer __objectDestroyNotifier = nullptr;

void b2SetObjectDestroyNotifier(b2ObjectDestroyNotifer notifier)
{
__objectDestroyNotifier = notifier;
}

void b2NotifyObjectDestroyed(void* obj, b2ObjectType type, const char* typeName)
{
if (__objectDestroyNotifier != nullptr)
{
__objectDestroyNotifier(obj, type, typeName);
}
}
18 changes: 18 additions & 0 deletions sources/Box2D/b2ObjectDestroyNotifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

enum class b2ObjectType
{
CONTACT,
CIRCLE_SHAPE,
EDGE_SHAPE,
POLYGON_SHAPE,
CHAIN_SHAPE,
FIXTURE,
JOIN,
BODY
};

typedef void (*b2ObjectDestroyNotifer)(void*, b2ObjectType, const char*);

void b2SetObjectDestroyNotifier(b2ObjectDestroyNotifer notifier);
void b2NotifyObjectDestroyed(void* obj, b2ObjectType type, const char* typeName);

0 comments on commit 4ea54c1

Please sign in to comment.