diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-25 12:56:16 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-25 12:56:16 -0500 |
commit | c5874a0fc256a429b682f14344fca74fd0deab3a (patch) | |
tree | 81deeffab51f4cb30f74855066b4d92a87f406e2 /src/cglm_bindings.c | |
parent | 8dbfbdc929c2321f23b50754eda8fbcdba00ad03 (diff) |
add basic cglm binding functions
Diffstat (limited to 'src/cglm_bindings.c')
-rw-r--r-- | src/cglm_bindings.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/cglm_bindings.c b/src/cglm_bindings.c new file mode 100644 index 0000000..c50114c --- /dev/null +++ b/src/cglm_bindings.c @@ -0,0 +1,56 @@ +#include "cglm_bindings.h" + +void honey_setup_cglm(lua_State* L) +{ + honey_lua_element cglm_elements[] = { + { "new_array_zero", HONEY_FUNC, { .function = honey_cglm_new_array_zero } }, + { "set_value", HONEY_FUNC, { .function = honey_cglm_array_set_value } }, + { "get_value", HONEY_FUNC, { .function = honey_cglm_array_get_value } }, + }; + + honey_lua_create_table(L, cglm_elements, 3); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_new_array_zero(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_INT)) + lua_error(L); + + int size = lua_tointeger(L, 1); + + float* array = lua_newuserdata(L, size*sizeof(float)); + memset(array, 0, size*sizeof(float)); + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_array_set_value(lua_State* L) +{ + if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_INT, HONEY_NUM)) + lua_error(L); + + float* array = lua_touserdata(L, 1); + int index = lua_tointeger(L, 2); + float value = lua_tonumber(L, 3); + + array[index] = value; + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_array_get_value(lua_State* L) +{ + if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_INT)) + lua_error(L); + + float* array = lua_touserdata(L, 1); + int index = lua_tointeger(L, 2); + + lua_pushnumber(L, array[index]); + return 1; +} + |