mirror of
https://github.com/love2d/love-android.git
synced 2026-08-20 12:40:28 +02:00
Using latest love-android branch (e263d91d136d)
This commit is contained in:
@@ -37,10 +37,14 @@ namespace box2d
|
||||
|
||||
Body::Body(World *world, b2Vec2 p, Body::Type type)
|
||||
: world(world)
|
||||
, udata(nullptr)
|
||||
{
|
||||
udata = new bodyudata();
|
||||
udata->ref = nullptr;
|
||||
world->retain();
|
||||
b2BodyDef def;
|
||||
def.position = Physics::scaleDown(p);
|
||||
def.userData = (void *) udata;
|
||||
body = world->world->CreateBody(&def);
|
||||
// Box2D body holds a reference to the love Body.
|
||||
this->retain();
|
||||
@@ -50,7 +54,9 @@ Body::Body(World *world, b2Vec2 p, Body::Type type)
|
||||
|
||||
Body::Body(b2Body *b)
|
||||
: body(b)
|
||||
, udata(nullptr)
|
||||
{
|
||||
udata = (bodyudata *) b->GetUserData();
|
||||
world = (World *)Memoizer::find(b->GetWorld());
|
||||
world->retain();
|
||||
// Box2D body holds a reference to the love Body.
|
||||
@@ -60,8 +66,10 @@ Body::Body(b2Body *b)
|
||||
|
||||
Body::~Body()
|
||||
{
|
||||
if (udata != nullptr)
|
||||
delete udata->ref;
|
||||
delete udata;
|
||||
world->release();
|
||||
body = 0;
|
||||
}
|
||||
|
||||
float Body::getX()
|
||||
@@ -469,6 +477,40 @@ void Body::destroy()
|
||||
this->release();
|
||||
}
|
||||
|
||||
int Body::setUserData(lua_State *L)
|
||||
{
|
||||
love::luax_assert_argc(L, 1, 1);
|
||||
|
||||
if (udata == nullptr)
|
||||
{
|
||||
udata = new bodyudata();
|
||||
body->SetUserData((void *) udata);
|
||||
}
|
||||
|
||||
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 Body::getUserData(lua_State *L)
|
||||
{
|
||||
if (udata != nullptr && udata->ref != nullptr)
|
||||
udata->ref->push(L);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
} // love
|
||||
|
||||
@@ -41,6 +41,16 @@ 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* A Body is an entity which has position and orientation
|
||||
* in world space. A Body does have collision geometry
|
||||
@@ -388,6 +398,18 @@ public:
|
||||
**/
|
||||
void destroy();
|
||||
|
||||
/**
|
||||
* This function stores an in-C reference to
|
||||
* arbitrary Lua data in the Box2D Body object.
|
||||
**/
|
||||
int setUserData(lua_State *L);
|
||||
|
||||
/**
|
||||
* Gets the data set with setData. If no
|
||||
* data is set, nil is returned.
|
||||
**/
|
||||
int getUserData(lua_State *L);
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
@@ -408,7 +430,10 @@ private:
|
||||
// This ensures that a World only can be destroyed
|
||||
// once all bodies have been destroyed too.
|
||||
World *world;
|
||||
};
|
||||
|
||||
bodyudata *udata;
|
||||
|
||||
}; // Body
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
|
||||
@@ -39,10 +39,10 @@ namespace box2d
|
||||
|
||||
Fixture::Fixture(Body *body, Shape *shape, float density)
|
||||
: body(body)
|
||||
, fixture(NULL)
|
||||
, fixture(nullptr)
|
||||
{
|
||||
data = new fixtureudata();
|
||||
data->ref = 0;
|
||||
data->ref = nullptr;
|
||||
b2FixtureDef def;
|
||||
def.shape = shape->shape;
|
||||
def.userData = (void *)data;
|
||||
@@ -65,13 +65,10 @@ Fixture::Fixture(b2Fixture *f)
|
||||
|
||||
Fixture::~Fixture()
|
||||
{
|
||||
if (data->ref != 0)
|
||||
if (data != nullptr)
|
||||
delete data->ref;
|
||||
|
||||
delete data;
|
||||
data = NULL;
|
||||
|
||||
fixture = NULL;
|
||||
}
|
||||
|
||||
Shape::Type Fixture::getType() const
|
||||
@@ -127,14 +124,14 @@ Body *Fixture::getBody() const
|
||||
Shape *Fixture::getShape() const
|
||||
{
|
||||
if (!fixture->GetShape())
|
||||
return NULL;
|
||||
return nullptr;
|
||||
|
||||
return new Shape(fixture->GetShape(), false);
|
||||
}
|
||||
|
||||
bool Fixture::isValid() const
|
||||
{
|
||||
return fixture != 0;
|
||||
return fixture != nullptr;
|
||||
}
|
||||
|
||||
void Fixture::setFilterData(int *v)
|
||||
@@ -205,7 +202,7 @@ uint16 Fixture::getBits(lua_State *L)
|
||||
{
|
||||
size_t bpos = (size_t)(lua_tointeger(L, i)-1);
|
||||
if (bpos >= 16)
|
||||
return luaL_error(L, "Values must be in range 1-16.");
|
||||
luaL_error(L, "Values must be in range 1-16.");
|
||||
b.set(bpos, true);
|
||||
}
|
||||
|
||||
@@ -230,21 +227,24 @@ int Fixture::setUserData(lua_State *L)
|
||||
{
|
||||
love::luax_assert_argc(L, 1, 1);
|
||||
|
||||
if (data->ref != 0)
|
||||
if (data->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.
|
||||
data->ref->setL(L);
|
||||
delete data->ref;
|
||||
data->ref = 0;
|
||||
}
|
||||
|
||||
data->ref = new Reference(L);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Fixture::getUserData(lua_State *L)
|
||||
{
|
||||
love::luax_assert_argc(L, 0, 0);
|
||||
if (data->ref != 0)
|
||||
data->ref->push();
|
||||
if (data->ref != nullptr)
|
||||
data->ref->push(L);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
|
||||
@@ -311,10 +311,10 @@ void Fixture::destroy(bool implicit)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!implicit && fixture != 0)
|
||||
if (!implicit && fixture != nullptr)
|
||||
body->body->DestroyFixture(fixture);
|
||||
Memoizer::remove(fixture);
|
||||
fixture = NULL;
|
||||
fixture = nullptr;
|
||||
|
||||
// Box2D fixture destroyed. Release its reference to the love Fixture.
|
||||
this->release();
|
||||
|
||||
@@ -123,9 +123,6 @@ public:
|
||||
/**
|
||||
* This function stores an in-C reference to
|
||||
* arbitrary Lua data in the Box2D Fixture object.
|
||||
*
|
||||
* The data set here will be passed to the collision
|
||||
* handler when collisions occur.
|
||||
**/
|
||||
int setUserData(lua_State *L);
|
||||
|
||||
|
||||
@@ -538,6 +538,20 @@ int w_Body_destroy(lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_Body_setUserData(lua_State *L)
|
||||
{
|
||||
Body *t = luax_checkbody(L, 1);
|
||||
lua_remove(L, 1);
|
||||
return t->setUserData(L);
|
||||
}
|
||||
|
||||
int w_Body_getUserData(lua_State *L)
|
||||
{
|
||||
Body *t = luax_checkbody(L, 1);
|
||||
lua_remove(L, 1);
|
||||
return t->getUserData(L);
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "getX", w_Body_getX },
|
||||
@@ -592,6 +606,8 @@ static const luaL_Reg functions[] =
|
||||
{ "isFixedRotation", w_Body_isFixedRotation },
|
||||
{ "getFixtureList", w_Body_getFixtureList },
|
||||
{ "destroy", w_Body_destroy },
|
||||
{ "setUserData", w_Body_setUserData },
|
||||
{ "getUserData", w_Body_getUserData },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
@@ -85,6 +85,8 @@ int w_Body_setFixedRotation(lua_State *L);
|
||||
int w_Body_isFixedRotation(lua_State *L);
|
||||
int w_Body_getFixtureList(lua_State *L);
|
||||
int w_Body_destroy(lua_State *L);
|
||||
int w_Body_setUserData(lua_State *L);
|
||||
int w_Body_getUserData(lua_State *L);
|
||||
extern "C" int luaopen_body(lua_State *L);
|
||||
|
||||
} // box2d
|
||||
|
||||
@@ -140,4 +140,4 @@ extern "C" int luaopen_motorjoint(lua_State *L)
|
||||
|
||||
} // box2d
|
||||
} // phyics
|
||||
} // love
|
||||
} // love
|
||||
|
||||
@@ -143,21 +143,27 @@ int w_World_getBodyList(lua_State *L)
|
||||
{
|
||||
World *t = luax_checkworld(L, 1);
|
||||
lua_remove(L, 1);
|
||||
return t->getBodyList(L);
|
||||
int ret = 0;
|
||||
EXCEPT_GUARD(ret = t->getBodyList(L);)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int w_World_getJointList(lua_State *L)
|
||||
{
|
||||
World *t = luax_checkworld(L, 1);
|
||||
lua_remove(L, 1);
|
||||
return t->getJointList(L);
|
||||
int ret = 0;
|
||||
EXCEPT_GUARD(ret = t->getJointList(L);)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int w_World_getContactList(lua_State *L)
|
||||
{
|
||||
World *t = luax_checkworld(L, 1);
|
||||
lua_remove(L, 1);
|
||||
return t->getContactList(L);
|
||||
int ret = 0;
|
||||
EXCEPT_GUARD(ret = t->getContactList(L);)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int w_World_queryBoundingBox(lua_State *L)
|
||||
|
||||
Reference in New Issue
Block a user