Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glfw3.h>
- #include "lua.h"
- #include "lj_obj.h"
- #include "lauxlib.h"
- lua_State *current_L;
- void insert_simple_int_event(const char *type, const char *key, int value, GLFWwindow window) {
- lua_State *L = current_L;
- lua_createtable(L, 0, 3);
- lua_pushstring(L, "type");
- lua_pushstring(L, type);
- lua_rawset(L, -3);
- lua_pushstring(L, key);
- lua_pushinteger(L, value);
- lua_rawset(L, -3);
- lua_pushstring(L, "window");
- lua_pushinteger(L, (lua_Integer)window);
- lua_rawset(L, -3);
- lua_pushinteger(L, lua_objlen(L, -2) + 1);
- lua_insert(L, -2);
- lua_rawset(L, -3);
- }
- /*
- key will be the key identifier, action will be GLFW_PRESS or GLFW_RELEASE
- */
- void handle_key_event(GLFWwindow window, int key, int action) {
- insert_simple_int_event((action == GLFW_PRESS) ? "keydown" : "keyup", "key", key, window);
- }
- /*
- char will be the unicode codepoint of the character typed
- */
- void handle_char_event(GLFWwindow window, int chr) {
- insert_simple_int_event("char", "char", chr, window);
- }
- static int l_registerEventListeners(lua_State *L) {
- glfwSetKeyCallback(&handle_key_event);
- glfwSetCharCallback(&handle_char_event);
- return 0;
- }
- static int l_pollEvents(lua_State *L) {
- lua_newtable(L);
- current_L = L;
- glfwPollEvents();
- return 1;
- }
- static const struct luaL_Reg glfw_c [] = {
- {"registerEventListeners", l_registerEventListeners},
- {"pollEvents", l_pollEvents},
- {NULL, NULL}
- };
- int luaopen_c_glfw_c(lua_State *L) {
- luaL_register(L, "glfw_c", glfw_c);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement