mirror of https://github.com/bjornbytes/lovr.git
Minor MeshShape cleanup;
- Fix memory leak - Minor error message improvements
This commit is contained in:
parent
880d06a1d1
commit
57a215a0aa
|
@ -112,17 +112,16 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) {
|
||||||
|
|
||||||
static int l_lovrWorldNewMeshCollider(lua_State* L) {
|
static int l_lovrWorldNewMeshCollider(lua_State* L) {
|
||||||
World* world = luax_checktype(L, 1, World);
|
World* world = luax_checktype(L, 1, World);
|
||||||
lovrAssert(lua_istable(L, 2), "Vertices must be a table");
|
luaL_checktype(L, 2, LUA_TTABLE);
|
||||||
lovrAssert(lua_istable(L, 3), "Indices must be a table");
|
luaL_checktype(L, 3, LUA_TTABLE);
|
||||||
int vertexCount = luax_len(L, 2);
|
int vertexCount = luax_len(L, 2);
|
||||||
int indexCount = luax_len(L, 3);
|
int indexCount = luax_len(L, 3);
|
||||||
// TODO: this never gets deallocated
|
float* vertices = malloc(sizeof(float) * vertexCount * 3);
|
||||||
float * vertices = malloc(sizeof(float) * vertexCount * 3);
|
unsigned* indices = malloc(sizeof(unsigned) * indexCount);
|
||||||
unsigned * indices = malloc(sizeof(unsigned) * indexCount);
|
|
||||||
|
|
||||||
for (int i = 0; i < vertexCount; i++) {
|
for (int i = 0; i < vertexCount; i++) {
|
||||||
lua_rawgeti(L, 2, i + 1);
|
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
|
lua_rawgeti(L, -1, 1); // x
|
||||||
vertices[i * 3 + 0] = luaL_optnumber(L, -1, 0.);
|
vertices[i * 3 + 0] = luaL_optnumber(L, -1, 0.);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
@ -136,9 +135,9 @@ static int l_lovrWorldNewMeshCollider(lua_State* L) {
|
||||||
for (int i = 0; i < indexCount; i++) {
|
for (int i = 0; i < indexCount; i++) {
|
||||||
lua_rawgeti(L, 3, i + 1);
|
lua_rawgeti(L, 3, i + 1);
|
||||||
indices[i] = luaL_checkinteger(L, -1) - 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);
|
MeshShape* shape = lovrMeshShapeCreate(vertexCount, vertices, indexCount, indices);
|
||||||
lovrColliderAddShape(collider, shape);
|
lovrColliderAddShape(collider, shape);
|
||||||
lovrColliderInitInertia(collider, shape);
|
lovrColliderInitInertia(collider, shape);
|
||||||
|
|
|
@ -698,6 +698,8 @@ void lovrShapeDestroyData(Shape* shape) {
|
||||||
if (shape->type == SHAPE_MESH) {
|
if (shape->type == SHAPE_MESH) {
|
||||||
dTriMeshDataID dataID = dGeomTriMeshGetData(shape->id);
|
dTriMeshDataID dataID = dGeomTriMeshGetData(shape->id);
|
||||||
dGeomTriMeshDataDestroy(dataID);
|
dGeomTriMeshDataDestroy(dataID);
|
||||||
|
free(shape->vertices);
|
||||||
|
free(shape->indices);
|
||||||
}
|
}
|
||||||
dGeomDestroy(shape->id);
|
dGeomDestroy(shape->id);
|
||||||
shape->id = NULL;
|
shape->id = NULL;
|
||||||
|
@ -925,7 +927,7 @@ void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) {
|
||||||
dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), 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));
|
MeshShape* mesh = calloc(1, sizeof(MeshShape));
|
||||||
lovrAssert(mesh, "Out of memory");
|
lovrAssert(mesh, "Out of memory");
|
||||||
mesh->ref = 1;
|
mesh->ref = 1;
|
||||||
|
@ -934,6 +936,8 @@ MeshShape* lovrMeshShapeCreate(int vertexCount, float vertices[], int indexCount
|
||||||
dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL);
|
dGeomTriMeshDataPreprocess2(dataID, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL);
|
||||||
mesh->id = dCreateTriMesh(0, dataID, 0, 0, 0);
|
mesh->id = dCreateTriMesh(0, dataID, 0, 0, 0);
|
||||||
mesh->type = SHAPE_MESH;
|
mesh->type = SHAPE_MESH;
|
||||||
|
mesh->vertices = vertices;
|
||||||
|
mesh->indices = indices;
|
||||||
dGeomSetData(mesh->id, mesh);
|
dGeomSetData(mesh->id, mesh);
|
||||||
return mesh;
|
return mesh;
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,8 @@ struct Shape {
|
||||||
ShapeType type;
|
ShapeType type;
|
||||||
dGeomID id;
|
dGeomID id;
|
||||||
Collider* collider;
|
Collider* collider;
|
||||||
|
void* vertices;
|
||||||
|
void* indices;
|
||||||
void* userdata;
|
void* userdata;
|
||||||
bool sensor;
|
bool sensor;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue