From c5874a0fc256a429b682f14344fca74fd0deab3a Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 25 Oct 2020 12:56:16 -0500 Subject: add basic cglm binding functions --- CMakeLists.txt | 1 + demo/main.lua | 29 +++++++++++++++++++++++++++ src/cglm_bindings.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cglm_bindings.h | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/honey.c | 3 +++ src/honey.h | 7 +------ 6 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 src/cglm_bindings.c create mode 100644 src/cglm_bindings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a374d4..ef6be9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ add_library(stb_image src/stb_image/stb_image.c) set(SOURCE_FILES src/main.c + src/cglm_bindings.c src/camera/camera.c src/error/error.c src/honey.c diff --git a/demo/main.lua b/demo/main.lua index 46a9947..28e74b2 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -1,4 +1,5 @@ local fullscreen = false +local cursor_mode = honey.input.mouse.mode.normal local a_func = function(action, data) if (action == 0) then return end @@ -19,10 +20,38 @@ honey.window.set_title('honey engine demo') honey.input.key.bind(honey.input.key.a, a_func) honey.input.key.bind(honey.input.key.escape, honey.exit) +honey.input.key.bind( + honey.input.key.c, + function(action) + local next_mode + if cursor_mode == honey.input.mouse.mode.normal then + next_mode = honey.input.mouse.mode.disabled + else + next_mode = honey.input.mouse.mode.normal + end + + if action == 1 then + honey.input.mouse.set_mode(next_mode) + cursor_mode = next_mode + end + end +) honey.window.resize_bind(resize_func) honey.input.mouse.set_mode( honey.input.mouse.mode.disabled ) honey.input.mouse.bind_movement(mousemove) +function demo_cglm() + local array = honey.cglm.new_array_zero(3) + honey.cglm.set_value(array, 0, 0) + honey.cglm.set_value(array, 1, 5) + honey.cglm.set_value(array, 2, 2) + local x = honey.cglm.get_value(array, 0) + local y = honey.cglm.get_value(array, 1) + local z = honey.cglm.get_value(array, 2) + + print(x, y, z) +end +demo_cglm() local focus_func = function(focus) print('focus:', focus) 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; +} + diff --git a/src/cglm_bindings.h b/src/cglm_bindings.h new file mode 100644 index 0000000..eaadc98 --- /dev/null +++ b/src/cglm_bindings.h @@ -0,0 +1,47 @@ +#ifndef HONEY_CGLM_BINDINGS_H +#define HONEY_CGLM_BINDINGS_H + +#include "common.h" + +/** @brief Push the honey cglm binding functions to the lua stack. + * + * @returns Nothing. + */ +void honey_setup_cglm(lua_State* L); + +/** @brief Push a new float array to the lua stack. + * + * This function initializes the array to all zeros. + * + * @param[in] n The size of the floating-point array to create. + * + * @returns The vector so generated. + */ +int honey_cglm_new_array_zero(lua_State* L); + +/** @brief Set an element of a float array. + * + * This function does NOT check if your index is out of bounds. + * Use caution! + * + * @param[in] array The array to modify. + * @param[in] index The index to set. + * @param[in] value The value to set array[index] to. + * + * @returns Nothing. + */ +int honey_cglm_array_set_value(lua_State* L); + +/** @brief Get an element of a vec3. + * + * This function does NOT check if your index is out of bounds. + * Use caution! + * + * @param[in] array The array to inspect. + * @param[in] index The index to get. + * + * @returns The value at array[index]. + */ +int honey_cglm_array_get_value(lua_State* L); + +#endif diff --git a/src/honey.c b/src/honey.c index da55ca6..73fb285 100644 --- a/src/honey.c +++ b/src/honey.c @@ -70,6 +70,9 @@ bool honey_setup(lua_State** L) honey_setup_input(*L); lua_setfield(*L, -2, "input"); + honey_setup_cglm(*L); + lua_setfield(*L, -2, "cglm"); + lua_pushcfunction(*L, honey_exit); lua_setfield(*L, -2, "exit"); diff --git a/src/honey.h b/src/honey.h index caa92e7..8231b70 100644 --- a/src/honey.h +++ b/src/honey.h @@ -9,10 +9,10 @@ #include "common.h" #include "camera/camera.h" +#include "cglm_bindings.h" #include "input/input.h" #include "light/light.h" #include "mesh/mesh.h" -#include "model/model.h" #include "primitives/primitives.h" #include "shader/shader.h" #include "texture/texture.h" @@ -68,9 +68,4 @@ bool honey_run(lua_State* L, honey_options opts); */ int honey_get_callback(lua_State* L, char* callback); -#define honey_set_resize_callback glfwSetFramebufferSizeCallback -#define honey_set_mouse_move_callback glfwSetCursorPosCallback - -#define honey_quit glfwTerminate - #endif -- cgit v1.2.1