Minor MeshShape cleanup;

- Fix memory leak
- Minor error message improvements
This commit is contained in:
bjorn 2021-02-19 17:28:04 -07:00
parent 880d06a1d1
commit 57a215a0aa
3 changed files with 14 additions and 9 deletions

View File

@ -112,17 +112,16 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) {
static int l_lovrWorldNewMeshCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World);
lovrAssert(lua_istable(L, 2), "Vertices must be a table");
lovrAssert(lua_istable(L, 3), "Indices must be a table");
luaL_checktype(L, 2, LUA_TTABLE);
luaL_checktype(L, 3, LUA_TTABLE);
int vertexCount = luax_len(L, 2);
int indexCount = luax_len(L, 3);
// TODO: this never gets deallocated
float * vertices = malloc(sizeof(float) * vertexCount * 3);
unsigned * indices = malloc(sizeof(unsigned) * indexCount);
float* vertices = malloc(sizeof(float) * vertexCount * 3);
unsigned* indices = malloc(sizeof(unsigned) * indexCount);
for (int i = 0; i < vertexCount; i++) {
lua_rawgeti(L, 2, i + 1);
lovrAssert(lua_istable(L, -1), "Each verticle must be a table of coordinates");
lovrAssert(lua_istable(L, -1), "Each vertex must be a table of coordinates");
lua_rawgeti(L, -1, 1); // x
vertices[i * 3 + 0] = luaL_optnumber(L, -1, 0.);
lua_pop(L, 1);
@ -136,9 +135,9 @@ static int l_lovrWorldNewMeshCollider(lua_State* L) {
for (int i = 0; i < indexCount; i++) {
lua_rawgeti(L, 3, i + 1);
indices[i] = luaL_checkinteger(L, -1) - 1;
lua_pop(L, 1);
}
lua_pop(L, indexCount);
Collider* collider = lovrColliderCreate(world, 0,0,0);
Collider* collider = lovrColliderCreate(world, 0, 0, 0);
MeshShape* shape = lovrMeshShapeCreate(vertexCount, vertices, indexCount, indices);
lovrColliderAddShape(collider, shape);
lovrColliderInitInertia(collider, shape);

View File

@ -698,6 +698,8 @@ void lovrShapeDestroyData(Shape* shape) {
if (shape->type == SHAPE_MESH) {
dTriMeshDataID dataID = dGeomTriMeshGetData(shape->id);
dGeomTriMeshDataDestroy(dataID);
free(shape->vertices);
free(shape->indices);
}
dGeomDestroy(shape->id);
shape->id = NULL;
@ -925,7 +927,7 @@ void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) {
dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length);
}
MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount, dTriIndex indices[]) {
MeshShape* lovrMeshShapeCreate(int vertexCount, float* vertices, int indexCount, dTriIndex* indices) {
MeshShape* mesh = calloc(1, sizeof(MeshShape));
lovrAssert(mesh, "Out of memory");
mesh->ref = 1;
@ -934,6 +936,8 @@ MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount
dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL);
mesh->id = dCreateTriMesh(0, dataID, 0, 0, 0);
mesh->type = SHAPE_MESH;
mesh->vertices = vertices;
mesh->indices = indices;
dGeomSetData(mesh->id, mesh);
return mesh;
}

View File

@ -59,6 +59,8 @@ struct Shape {
ShapeType type;
dGeomID id;
Collider* collider;
void* vertices;
void* indices;
void* userdata;
bool sensor;
};