Skip to content

Commit

Permalink
love.physics: simplify some internal bookkeeping code
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Oct 1, 2023
1 parent dbc7d7f commit b1609ed
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 137 deletions.
42 changes: 13 additions & 29 deletions src/modules/physics/box2d/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,20 @@ namespace box2d

Body::Body(World *world, b2Vec2 p, Body::Type type)
: world(world)
, udata(nullptr)
{
udata = new bodyudata();
udata->ref = nullptr;
b2BodyDef def;
def.position = Physics::scaleDown(p);
def.userData.pointer = (uintptr_t)udata;
def.userData.pointer = (uintptr_t)this;
body = world->world->CreateBody(&def);
// Box2D body holds a reference to the love Body.
this->retain();
this->setType(type);
world->registerObject(body, this);
}

Body::~Body()
{
if (!udata)
return;

if (udata->ref)
delete udata->ref;

delete udata;
if (ref)
delete ref;
}

float Body::getX()
Expand Down Expand Up @@ -479,7 +470,7 @@ int Body::getFixtures(lua_State *L) const
{
if (!f)
break;
Fixture *fixture = (Fixture *)world->findObject(f);
Fixture *fixture = (Fixture *)(f->GetUserData().pointer);
if (!fixture)
throw love::Exception("A fixture has escaped Memoizer!");
luax_pushtype(L, fixture);
Expand All @@ -501,7 +492,7 @@ int Body::getJoints(lua_State *L) const
if (!je)
break;

Joint *joint = (Joint *) world->findObject(je->joint);
Joint *joint = (Joint *) (je->joint->GetUserData().pointer);
if (!joint)
throw love::Exception("A joint has escaped Memoizer!");

Expand Down Expand Up @@ -550,12 +541,11 @@ void Body::destroy()
}

world->world->DestroyBody(body);
world->unregisterObject(body);
body = NULL;
body = nullptr;

// Remove userdata reference to avoid it sticking around after GC
if (udata && udata->ref)
udata->ref->unref();
if (ref)
ref->unref();

// Box2D body destroyed. Release its reference to the love Body.
this->release();
Expand All @@ -565,24 +555,18 @@ int Body::setUserData(lua_State *L)
{
love::luax_assert_argc(L, 1, 1);

if (udata == nullptr)
{
udata = new bodyudata();
body->GetUserData().pointer = (uintptr_t)udata;
}

if(!udata->ref)
udata->ref = new Reference();
if(!ref)
ref = new Reference();

udata->ref->ref(L);
ref->ref(L);

return 0;
}

