diff options
author | sanine-a <sanine.not@pm.me> | 2020-12-05 16:59:28 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-12-05 16:59:28 -0600 |
commit | 09a96abe4d6933eb473b0dd8a7027cc0cb04ac63 (patch) | |
tree | fd60e3080694689f336351cab4bb269a21869606 /src | |
parent | bbebc8d2582f639ecac1c4551411949eeacfc7ee (diff) |
change textures to use metamethods
Diffstat (limited to 'src')
-rw-r--r-- | src/texture.c | 39 | ||||
-rw-r--r-- | src/texture.h | 2 |
2 files changed, 29 insertions, 12 deletions
diff --git a/src/texture.c b/src/texture.c index cdd36c4..919cbda 100644 --- a/src/texture.c +++ b/src/texture.c @@ -1,8 +1,12 @@ #include "texture.h" +int honey_texture_mt_ref = LUA_NOREF; + static int honey_lua_texture_new(lua_State* L) { honey_texture* texture = lua_newuserdata(L, sizeof(honey_texture)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_texture_mt_ref); + lua_setmetatable(L, -2); return 1; } @@ -26,17 +30,20 @@ static int honey_lua_texture_create(lua_State* L) else if (strcmp(type, "depth") == 0) honey_texture_new_depth(texture, width, height, NULL); else { - char* error; - honey_format_string(&error, - "unknown texture type '%s'", - type); - lua_pushstring(L, error); - free(error); - lua_error(L); + honey_lua_throw_error + (L, "unknown texture type '%s'", type); } return 0; } +static int honey_lua_texture_destroy(lua_State* L) +{ + honey_texture* texture; + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &texture); + glDeleteTextures(1, &(texture->id)); + return 0; +} + static int honey_lua_texture_load(lua_State* L) { honey_texture* texture; @@ -100,12 +107,20 @@ static int honey_lua_framebuffer_new(lua_State* L) void honey_setup_texture(lua_State* L) { honey_lua_create_table - (L, 5, + (L, 2, + HONEY_TABLE, "__index", 3, + HONEY_FUNCTION, "create", honey_lua_texture_create, + HONEY_FUNCTION, "load", honey_lua_texture_load, + HONEY_FUNCTION, "use", honey_lua_texture_use, + + HONEY_FUNCTION, "__gc", honey_lua_texture_destroy); + honey_texture_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + honey_lua_create_table + (L, 2, HONEY_FUNCTION, "new", honey_lua_texture_new, - HONEY_FUNCTION, "new_framebuffer", honey_lua_framebuffer_new, - HONEY_FUNCTION, "create", honey_lua_texture_create, - HONEY_FUNCTION, "load", honey_lua_texture_load, - HONEY_FUNCTION, "use", honey_lua_texture_use); + HONEY_FUNCTION, "new_framebuffer", honey_lua_framebuffer_new); + lua_setfield(L, -2, "texture"); } diff --git a/src/texture.h b/src/texture.h index aed6057..93417f0 100644 --- a/src/texture.h +++ b/src/texture.h @@ -8,6 +8,8 @@ #include "common.h" +extern int honey_texture_mt_ref; + enum honey_texture_result { TEXTURE_OK, TEXTURE_FAILED, |