#include #include #include #include #include #include "util/util.h" struct window_data { lua_State *L; int framebuffer_size_callback; }; struct window_data * create_window_data(lua_State *L) { struct window_data *wdata = malloc(sizeof(struct window_data)); if (wdata == NULL) return NULL; wdata->L = L; wdata->framebuffer_size_callback = LUA_NOREF; return wdata; } int window_create(lua_State *L); int window_destroy(lua_State *L); int window_make_context_current(lua_State *L); int window_set_hint(lua_State *L); int window_should_close(lua_State *L); int window_set_should_close(lua_State *L); int window_poll_events(lua_State *L); int window_swap_buffers(lua_State *L); int window_set_framebuffer_size_callback(lua_State *L); int window_get_time(lua_State *L); int window_get_key(lua_State *L); int window_get_cursor_pos(lua_State *L); int window_set_input_mode(lua_State *L); static const char *window_tname = "window"; void setup_window(lua_State *L, int honey_index) { luaL_newmetatable(L, window_tname); lua_pop(L, 1); 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 */ H_INT("CURSOR", GLFW_CURSOR), /* cursor modes */ H_INT("CURSOR_NORMAL", GLFW_CURSOR_NORMAL), H_INT("CURSOR_HIDDEN", GLFW_CURSOR_HIDDEN), H_INT("CURSOR_DISABLED", GLFW_CURSOR_DISABLED), /* key states */ H_INT("PRESS", GLFW_PRESS), H_INT("RELEASE", GLFW_RELEASE), /* key buttons */ 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"); lua_setfield(L, honey_index, "window"); } 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) { 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; 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) luaL_error(L, "failed to create window"); struct window_data *wdata = create_window_data(L); glfwSetWindowUserPointer(*win, wdata); glfwSetFramebufferSizeCallback(*win, framebuffer_size_callback_); luaL_getmetatable(L, window_tname); lua_setmetatable(L, -2); return 1; } int window_destroy(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); void *wdata = glfwGetWindowUserPointer(*win); if (wdata != NULL) { free(wdata); glfwSetWindowUserPointer(*win, NULL); } glfwDestroyWindow(*win); return 0; } int window_make_context_current(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); glfwMakeContextCurrent(*win); return 0; } int window_set_hint(lua_State *L) { int hint = luaL_checkinteger(L, 1); int value = luaL_checkinteger(L, 2); glfwWindowHint(hint, value); return 0; } int window_should_close(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); lua_pushboolean(L, glfwWindowShouldClose(*win)); return 1; } int window_set_should_close(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); int value = lua_toboolean(L, 2); glfwSetWindowShouldClose(*win, value); return 0; } int window_poll_events(lua_State *L) { glfwPollEvents(); return 0; } int window_swap_buffers(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); glfwSwapBuffers(*win); return 0; } int window_set_framebuffer_size_callback(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); luaL_checktype(L, 2, LUA_TFUNCTION); int func = 2; struct window_data *wdata = glfwGetWindowUserPointer(*win); lua_pushvalue(L, func); wdata->framebuffer_size_callback = luaL_ref(L, LUA_REGISTRYINDEX); return 0; } int window_get_time(lua_State *L) { lua_pushnumber(L, glfwGetTime()); return 1; } int window_get_key(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); int key = luaL_checkinteger(L, 2); lua_pushinteger(L, glfwGetKey(*win, key)); return 1; } int window_get_cursor_pos(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); double x, y; glfwGetCursorPos(*win, &x, &y); lua_pushnumber(L, x); lua_pushnumber(L, y); return 2; } int window_set_input_mode(lua_State *L) { GLFWwindow **win = luaL_checkudata(L, 1, window_tname); int mode = luaL_checkinteger(L, 2); int value = luaL_checkinteger(L, 3); glfwSetInputMode(*win, mode, value); return 0; }