mirror of
https://github.com/love2d/love.git
synced 2026-08-20 04:30:09 +02:00
Added Body:setUserData and Body:getUserData (resolves issue #853_
This commit is contained in:
@@ -26,7 +26,7 @@ namespace love
|
|||||||
const char REFERENCE_TABLE_NAME[] = "love-references";
|
const char REFERENCE_TABLE_NAME[] = "love-references";
|
||||||
|
|
||||||
Reference::Reference()
|
Reference::Reference()
|
||||||
: L(0)
|
: L(nullptr)
|
||||||
, idx(LUA_REFNIL)
|
, idx(LUA_REFNIL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ struct FramebufferStrategyGL3 : public FramebufferStrategy
|
|||||||
glGenRenderbuffers(1, &stencil);
|
glGenRenderbuffers(1, &stencil);
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, stencil);
|
glBindRenderbuffer(GL_RENDERBUFFER, stencil);
|
||||||
|
|
||||||
if (samples > 0)
|
if (samples > 1)
|
||||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH_STENCIL, width, height);
|
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH_STENCIL, width, height);
|
||||||
else
|
else
|
||||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, width, height);
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, width, height);
|
||||||
@@ -249,7 +249,7 @@ struct FramebufferStrategyPackedEXT : public FramebufferStrategy
|
|||||||
glGenRenderbuffersEXT(1, &stencil);
|
glGenRenderbuffersEXT(1, &stencil);
|
||||||
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
|
||||||
|
|
||||||
if (samples > 0)
|
if (samples > 1)
|
||||||
{
|
{
|
||||||
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, samples,
|
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, samples,
|
||||||
GL_DEPTH_STENCIL, width, height);
|
GL_DEPTH_STENCIL, width, height);
|
||||||
@@ -358,7 +358,7 @@ struct FramebufferStrategyEXT : public FramebufferStrategyPackedEXT
|
|||||||
glGenRenderbuffersEXT(1, &stencil);
|
glGenRenderbuffersEXT(1, &stencil);
|
||||||
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencil);
|
||||||
|
|
||||||
if (samples > 0)
|
if (samples > 1)
|
||||||
{
|
{
|
||||||
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, samples,
|
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER, samples,
|
||||||
GL_STENCIL_INDEX, width, height);
|
GL_STENCIL_INDEX, width, height);
|
||||||
|
|||||||
@@ -37,10 +37,14 @@ namespace box2d
|
|||||||
|
|
||||||
Body::Body(World *world, b2Vec2 p, Body::Type type)
|
Body::Body(World *world, b2Vec2 p, Body::Type type)
|
||||||
: world(world)
|
: world(world)
|
||||||
|
, udata(nullptr)
|
||||||
{
|
{
|
||||||
|
udata = new bodyudata();
|
||||||
|
udata->ref = nullptr;
|
||||||
world->retain();
|
world->retain();
|
||||||
b2BodyDef def;
|
b2BodyDef def;
|
||||||
def.position = Physics::scaleDown(p);
|
def.position = Physics::scaleDown(p);
|
||||||
|
def.userData = (void *) udata;
|
||||||
body = world->world->CreateBody(&def);
|
body = world->world->CreateBody(&def);
|
||||||
// Box2D body holds a reference to the love Body.
|
// Box2D body holds a reference to the love Body.
|
||||||
this->retain();
|
this->retain();
|
||||||
@@ -50,7 +54,9 @@ Body::Body(World *world, b2Vec2 p, Body::Type type)
|
|||||||
|
|
||||||
Body::Body(b2Body *b)
|
Body::Body(b2Body *b)
|
||||||
: body(b)
|
: body(b)
|
||||||
|
, udata(nullptr)
|
||||||
{
|
{
|
||||||
|
udata = (bodyudata *) b->GetUserData();
|
||||||
world = (World *)Memoizer::find(b->GetWorld());
|
world = (World *)Memoizer::find(b->GetWorld());
|
||||||
world->retain();
|
world->retain();
|
||||||
// Box2D body holds a reference to the love Body.
|
// Box2D body holds a reference to the love Body.
|
||||||
@@ -60,8 +66,10 @@ Body::Body(b2Body *b)
|
|||||||
|
|
||||||
Body::~Body()
|
Body::~Body()
|
||||||
{
|
{
|
||||||
|
if (udata != nullptr)
|
||||||
|
delete udata->ref;
|
||||||
|
delete udata;
|
||||||
world->release();
|
world->release();
|
||||||
body = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float Body::getX()
|
float Body::getX()
|
||||||
@@ -469,6 +477,40 @@ void Body::destroy()
|
|||||||
this->release();
|
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
|
} // box2d
|
||||||
} // physics
|
} // physics
|
||||||
} // love
|
} // love
|
||||||
|
|||||||
@@ -41,6 +41,16 @@ class World;
|
|||||||
class Shape;
|
class Shape;
|
||||||
class Fixture;
|
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
|
* A Body is an entity which has position and orientation
|
||||||
* in world space. A Body does have collision geometry
|
* in world space. A Body does have collision geometry
|
||||||
@@ -388,6 +398,18 @@ public:
|
|||||||
**/
|
**/
|
||||||
void destroy();
|
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:
|
private:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -408,7 +430,10 @@ private:
|
|||||||
// This ensures that a World only can be destroyed
|
// This ensures that a World only can be destroyed
|
||||||
// once all bodies have been destroyed too.
|
// once all bodies have been destroyed too.
|
||||||
World *world;
|
World *world;
|
||||||
};
|
|
||||||
|
bodyudata *udata;
|
||||||
|
|
||||||
|
}; // Body
|
||||||
|
|
||||||
} // box2d
|
} // box2d
|
||||||
} // physics
|
} // physics
|
||||||
|
|||||||
@@ -123,9 +123,6 @@ public:
|
|||||||
/**
|
/**
|
||||||
* This function stores an in-C reference to
|
* This function stores an in-C reference to
|
||||||
* arbitrary Lua data in the Box2D Fixture object.
|
* 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);
|
int setUserData(lua_State *L);
|
||||||
|
|
||||||
|
|||||||
@@ -538,6 +538,20 @@ int w_Body_destroy(lua_State *L)
|
|||||||
return 0;
|
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[] =
|
static const luaL_Reg functions[] =
|
||||||
{
|
{
|
||||||
{ "getX", w_Body_getX },
|
{ "getX", w_Body_getX },
|
||||||
@@ -592,6 +606,8 @@ static const luaL_Reg functions[] =
|
|||||||
{ "isFixedRotation", w_Body_isFixedRotation },
|
{ "isFixedRotation", w_Body_isFixedRotation },
|
||||||
{ "getFixtureList", w_Body_getFixtureList },
|
{ "getFixtureList", w_Body_getFixtureList },
|
||||||
{ "destroy", w_Body_destroy },
|
{ "destroy", w_Body_destroy },
|
||||||
|
{ "setUserData", w_Body_setUserData },
|
||||||
|
{ "getUserData", w_Body_getUserData },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,8 @@ int w_Body_setFixedRotation(lua_State *L);
|
|||||||
int w_Body_isFixedRotation(lua_State *L);
|
int w_Body_isFixedRotation(lua_State *L);
|
||||||
int w_Body_getFixtureList(lua_State *L);
|
int w_Body_getFixtureList(lua_State *L);
|
||||||
int w_Body_destroy(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);
|
extern "C" int luaopen_body(lua_State *L);
|
||||||
|
|
||||||
} // box2d
|
} // box2d
|
||||||
|
|||||||
Reference in New Issue
Block a user