diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-24 22:51:10 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-24 22:51:10 -0500 |
commit | ef48092c9752fe3fdd358351f368c8c66da3638d (patch) | |
tree | 843df4575c05dae633e3f0a17972980fd2d0a881 | |
parent | de2fa7938758ff83cabf06baf4c0ac7a230e78bf (diff) |
add window resize callback functions
-rw-r--r-- | demo/main.lua | 11 | ||||
-rw-r--r-- | src/window/window.c | 58 | ||||
-rw-r--r-- | src/window/window.h | 16 |
3 files changed, 76 insertions, 9 deletions
diff --git a/demo/main.lua b/demo/main.lua index 64ab0ae..e7f95c1 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -6,20 +6,15 @@ local a_func = function(action, data) honey.window.set_fullscreen(fullscreen) end -local b_func = function(action, data) - if (action == 0) then return end +local resize_func = function(width, height, data) local w, h = honey.window.get_size() - print(w, h) -end - -for k,v in pairs(honey.window) do - print(k, v) + print('resized!', w, h) end honey.window.set_title('honey engine demo') honey.input.bind_key(honey.input.key.a, a_func) -honey.input.bind_key(honey.input.key.b, b_func) +honey.window.resize_bind(resize_func) function honey.update(dt) end diff --git a/src/window/window.c b/src/window/window.c index ff04b5a..0980c87 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -1,7 +1,35 @@ #include "window.h" int honey_window_info_ref = LUA_NOREF; +int honey_window_resize_callback_ref = LUA_NOREF; +int honey_window_resize_callback_data_ref = LUA_NOREF; +static void honey_glfw_window_resize_callback(honey_window window, + int width, int height) +{ + lua_State* L = glfwGetWindowUserPointer(window); + + int callback = honey_window_resize_callback_ref; + int data = honey_window_resize_callback_data_ref; + + if (callback == LUA_NOREF) + return; + + lua_rawgeti(L, LUA_REGISTRYINDEX, callback); + + lua_pushinteger(L, width); + lua_pushinteger(L, height); + + if (data == LUA_NOREF || data == LUA_REFNIL) + lua_pushnil(L); + else + lua_rawgeti(L, LUA_REGISTRYINDEX, data); + + honey_lua_pcall(L, 3, 0); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + bool honey_setup_window(lua_State* L) { honey_window_information* info = lua_newuserdata(L, sizeof(honey_window_information)); @@ -39,14 +67,18 @@ bool honey_setup_window(lua_State* L) glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glfwSetWindowSizeCallback(info->window, honey_glfw_window_resize_callback); + honey_lua_element window_elements[] = { { "set_fullscreen", HONEY_FUNC, { .function = honey_window_set_fullscreen } }, { "set_title", HONEY_FUNC, { .function = honey_window_set_title } }, { "get_size", HONEY_FUNC, { .function = honey_window_get_size } }, { "set_size", HONEY_FUNC, { .function = honey_window_set_size } }, + { "resize_bind", HONEY_FUNC, { .function = honey_window_resize_bind } }, + { "resize_unbind", HONEY_FUNC, { .function = honey_window_resize_unbind } }, }; - honey_lua_create_table(L, window_elements, 4); + honey_lua_create_table(L, window_elements, 6); return true; } @@ -123,3 +155,27 @@ int honey_window_set_size(lua_State* L) glfwSetWindowSize(info->window, width, height); return 0; } + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_resize_bind(lua_State* L) +{ + if (!honey_lua_validate_types(L, 2, HONEY_FUNC, HONEY_ANY)) + lua_error(L); + + lua_pushvalue(L, 1); + honey_window_resize_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 2); + honey_window_resize_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_resize_unbind(lua_State* L) +{ + honey_window_resize_callback_ref = LUA_NOREF; + honey_window_resize_callback_data_ref = LUA_NOREF; + return 0; +} diff --git a/src/window/window.h b/src/window/window.h index 3d320f7..7b12156 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -16,6 +16,8 @@ typedef struct { } honey_window_information; extern int honey_window_info_ref; +extern int honey_window_resize_callback_ref; +extern int honey_window_resize_callback_data_ref; /** @brief Push the various honey.window table to the stack. * @@ -58,4 +60,18 @@ int honey_window_get_size(lua_State* L); */ int honey_window_set_size(lua_State* L); +/** @brief Bind a call back to the window resize. + * + * @param[in] callback The callback function to call on a window resize. + * + * @returns Nothing. + */ +int honey_window_resize_bind(lua_State* L); + +/** @brief Unbind any callback that may be attached to the window resize. + * + * @returns Nothing. + */ +int honey_window_resize_unbind(lua_State* L); + #endif |