Added Joint:setUserData and Joint:getUserData (resolves issue #894.)

This commit is contained in:
Alex Szpakowski
2014-06-05 20:56:43 -03:00
parent 3792dee4b4
commit 5dd23dffd1
15 changed files with 102 additions and 1 deletions
+39 -1
View File
@@ -41,20 +41,29 @@ namespace box2d
Joint::Joint(Body *body1)
: world(body1->world)
, udata(nullptr)
, body1(body1)
, body2(0)
, 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 != nullptr)
delete udata->ref;
delete udata;
}
Joint::Type Joint::getType() const
@@ -116,6 +125,7 @@ float Joint::getReactionTorque(float dt)
b2Joint *Joint::createJoint(b2JointDef *def)
{
def->userData = udata;
joint = world->world->CreateJoint(def);
Memoizer::add(joint, this);
// Box2D joint has a reference to this love Joint.
@@ -151,6 +161,34 @@ bool Joint::getCollideConnected() const
return joint->GetCollideConnected();
}
int Joint::setUserData(lua_State *L)
{
love::luax_assert_argc(L, 1, 1);
if (udata->ref != nullptr)
{
// We set the Reference's lua_State to this one before deleting it, so
// it unrefs using the current lua_State's stack. This is necessary
// if setUserData is called in a coroutine.
udata->ref->setL(L);
delete udata->ref;
}
udata->ref = new Reference(L);
return 0;
}
int Joint::getUserData(lua_State *L)
{
if (udata != nullptr && udata->ref != nullptr)
udata->ref->push(L);
else
lua_pushnil(L);
return 1;
}
} // box2d
} // physics
} // love