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 | |
parent | bbebc8d2582f639ecac1c4551411949eeacfc7ee (diff) |
change textures to use metamethods
-rw-r--r-- | demo/ScreenQuad.lua | 4 | ||||
-rw-r--r-- | demo/SpatialShader.lua | 2 | ||||
-rw-r--r-- | demo/main.lua | 2 | ||||
-rw-r--r-- | src/texture.c | 39 | ||||
-rw-r--r-- | src/texture.h | 2 |
5 files changed, 33 insertions, 16 deletions
diff --git a/demo/ScreenQuad.lua b/demo/ScreenQuad.lua index 4cc247f..73cce46 100644 --- a/demo/ScreenQuad.lua +++ b/demo/ScreenQuad.lua @@ -35,8 +35,8 @@ ScreenQuad.shader = honey.shader(vertexShader, fragmentShader) ScreenQuad.tex = honey.texture.new() ScreenQuad.depth = honey.texture.new() -honey.texture.create(ScreenQuad.tex, 'rgb', 640, 480); -honey.texture.create(ScreenQuad.depth, 'depth', 640, 480); +ScreenQuad.tex:create('rgb', 640, 480) +ScreenQuad.depth:create('depth', 640, 480) --honey.texture.load(ScreenQuad.texture, 'checkerboard.png', false) ScreenQuad.fb = honey.texture.new_framebuffer(ScreenQuad.tex, ScreenQuad.depth, 640, 480) diff --git a/demo/SpatialShader.lua b/demo/SpatialShader.lua index 5f83e59..699cff2 100644 --- a/demo/SpatialShader.lua +++ b/demo/SpatialShader.lua @@ -76,7 +76,7 @@ end SpatialShader.prototype.drawMesh = function(self, mesh) self.shader:setMat4('model', mesh.transform) - honey.texture.use(self.albedo, 0) + self.albedo:use(0) honey.mesh.draw(mesh.mesh, self.shader) end diff --git a/demo/main.lua b/demo/main.lua index 231af10..5b19166 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -11,7 +11,7 @@ local buffer = false honey.input.key.bind(honey.input.key.f, function(action) if action == 1 then buffer = not buffer end end) local tex = honey.texture.new() -honey.texture.load(tex, 'checkerboard.png', false) +tex:load('checkerboard.png', false) local sceneRoot = Node.new() 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, |