diff options
author | sanine-a <sanine.not@pm.me> | 2020-12-05 18:37:44 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-12-05 18:37:44 -0600 |
commit | ba9a7162b72e6b20466de7dd115719777315b5c2 (patch) | |
tree | 80ecd412817ca85adcb0ec944ab4a28be7b4737c /src | |
parent | 09a96abe4d6933eb473b0dd8a7027cc0cb04ac63 (diff) |
switch meshes to use metamathods
Diffstat (limited to 'src')
-rw-r--r-- | src/mesh.c | 17 | ||||
-rw-r--r-- | src/mesh.h | 2 | ||||
-rw-r--r-- | src/primitives.c | 4 |
3 files changed, 19 insertions, 4 deletions
@@ -1,5 +1,7 @@ #include "mesh.h" +int honey_mesh_mt_ref = LUA_NOREF; + static int honey_mesh_lua_draw(lua_State* L) { honey_mesh* mesh; @@ -22,10 +24,14 @@ static int honey_mesh_lua_delete(lua_State* L) void honey_setup_mesh(lua_State* L) { honey_lua_create_table - (L, 3, - HONEY_FUNCTION, "load", honey_mesh_load, - HONEY_FUNCTION, "draw", honey_mesh_lua_draw, - HONEY_FUNCTION, "delete", honey_mesh_lua_delete); + (L, 2, + HONEY_TABLE, "__index", 1, + HONEY_FUNCTION, "draw", honey_mesh_lua_draw, + + HONEY_FUNCTION, "__gc", honey_mesh_lua_delete); + honey_mesh_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + lua_pushcfunction(L, honey_mesh_load); lua_setfield(L, -2, "mesh"); } @@ -107,6 +113,9 @@ static void process_nodes_recursively(lua_State* L, { for (int i=0; i<node->mNumMeshes; i++) { honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_mesh_mt_ref); + lua_setmetatable(L, -2); + struct aiMesh* assimp_mesh = scene->mMeshes[node->mMeshes[i]]; *mesh = assimp_to_honey_mesh(assimp_mesh, scene); lua_rawseti(L, -2, *n_meshes); @@ -8,6 +8,8 @@ #include "common.h" +extern int honey_mesh_mt_ref; + typedef struct { unsigned int n_vertices, n_indices; unsigned int vertex_array, vertex_buffer, element_buffer; diff --git a/src/primitives.c b/src/primitives.c index ee68f6a..d858cc6 100644 --- a/src/primitives.c +++ b/src/primitives.c @@ -8,6 +8,8 @@ static int honey_mesh_lua_plane(lua_State* L) HONEY_NUMBER, &height); honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_mesh_mt_ref); + lua_setmetatable(L, -2); if (honey_mesh_new_textured_plane(mesh, width, height) != HONEY_OK) { lua_pushstring(L, "error encountered while building plane"); lua_error(L); @@ -24,6 +26,8 @@ static int honey_mesh_lua_cube(lua_State* L) HONEY_NUMBER, &depth); honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_mesh_mt_ref); + lua_setmetatable(L, -2); if (honey_mesh_new_textured_cube(mesh, width, height, depth) != HONEY_OK) { lua_pushstring(L, "error encountered while building plane"); lua_error(L); |