Updated the Lua wrapper code for modules to account for cases where the module's instance is removed from memory completely and recreated during the program's lifetime.

This commit is contained in:
Alex Szpakowski
2014-07-21 14:52:01 -03:00
parent e87fbdcaaf
commit 014b9a46e5
45 changed files with 432 additions and 316 deletions
+5
View File
@@ -33,6 +33,11 @@ namespace box2d
int Physics::meter = Physics::DEFAULT_METER;
Physics::Physics()
{
moduleType = M_PHYSICS;
}
const char *Physics::getName() const
{
return "love.physics.box2d";
+2
View File
@@ -61,6 +61,8 @@ public:
**/
static const int DEFAULT_METER = 30;
Physics();
// Implements Module.
const char *getName() const;
+26 -24
View File
@@ -50,6 +50,7 @@ namespace box2d
{
static Physics *instance = 0;
#define instance() (Module::getInstance<Physics>(Module::M_PHYSICS))
int w_newWorld(lua_State *L)
{
@@ -58,7 +59,7 @@ int w_newWorld(lua_State *L)
bool sleep = luax_optboolean(L, 3, true);
World *w;
luax_catchexcept(L, [&](){ w = instance->newWorld(gx, gy, sleep); });
luax_catchexcept(L, [&](){ w = instance()->newWorld(gx, gy, sleep); });
luax_pushtype(L, "World", PHYSICS_WORLD_T, w);
return 1;
@@ -76,7 +77,7 @@ int w_newBody(lua_State *L)
return luaL_error(L, "Invalid Body type: %s", typestr);
Body *body;
luax_catchexcept(L, [&](){ body = instance->newBody(world, x, y, btype); });
luax_catchexcept(L, [&](){ body = instance()->newBody(world, x, y, btype); });
luax_pushtype(L, "Body", PHYSICS_BODY_T, body);
return 1;
}
@@ -87,7 +88,7 @@ int w_newFixture(lua_State *L)
Shape *shape = luax_checkshape(L, 2);
float density = (float)luaL_optnumber(L, 3, 1.0f);
Fixture *fixture;
luax_catchexcept(L, [&](){ fixture = instance->newFixture(body, shape, density); });
luax_catchexcept(L, [&](){ fixture = instance()->newFixture(body, shape, density); });
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, fixture);
return 1;
}
@@ -100,7 +101,7 @@ int w_newCircleShape(lua_State *L)
{
float radius = (float)luaL_checknumber(L, 1);
CircleShape *shape;
luax_catchexcept(L, [&](){ shape = instance->newCircleShape(radius); });
luax_catchexcept(L, [&](){ shape = instance()->newCircleShape(radius); });
luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape);
return 1;
}
@@ -110,7 +111,7 @@ int w_newCircleShape(lua_State *L)
float y = (float)luaL_checknumber(L, 2);
float radius = (float)luaL_checknumber(L, 3);
CircleShape *shape;
luax_catchexcept(L, [&](){ shape = instance->newCircleShape(x, y, radius); });
luax_catchexcept(L, [&](){ shape = instance()->newCircleShape(x, y, radius); });
luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape);
return 1;
}
@@ -127,7 +128,7 @@ int w_newRectangleShape(lua_State *L)
float w = (float)luaL_checknumber(L, 1);
float h = (float)luaL_checknumber(L, 2);
PolygonShape *shape;
luax_catchexcept(L, [&](){ shape = instance->newRectangleShape(w, h); });
luax_catchexcept(L, [&](){ shape = instance()->newRectangleShape(w, h); });
luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape);
return 1;
}
@@ -139,7 +140,7 @@ int w_newRectangleShape(lua_State *L)
float h = (float)luaL_checknumber(L, 4);
float angle = (float)luaL_optnumber(L, 5, 0);
PolygonShape *shape;
luax_catchexcept(L, [&](){ shape = instance->newRectangleShape(x, y, w, h, angle); });
luax_catchexcept(L, [&](){ shape = instance()->newRectangleShape(x, y, w, h, angle); });
luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape);
return 1;
}
@@ -154,7 +155,7 @@ int w_newEdgeShape(lua_State *L)
float x2 = (float)luaL_checknumber(L, 3);
float y2 = (float)luaL_checknumber(L, 4);
EdgeShape *shape;
luax_catchexcept(L, [&](){ shape = instance->newEdgeShape(x1, y1, x2, y2); });
luax_catchexcept(L, [&](){ shape = instance()->newEdgeShape(x1, y1, x2, y2); });
luax_pushtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, shape);
return 1;
}
@@ -162,14 +163,14 @@ int w_newEdgeShape(lua_State *L)
int w_newPolygonShape(lua_State *L)
{
int ret = 0;
luax_catchexcept(L, [&](){ ret = instance->newPolygonShape(L); });
luax_catchexcept(L, [&](){ ret = instance()->newPolygonShape(L); });
return ret;
}
int w_newChainShape(lua_State *L)
{
int ret = 0;
luax_catchexcept(L, [&](){ ret = instance->newChainShape(L); });
luax_catchexcept(L, [&](){ ret = instance()->newChainShape(L); });
return ret;
}
@@ -184,7 +185,7 @@ int w_newDistanceJoint(lua_State *L)
bool collideConnected = luax_optboolean(L, 7, false);
DistanceJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);
j = instance()->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);
});
luax_pushtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, j);
return 1;
@@ -196,7 +197,7 @@ int w_newMouseJoint(lua_State *L)
float x = (float)luaL_checknumber(L, 2);
float y = (float)luaL_checknumber(L, 3);
MouseJoint *j;
luax_catchexcept(L, [&](){ j = instance->newMouseJoint(body, x, y); });
luax_catchexcept(L, [&](){ j = instance()->newMouseJoint(body, x, y); });
luax_pushtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, j);
return 1;
}
@@ -210,7 +211,7 @@ int w_newRevoluteJoint(lua_State *L)
bool collideConnected = luax_optboolean(L, 5, false);
RevoluteJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newRevoluteJoint(body1, body2, x, y, collideConnected);
j = instance()->newRevoluteJoint(body1, body2, x, y, collideConnected);
});
luax_pushtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, j);
return 1;
@@ -242,7 +243,7 @@ int w_newPrismaticJoint(lua_State *L)
}
PrismaticJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
j = instance()->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
});
luax_pushtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, j);
return 1;
@@ -265,7 +266,7 @@ int w_newPulleyJoint(lua_State *L)
PulleyJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio, collideConnected);
j = instance()->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio, collideConnected);
});
luax_pushtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, j);
return 1;
@@ -280,7 +281,7 @@ int w_newGearJoint(lua_State *L)
GearJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newGearJoint(joint1, joint2, ratio, collideConnected);
j = instance()->newGearJoint(joint1, joint2, ratio, collideConnected);
});
luax_pushtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, j);
return 1;
@@ -308,7 +309,7 @@ int w_newFrictionJoint(lua_State *L)
}
FrictionJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);
j = instance()->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);
});
luax_pushtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, j);
return 1;
@@ -336,7 +337,7 @@ int w_newWeldJoint(lua_State *L)
}
WeldJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);
j = instance()->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);
});
luax_pushtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, j);
return 1;
@@ -369,7 +370,7 @@ int w_newWheelJoint(lua_State *L)
WheelJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
j = instance()->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
});
luax_pushtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, j);
return 1;
@@ -387,7 +388,7 @@ int w_newRopeJoint(lua_State *L)
bool collideConnected = luax_optboolean(L, 8, false);
RopeJoint *j;
luax_catchexcept(L, [&]() {
j = instance->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);
j = instance()->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);
});
luax_pushtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, j);
return 1;
@@ -402,12 +403,12 @@ int w_newMotorJoint(lua_State *L)
{
float correctionFactor = (float)luaL_checknumber(L, 3);
luax_catchexcept(L, [&]() {
j = instance->newMotorJoint(body1, body2, correctionFactor);
j = instance()->newMotorJoint(body1, body2, correctionFactor);
});
}
else
{
luax_catchexcept(L, [&](){ j = instance->newMotorJoint(body1, body2); });
luax_catchexcept(L, [&](){ j = instance()->newMotorJoint(body1, body2); });
}
luax_pushtype(L, "MotorJoint", PHYSICS_MOTOR_JOINT_T, j);
return 1;
@@ -415,7 +416,7 @@ int w_newMotorJoint(lua_State *L)
int w_getDistance(lua_State *L)
{
return instance->getDistance(L);
return instance()->getDistance(L);
}
int w_setMeter(lua_State *L)
@@ -487,7 +488,8 @@ static const lua_CFunction types[] =
extern "C" int luaopen_love_physics(lua_State *L)
{
if (instance == 0)
Physics *instance = instance();
if (instance == nullptr)
{
luax_catchexcept(L, [&](){ instance = new Physics(); });
}