From 7948b11082c9a30967c59f6e4aeae219fb211299 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 21 Sep 2014 03:31:36 -0300 Subject: [PATCH] Updated love.physics.newPolygonShape and love.physics.newChainShape to accept a table of vertices, fixed a small memory leak if newChainShape errors, fixed the vertex order of newChainShape. --- src/modules/physics/box2d/Physics.cpp | 68 ++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index b62c17efe..393c93c7f 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -93,6 +93,12 @@ EdgeShape *Physics::newEdgeShape(float x1, float y1, float x2, float y2) int Physics::newPolygonShape(lua_State *L) { int argc = lua_gettop(L); + + bool istable = lua_istable(L, 1); + + if (istable) + argc = lua_objlen(L, 1); + if (argc % 2 != 0) return luaL_error(L, "Number of vertex components must be a multiple of two."); @@ -103,16 +109,31 @@ int Physics::newPolygonShape(lua_State *L) else if (vcount > b2_maxPolygonVertices) return luaL_error(L, "Expected a maximum of %d vertices, got %d.", b2_maxPolygonVertices, vcount); - b2PolygonShape *s = new b2PolygonShape(); - b2Vec2 vecs[b2_maxPolygonVertices]; - for (int i = 0; i < vcount; i++) + if (istable) { - float x = (float)luaL_checknumber(L, 1 + i * 2); - float y = (float)luaL_checknumber(L, 2 + i * 2); - vecs[i] = Physics::scaleDown(b2Vec2(x, y)); + for (int i = 0; i < vcount; i++) + { + lua_rawgeti(L, 1, 1 + i * 2); + lua_rawgeti(L, 1, 2 + i * 2); + float x = (float)luaL_checknumber(L, -2); + float y = (float)luaL_checknumber(L, -1); + vecs[i] = Physics::scaleDown(b2Vec2(x, y)); + lua_pop(L, 2); + } } + else + { + for (int i = 0; i < vcount; i++) + { + float x = (float)luaL_checknumber(L, 1 + i * 2); + float y = (float)luaL_checknumber(L, 2 + i * 2); + vecs[i] = Physics::scaleDown(b2Vec2(x, y)); + } + } + + b2PolygonShape *s = new b2PolygonShape(); try { @@ -133,22 +154,42 @@ int Physics::newPolygonShape(lua_State *L) int Physics::newChainShape(lua_State *L) { int argc = lua_gettop(L)-1; // first argument is looping + + bool istable = lua_istable(L, 2); + + if (istable) + argc = lua_objlen(L, 2); + if (argc % 2 != 0) return luaL_error(L, "Number of vertex components must be a multiple of two."); int vcount = (int)argc/2; - b2ChainShape *s = new b2ChainShape(); bool loop = luax_toboolean(L, 1); b2Vec2 *vecs = new b2Vec2[vcount]; - for (int i = 0; i