int Body::getUserData(lua_State *L)
{
if (udata != nullptr && udata->ref != nullptr)
udata->ref->push(L);
if (ref != nullptr)
ref->push(L);
else
lua_pushnil(L);

Expand Down
13 changes: 2 additions & 11 deletions src/modules/physics/box2d/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ class World;
class Shape;
class Fixture;

/**
* This struct is stored in a void pointer in the Box2D Body class. For now, all
* we need is a Lua reference to arbitrary data, but we might need more later.
**/
struct bodyudata
{
// Reference to arbitrary data.
Reference *ref = nullptr;
};

/**
* A Body is an entity which has position and orientation
* in world space. A Body does have collision geometry
Expand Down Expand Up @@ -441,7 +431,8 @@ class Body : public love::physics::Body
// unowned?
World *world;

bodyudata *udata;
// Reference to arbitrary data.
Reference* ref = nullptr;

}; // Body

Expand Down
5 changes: 3 additions & 2 deletions src/modules/physics/box2d/Contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Contact::Contact(World *world, b2Contact *contact)
: contact(contact)
, world(world)
{
//contact->user
world->registerObject(contact, this);
}

Expand Down Expand Up @@ -145,8 +146,8 @@ void Contact::getChildren(int &childA, int &childB)

void Contact::getFixtures(Fixture *&fixtureA, Fixture *&fixtureB)
{
fixtureA = (Fixture *) world->findObject(contact->GetFixtureA());
fixtureB = (Fixture *) world->findObject(contact->GetFixtureB());
fixtureA = (Fixture *) (contact->GetFixtureA()->GetUserData().pointer);
fixtureB = (Fixture *) (contact->GetFixtureB()->GetUserData().pointer);

if (!fixtureA || !fixtureB)
throw love::Exception("A fixture has escaped Memoizer!");
Expand Down
35 changes: 10 additions & 25 deletions src/modules/physics/box2d/Fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,18 @@ Fixture::Fixture(Body *body, Shape *shape, float density)
: body(body)
, fixture(nullptr)
{
udata = new fixtureudata();
udata->ref = nullptr;
b2FixtureDef def;
def.shape = shape->shape;
def.userData.pointer = (uintptr_t)udata;
def.userData.pointer = (uintptr_t)this;
def.density = density;
fixture = body->body->CreateFixture(&def);
this->retain();
body->world->registerObject(fixture, this);
}

Fixture::~Fixture()
{
if (!udata)
return;

if (udata->ref)
delete udata->ref;

delete udata;
if (ref)
delete ref;
}

void Fixture::checkCreateShape()
Expand Down Expand Up @@ -259,24 +251,18 @@ int Fixture::setUserData(lua_State *L)
{
love::luax_assert_argc(L, 1, 1);

if (udata == nullptr)
{
udata = new fixtureudata();
fixture->GetUserData().pointer = (uintptr_t)udata;
}

if(!udata->ref)
udata->ref = new Reference();
if(!ref)
ref = new Reference();

udata->ref->ref(L);
ref->ref(L);

return 0;
}

int Fixture::getUserData(lua_State *L)
{
if (udata->ref != nullptr)
udata->ref->push(L);
if (ref != nullptr)
ref->push(L);
else
lua_pushnil(L);

Expand Down Expand Up @@ -348,12 +334,11 @@ void Fixture::destroy(bool implicit)

if (!implicit && fixture != nullptr)
body->body->DestroyFixture(fixture);
body->world->unregisterObject(fixture);
fixture = nullptr;

// Remove userdata reference to avoid it sticking around after GC
if (udata && udata->ref)
udata->ref->unref();
if (ref)
ref->unref();

// Box2D fixture destroyed. Release its reference to the love Fixture.
this->release();
Expand Down
16 changes: 3 additions & 13 deletions src/modules/physics/box2d/Fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ namespace box2d

class World;

/**
* This struct is stored in a void pointer
* in the Box2D Fixture class. For now, all we
* need is a Lua reference to arbitrary data,
* but we might need more later.
**/
struct fixtureudata
{
// Reference to arbitrary data.
Reference *ref = nullptr;
};

/**
* A Fixture is used to attach a shape to a body for collision detection.
* A Fixture inherits its transform from its parent. Fixtures hold
Expand Down Expand Up @@ -212,9 +200,11 @@ class Fixture : public Object
void checkCreateShape();

Body *body;
fixtureudata *udata;
b2Fixture *fixture;

// Reference to arbitrary data.
Reference* ref = nullptr;

StrongRef<Shape> shape;

};
Expand Down
4 changes: 2 additions & 2 deletions src/modules/physics/box2d/GearJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Joint *GearJoint::getJointA() const
if (b2joint == nullptr)
return nullptr;

Joint *j = (Joint *) world->findObject(b2joint);
Joint *j = (Joint *) (b2joint->GetUserData().pointer);
if (j == nullptr)
throw love::Exception("A joint has escaped Memoizer!");

Expand All @@ -82,7 +82,7 @@ Joint *GearJoint::getJointB() const
if (b2joint == nullptr)
return nullptr;

Joint *j = (Joint *) world->findObject(b2joint);
Joint *j = (Joint *) (b2joint->GetUserData().pointer);
if (j == nullptr)
throw love::Exception("A joint has escaped Memoizer!");

Expand Down
45 changes: 13 additions & 32 deletions src/modules/physics/box2d/Joint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,22 @@ namespace box2d

Joint::Joint(Body *body1)
: world(body1->world)
, udata(nullptr)
, body1(body1)
, body2(nullptr)
{
udata = new jointudata();
udata->ref = nullptr;
}

Joint::Joint(Body *body1, Body *body2)
: world(body1->world)
, udata(nullptr)
, body1(body1)
, body2(body2)
{
udata = new jointudata();
udata->ref = nullptr;
}

Joint::~Joint()
{
if (!udata)
return;

if (udata->ref)
delete udata->ref;

delete udata;
if (ref)
delete ref;
}

Joint::Type Joint::getType() const
Expand Down Expand Up @@ -104,7 +93,7 @@ Body *Joint::getBodyA() const
if (b2body == nullptr)
return nullptr;

Body *body = (Body *) world->findObject(b2body);
Body *body = (Body *) (b2body->GetUserData().pointer);
if (body == nullptr)
throw love::Exception("A body has escaped Memoizer!");

Expand All @@ -117,7 +106,7 @@ Body *Joint::getBodyB() const
if (b2body == nullptr)
return nullptr;

Body *body = (Body *) world->findObject(b2body);
Body *body = (Body *) (b2body->GetUserData().pointer);
if (body == nullptr)
throw love::Exception("A body has escaped Memoizer!");

Expand Down Expand Up @@ -154,9 +143,8 @@ float Joint::getReactionTorque(float dt)

b2Joint *Joint::createJoint(b2JointDef *def)
{
def->userData.pointer = (uintptr_t)udata;
def->userData.pointer = (uintptr_t)this;
joint = world->world->CreateJoint(def);
world->registerObject(joint, this);
// Box2D joint has a reference to this love Joint.
this->retain();
return joint;
Expand All @@ -174,12 +162,11 @@ void Joint::destroyJoint(bool implicit)

if (!implicit && joint != nullptr)
world->world->DestroyJoint(joint);
world->unregisterObject(joint);
joint = NULL;
joint = nullptr;

// Remove userdata reference to avoid it sticking around after GC
if (udata && udata->ref)
udata->ref->unref();
if (ref)
ref->unref();

// Release the reference of the Box2D joint.
this->release();
Expand All @@ -199,24 +186,18 @@ int Joint::setUserData(lua_State *L)
{
love::luax_assert_argc(L, 1, 1);

if (udata == nullptr)
{
udata = new jointudata();
joint->GetUserData().pointer = (uintptr_t)udata;
}

if(!udata->ref)
udata->ref = new Reference();
if(!ref)
ref = new Reference();

udata->ref->ref(L);
ref->ref(L);

return 0;
}

int Joint::getUserData(lua_State *L)
{
if (udata != nullptr && udata->ref != nullptr)
udata->ref->push(L);
if (ref != nullptr)
ref->push(L);
else
lua_pushnil(L);

Expand Down
Loading

0 comments on commit b1609ed

Please sign in to comment.