diff options
Diffstat (limited to 'src/gl/window.c')
-rw-r--r-- | src/gl/window.c | 339 |
1 files changed, 174 insertions, 165 deletions
diff --git a/src/gl/window.c b/src/gl/window.c index 46e9c35..46bc2f5 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -2,7 +2,8 @@ #include <glad/glad.h> #include <GLFW/glfw3.h> #include <lua.h> -#include <honeysuckle.h> +#include <lauxlib.h> +#include "util/util.h" struct window_data { lua_State *L; @@ -42,171 +43,177 @@ void setup_window(lua_State *L, int honey_index) luaL_newmetatable(L, window_tname); lua_pop(L, 1); - int hint_types = hs_create_table(L, - hs_str_int("contextVersionMajor", GLFW_CONTEXT_VERSION_MAJOR), - hs_str_int("contextVersionMinor", GLFW_CONTEXT_VERSION_MINOR), - hs_str_int("openGlProfile", GLFW_OPENGL_PROFILE), - ); - - int profile_types = hs_create_table(L, - hs_str_int("openGlCoreProfile", GLFW_OPENGL_CORE_PROFILE), - ); - - struct hs_tbl_entry tbl[] = { - hs_str_cfunc("create", window_create), - hs_str_cfunc("destroy", window_destroy), - hs_str_cfunc("makeContextCurrent", window_make_context_current), - hs_str_cfunc("setHint", window_set_hint), - hs_str_cfunc("shouldClose", window_should_close), - hs_str_cfunc("setShouldClose", window_set_should_close), - hs_str_cfunc("pollEvents", window_poll_events), - hs_str_cfunc("swapBuffers", window_swap_buffers), - hs_str_cfunc("setFramebufferSizeCallback", window_set_framebuffer_size_callback), - hs_str_cfunc("getTime", window_get_time), - hs_str_cfunc("getKey", window_get_key), - hs_str_cfunc("getCursorPos", window_get_cursor_pos), - hs_str_cfunc("setInputMode", window_set_input_mode), - - hs_str_tbl("hintType", hint_types), - hs_str_tbl("profileType", profile_types), + struct honey_tbl_t tbl[] = { + H_FUNC("create", window_create), + H_FUNC("destroy", window_destroy), + H_FUNC("makeContextCurrent", window_make_context_current), + H_FUNC("setHint", window_set_hint), + H_FUNC("shouldClose", window_should_close), + H_FUNC("setShouldClose", window_set_should_close), + H_FUNC("pollEvents", window_poll_events), + H_FUNC("swapBuffers", window_swap_buffers), + H_FUNC("setFramebufferSizeCallback", window_set_framebuffer_size_callback), + H_FUNC("getTime", window_get_time), + H_FUNC("getKey", window_get_key), + H_FUNC("getCursorPos", window_get_cursor_pos), + H_FUNC("setInputMode", window_set_input_mode), /* input modes */ - hs_str_int("CURSOR", GLFW_CURSOR), + H_INT("CURSOR", GLFW_CURSOR), /* cursor modes */ - hs_str_int("CURSOR_NORMAL", GLFW_CURSOR_NORMAL), - hs_str_int("CURSOR_HIDDEN", GLFW_CURSOR_HIDDEN), - hs_str_int("CURSOR_DISABLED", GLFW_CURSOR_DISABLED), + H_INT("CURSOR_NORMAL", GLFW_CURSOR_NORMAL), + H_INT("CURSOR_HIDDEN", GLFW_CURSOR_HIDDEN), + H_INT("CURSOR_DISABLED", GLFW_CURSOR_DISABLED), /* key states */ - hs_str_int("PRESS", GLFW_PRESS), - hs_str_int("RELEASE", GLFW_RELEASE), + H_INT("PRESS", GLFW_PRESS), + H_INT("RELEASE", GLFW_RELEASE), /* key buttons */ - hs_str_int("KEY_UNKNOWN", GLFW_KEY_UNKNOWN), - hs_str_int("KEY_SPACE", GLFW_KEY_SPACE), - hs_str_int("KEY_APOSTROPHE", GLFW_KEY_APOSTROPHE), - hs_str_int("KEY_COMMA", GLFW_KEY_COMMA), - hs_str_int("KEY_MINUS", GLFW_KEY_MINUS), - hs_str_int("KEY_PERIOD", GLFW_KEY_PERIOD), - hs_str_int("KEY_SLASH", GLFW_KEY_SLASH), - hs_str_int("KEY_0", GLFW_KEY_0), - hs_str_int("KEY_1", GLFW_KEY_1), - hs_str_int("KEY_2", GLFW_KEY_2), - hs_str_int("KEY_3", GLFW_KEY_3), - hs_str_int("KEY_4", GLFW_KEY_4), - hs_str_int("KEY_5", GLFW_KEY_5), - hs_str_int("KEY_6", GLFW_KEY_6), - hs_str_int("KEY_7", GLFW_KEY_7), - hs_str_int("KEY_8", GLFW_KEY_8), - hs_str_int("KEY_9", GLFW_KEY_9), - hs_str_int("KEY_SEMICOLON", GLFW_KEY_SEMICOLON), - hs_str_int("KEY_EQUAL", GLFW_KEY_EQUAL), - hs_str_int("KEY_A", GLFW_KEY_A), - hs_str_int("KEY_B", GLFW_KEY_B), - hs_str_int("KEY_C", GLFW_KEY_C), - hs_str_int("KEY_D", GLFW_KEY_D), - hs_str_int("KEY_E", GLFW_KEY_E), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_G", GLFW_KEY_G), - hs_str_int("KEY_H", GLFW_KEY_H), - hs_str_int("KEY_I", GLFW_KEY_I), - hs_str_int("KEY_J", GLFW_KEY_J), - hs_str_int("KEY_K", GLFW_KEY_K), - hs_str_int("KEY_L", GLFW_KEY_L), - hs_str_int("KEY_M", GLFW_KEY_M), - hs_str_int("KEY_N", GLFW_KEY_N), - hs_str_int("KEY_O", GLFW_KEY_O), - hs_str_int("KEY_P", GLFW_KEY_P), - hs_str_int("KEY_Q", GLFW_KEY_Q), - hs_str_int("KEY_R", GLFW_KEY_R), - hs_str_int("KEY_S", GLFW_KEY_S), - hs_str_int("KEY_T", GLFW_KEY_T), - hs_str_int("KEY_U", GLFW_KEY_U), - hs_str_int("KEY_V", GLFW_KEY_V), - hs_str_int("KEY_W", GLFW_KEY_W), - hs_str_int("KEY_X", GLFW_KEY_X), - hs_str_int("KEY_Y", GLFW_KEY_Y), - hs_str_int("KEY_Z", GLFW_KEY_Z), - hs_str_int("KEY_LEFT_BRACKET", GLFW_KEY_LEFT_BRACKET), - hs_str_int("KEY_BACKSLASH", GLFW_KEY_BACKSLASH), - hs_str_int("KEY_RIGHT_BRACKET", GLFW_KEY_RIGHT_BRACKET), - hs_str_int("KEY_GRAVE_ACCENT", GLFW_KEY_GRAVE_ACCENT), - hs_str_int("KEY_WORLD_1", GLFW_KEY_WORLD_1), - hs_str_int("KEY_WORLD_2", GLFW_KEY_WORLD_2), - hs_str_int("KEY_ESCAPE", GLFW_KEY_ESCAPE), - hs_str_int("KEY_ENTER", GLFW_KEY_ENTER), - hs_str_int("KEY_TAB", GLFW_KEY_TAB), - hs_str_int("KEY_BACKSPACE", GLFW_KEY_BACKSPACE), - hs_str_int("KEY_INSERT", GLFW_KEY_INSERT), - hs_str_int("KEY_DELETE", GLFW_KEY_DELETE), - hs_str_int("KEY_RIGHT", GLFW_KEY_RIGHT), - hs_str_int("KEY_LEFT", GLFW_KEY_LEFT), - hs_str_int("KEY_DOWN", GLFW_KEY_DOWN), - hs_str_int("KEY_UP", GLFW_KEY_UP), - hs_str_int("KEY_PAGE_UP", GLFW_KEY_PAGE_UP), - hs_str_int("KEY_PAGE_DOWN", GLFW_KEY_PAGE_DOWN), - hs_str_int("KEY_HOME", GLFW_KEY_HOME), - hs_str_int("KEY_END", GLFW_KEY_END), - hs_str_int("KEY_CAPS_LOCK", GLFW_KEY_CAPS_LOCK), - hs_str_int("KEY_SCROLL_LOCK", GLFW_KEY_SCROLL_LOCK), - hs_str_int("KEY_NUM_LOCK", GLFW_KEY_NUM_LOCK), - hs_str_int("KEY_PRINT_SCREEN", GLFW_KEY_PRINT_SCREEN), - hs_str_int("KEY_PAUSE", GLFW_KEY_PAUSE), - hs_str_int("KEY_F1", GLFW_KEY_F1), - hs_str_int("KEY_F2", GLFW_KEY_F2), - hs_str_int("KEY_F3", GLFW_KEY_F3), - hs_str_int("KEY_F4", GLFW_KEY_F4), - hs_str_int("KEY_F5", GLFW_KEY_F5), - hs_str_int("KEY_F6", GLFW_KEY_F6), - hs_str_int("KEY_F7", GLFW_KEY_F7), - hs_str_int("KEY_F8", GLFW_KEY_F8), - hs_str_int("KEY_F9", GLFW_KEY_F9), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_F", GLFW_KEY_F), - hs_str_int("KEY_KP_0", GLFW_KEY_KP_0), - hs_str_int("KEY_KP_1", GLFW_KEY_KP_1), - hs_str_int("KEY_KP_2", GLFW_KEY_KP_2), - hs_str_int("KEY_KP_3", GLFW_KEY_KP_3), - hs_str_int("KEY_KP_4", GLFW_KEY_KP_4), - hs_str_int("KEY_KP_5", GLFW_KEY_KP_5), - hs_str_int("KEY_KP_6", GLFW_KEY_KP_6), - hs_str_int("KEY_KP_7", GLFW_KEY_KP_7), - hs_str_int("KEY_KP_8", GLFW_KEY_KP_8), - hs_str_int("KEY_KP_9", GLFW_KEY_KP_9), - hs_str_int("KEY_KP_DECIMAL", GLFW_KEY_KP_DECIMAL), - hs_str_int("KEY_KP_DIVIDE", GLFW_KEY_KP_DIVIDE), - hs_str_int("KEY_KP_MULTIPLY", GLFW_KEY_KP_MULTIPLY), - hs_str_int("KEY_KP_SUBTRACT", GLFW_KEY_KP_SUBTRACT), - hs_str_int("KEY_KP_ADD", GLFW_KEY_KP_ADD), - hs_str_int("KEY_KP_ENTER", GLFW_KEY_KP_ENTER), - hs_str_int("KEY_KP_EQUAL", GLFW_KEY_KP_EQUAL), - hs_str_int("KEY_LEFT_SHIFT", GLFW_KEY_LEFT_SHIFT), - hs_str_int("KEY_LEFT_CONTROL", GLFW_KEY_LEFT_CONTROL), - hs_str_int("KEY_LEFT_ALT", GLFW_KEY_LEFT_ALT), - hs_str_int("KEY_LEFT_SUPER", GLFW_KEY_LEFT_SUPER), - hs_str_int("KEY_RIGHT_SHIFT", GLFW_KEY_RIGHT_SHIFT), - hs_str_int("KEY_RIGHT_CONTROL", GLFW_KEY_RIGHT_CONTROL), - hs_str_int("KEY_RIGHT_ALT", GLFW_KEY_RIGHT_ALT), - hs_str_int("KEY_RIGHT_SUPER", GLFW_KEY_RIGHT_SUPER), - hs_str_int("KEY_MENU", GLFW_KEY_MENU), + H_INT("KEY_UNKNOWN", GLFW_KEY_UNKNOWN), + H_INT("KEY_SPACE", GLFW_KEY_SPACE), + H_INT("KEY_APOSTROPHE", GLFW_KEY_APOSTROPHE), + H_INT("KEY_COMMA", GLFW_KEY_COMMA), + H_INT("KEY_MINUS", GLFW_KEY_MINUS), + H_INT("KEY_PERIOD", GLFW_KEY_PERIOD), + H_INT("KEY_SLASH", GLFW_KEY_SLASH), + H_INT("KEY_0", GLFW_KEY_0), + H_INT("KEY_1", GLFW_KEY_1), + H_INT("KEY_2", GLFW_KEY_2), + H_INT("KEY_3", GLFW_KEY_3), + H_INT("KEY_4", GLFW_KEY_4), + H_INT("KEY_5", GLFW_KEY_5), + H_INT("KEY_6", GLFW_KEY_6), + H_INT("KEY_7", GLFW_KEY_7), + H_INT("KEY_8", GLFW_KEY_8), + H_INT("KEY_9", GLFW_KEY_9), + H_INT("KEY_SEMICOLON", GLFW_KEY_SEMICOLON), + H_INT("KEY_EQUAL", GLFW_KEY_EQUAL), + H_INT("KEY_A", GLFW_KEY_A), + H_INT("KEY_B", GLFW_KEY_B), + H_INT("KEY_C", GLFW_KEY_C), + H_INT("KEY_D", GLFW_KEY_D), + H_INT("KEY_E", GLFW_KEY_E), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_G", GLFW_KEY_G), + H_INT("KEY_H", GLFW_KEY_H), + H_INT("KEY_I", GLFW_KEY_I), + H_INT("KEY_J", GLFW_KEY_J), + H_INT("KEY_K", GLFW_KEY_K), + H_INT("KEY_L", GLFW_KEY_L), + H_INT("KEY_M", GLFW_KEY_M), + H_INT("KEY_N", GLFW_KEY_N), + H_INT("KEY_O", GLFW_KEY_O), + H_INT("KEY_P", GLFW_KEY_P), + H_INT("KEY_Q", GLFW_KEY_Q), + H_INT("KEY_R", GLFW_KEY_R), + H_INT("KEY_S", GLFW_KEY_S), + H_INT("KEY_T", GLFW_KEY_T), + H_INT("KEY_U", GLFW_KEY_U), + H_INT("KEY_V", GLFW_KEY_V), + H_INT("KEY_W", GLFW_KEY_W), + H_INT("KEY_X", GLFW_KEY_X), + H_INT("KEY_Y", GLFW_KEY_Y), + H_INT("KEY_Z", GLFW_KEY_Z), + H_INT("KEY_LEFT_BRACKET", GLFW_KEY_LEFT_BRACKET), + H_INT("KEY_BACKSLASH", GLFW_KEY_BACKSLASH), + H_INT("KEY_RIGHT_BRACKET", GLFW_KEY_RIGHT_BRACKET), + H_INT("KEY_GRAVE_ACCENT", GLFW_KEY_GRAVE_ACCENT), + H_INT("KEY_WORLD_1", GLFW_KEY_WORLD_1), + H_INT("KEY_WORLD_2", GLFW_KEY_WORLD_2), + H_INT("KEY_ESCAPE", GLFW_KEY_ESCAPE), + H_INT("KEY_ENTER", GLFW_KEY_ENTER), + H_INT("KEY_TAB", GLFW_KEY_TAB), + H_INT("KEY_BACKSPACE", GLFW_KEY_BACKSPACE), + H_INT("KEY_INSERT", GLFW_KEY_INSERT), + H_INT("KEY_DELETE", GLFW_KEY_DELETE), + H_INT("KEY_RIGHT", GLFW_KEY_RIGHT), + H_INT("KEY_LEFT", GLFW_KEY_LEFT), + H_INT("KEY_DOWN", GLFW_KEY_DOWN), + H_INT("KEY_UP", GLFW_KEY_UP), + H_INT("KEY_PAGE_UP", GLFW_KEY_PAGE_UP), + H_INT("KEY_PAGE_DOWN", GLFW_KEY_PAGE_DOWN), + H_INT("KEY_HOME", GLFW_KEY_HOME), + H_INT("KEY_END", GLFW_KEY_END), + H_INT("KEY_CAPS_LOCK", GLFW_KEY_CAPS_LOCK), + H_INT("KEY_SCROLL_LOCK", GLFW_KEY_SCROLL_LOCK), + H_INT("KEY_NUM_LOCK", GLFW_KEY_NUM_LOCK), + H_INT("KEY_PRINT_SCREEN", GLFW_KEY_PRINT_SCREEN), + H_INT("KEY_PAUSE", GLFW_KEY_PAUSE), + H_INT("KEY_F1", GLFW_KEY_F1), + H_INT("KEY_F2", GLFW_KEY_F2), + H_INT("KEY_F3", GLFW_KEY_F3), + H_INT("KEY_F4", GLFW_KEY_F4), + H_INT("KEY_F5", GLFW_KEY_F5), + H_INT("KEY_F6", GLFW_KEY_F6), + H_INT("KEY_F7", GLFW_KEY_F7), + H_INT("KEY_F8", GLFW_KEY_F8), + H_INT("KEY_F9", GLFW_KEY_F9), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_F", GLFW_KEY_F), + H_INT("KEY_KP_0", GLFW_KEY_KP_0), + H_INT("KEY_KP_1", GLFW_KEY_KP_1), + H_INT("KEY_KP_2", GLFW_KEY_KP_2), + H_INT("KEY_KP_3", GLFW_KEY_KP_3), + H_INT("KEY_KP_4", GLFW_KEY_KP_4), + H_INT("KEY_KP_5", GLFW_KEY_KP_5), + H_INT("KEY_KP_6", GLFW_KEY_KP_6), + H_INT("KEY_KP_7", GLFW_KEY_KP_7), + H_INT("KEY_KP_8", GLFW_KEY_KP_8), + H_INT("KEY_KP_9", GLFW_KEY_KP_9), + H_INT("KEY_KP_DECIMAL", GLFW_KEY_KP_DECIMAL), + H_INT("KEY_KP_DIVIDE", GLFW_KEY_KP_DIVIDE), + H_INT("KEY_KP_MULTIPLY", GLFW_KEY_KP_MULTIPLY), + H_INT("KEY_KP_SUBTRACT", GLFW_KEY_KP_SUBTRACT), + H_INT("KEY_KP_ADD", GLFW_KEY_KP_ADD), + H_INT("KEY_KP_ENTER", GLFW_KEY_KP_ENTER), + H_INT("KEY_KP_EQUAL", GLFW_KEY_KP_EQUAL), + H_INT("KEY_LEFT_SHIFT", GLFW_KEY_LEFT_SHIFT), + H_INT("KEY_LEFT_CONTROL", GLFW_KEY_LEFT_CONTROL), + H_INT("KEY_LEFT_ALT", GLFW_KEY_LEFT_ALT), + H_INT("KEY_LEFT_SUPER", GLFW_KEY_LEFT_SUPER), + H_INT("KEY_RIGHT_SHIFT", GLFW_KEY_RIGHT_SHIFT), + H_INT("KEY_RIGHT_CONTROL", GLFW_KEY_RIGHT_CONTROL), + H_INT("KEY_RIGHT_ALT", GLFW_KEY_RIGHT_ALT), + H_INT("KEY_RIGHT_SUPER", GLFW_KEY_RIGHT_SUPER), + H_INT("KEY_MENU", GLFW_KEY_MENU), + + H_END }; + create_table(L, tbl); + int window_tbl = lua_gettop(L); + + struct honey_tbl_t hint_types[] = { + H_INT("contextVersionMajor", GLFW_CONTEXT_VERSION_MAJOR), + H_INT("contextVersionMinor", GLFW_CONTEXT_VERSION_MINOR), + H_INT("openGlProfile", GLFW_OPENGL_PROFILE), + H_END + }; + create_table(L, hint_types); + lua_setfield(L, window_tbl, "hintType"); + + struct honey_tbl_t profile_types[] = { + H_INT("openGlCoreProfile", GLFW_OPENGL_CORE_PROFILE), + H_END + }; + create_table(L, profile_types); + lua_setfield(L, window_tbl, "profileType"); - hs_create_table_(L, sizeof(tbl)/sizeof(struct hs_tbl_entry), tbl); lua_setfield(L, honey_index, "window"); } @@ -214,25 +221,27 @@ void setup_window(lua_State *L, int honey_index) static void framebuffer_size_callback_(GLFWwindow *win, int width, int height) { struct window_data *wdata = glfwGetWindowUserPointer(win); + lua_State *L = wdata->L; if (wdata->framebuffer_size_callback != LUA_NOREF) { - hs_rload(wdata->L, wdata->framebuffer_size_callback); - lua_pushlightuserdata(wdata->L, win); - lua_pushinteger(wdata->L, width); - lua_pushinteger(wdata->L, height); - lua_call(wdata->L, 3, 0); + lua_rawgeti(L, LUA_REGISTRYINDEX, wdata->framebuffer_size_callback); + lua_pushlightuserdata(L, win); + lua_pushinteger(L, width); + lua_pushinteger(L, height); + lua_call(L, 3, 0); } } int window_create(lua_State *L) { lua_Integer width, height; - char *title; - hs_parse_args(L, hs_int(width), hs_int(height), hs_str(title)); + width = luaL_checkinteger(L, 1); + height = luaL_checkinteger(L, 2); + const char *title = luaL_checkstring(L, 3); GLFWwindow **win = lua_newuserdata(L, sizeof(GLFWwindow *)); *win = glfwCreateWindow(width, height, title, NULL, NULL); if (*win == NULL) - hs_throw_error(L, "failed to create window"); + luaL_error(L, "failed to create window"); struct window_data *wdata = create_window_data(L); glfwSetWindowUserPointer(*win, wdata); @@ -317,7 +326,7 @@ int window_set_framebuffer_size_callback(lua_State *L) struct window_data *wdata = glfwGetWindowUserPointer(*win); lua_pushvalue(L, func); - wdata->framebuffer_size_callback = hs_rstore(L); + wdata->framebuffer_size_callback = luaL_ref(L, LUA_REGISTRYINDEX); return 0; } |