From 50d8b05f588d019a8a5ae1a76d22677b10553167 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sat, 31 Oct 2020 14:01:55 -0500 Subject: add basic texture bindings --- src/texture/texture.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/texture/texture.h | 9 ++++++--- 2 files changed, 60 insertions(+), 4 deletions(-) (limited to 'src/texture') diff --git a/src/texture/texture.c b/src/texture/texture.c index 591ee11..1bb798a 100644 --- a/src/texture/texture.c +++ b/src/texture/texture.c @@ -1,6 +1,59 @@ #include "texture.h" -enum honey_texture_result honey_texture_new(honey_texture* texture, +static int honey_lua_texture_new(lua_State* L) +{ + honey_texture* texture = lua_newuserdata(L, sizeof(honey_texture)); + return 1; +} + +static int honey_lua_texture_load(lua_State* L) +{ + honey_texture* texture; + char* texture_path; + bool use_alpha; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &texture, + HONEY_STRING, &texture_path, + HONEY_BOOLEAN, &use_alpha); + enum honey_texture_result result = honey_texture_load(texture, texture_path, use_alpha); + if (result != TEXTURE_OK) { + char* error; + honey_format_string(&error, + "failed to load '%s'", + texture_path); + lua_pushstring(L, error); + free(error); + lua_error(L); + } + + return 0; +} + +static int honey_lua_texture_use(lua_State* L) +{ + honey_texture* texture; + int texture_unit; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &texture, + HONEY_INTEGER, &texture_unit); + honey_texture_use(*texture, texture_unit); + return 0; +} + +void honey_setup_texture(lua_State* L) +{ + honey_lua_element texture_elements[] = { + { "new", HONEY_FUNCTION, { .function = honey_lua_texture_new } }, + { "load", HONEY_FUNCTION, { .function = honey_lua_texture_load } }, + { "use", HONEY_FUNCTION, { .function = honey_lua_texture_use } }, + }; + + honey_lua_create_table(L, texture_elements, 3); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +enum honey_texture_result honey_texture_load(honey_texture* texture, char* texture_path, bool alpha_channel) { unsigned int texture_id; diff --git a/src/texture/texture.h b/src/texture/texture.h index 037d3d0..c9d1104 100644 --- a/src/texture/texture.h +++ b/src/texture/texture.h @@ -20,6 +20,9 @@ typedef struct { int channels; } honey_texture; +/** @brief Place the honey.texture bindings as a table on the stack. */ +void honey_setup_texture(lua_State* L); + /** @brief Load a texture from disk. * * @param[out] texture Pointer to the destination texture @@ -28,9 +31,9 @@ typedef struct { * * @return Success or failure type */ -enum honey_texture_result honey_texture_new(honey_texture* texture, - char* texture_path, - bool alpha_channel); +enum honey_texture_result honey_texture_load(honey_texture* texture, + char* texture_path, + bool alpha_channel); /** @brief Load a texture into a texture unit. * -- cgit v1.2.1