Advertisement
pib

GLFW Events for Lua

pib
May 11th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. #include <GL/glfw3.h>
  2. #include "lua.h"
  3. #include "lj_obj.h"
  4. #include "lauxlib.h"
  5.  
  6. lua_State *current_L;
  7.  
  8. void insert_simple_int_event(const char *type, const char *key, int value, GLFWwindow window) {
  9.     lua_State *L = current_L;
  10.  
  11.     lua_createtable(L, 0, 3);
  12.  
  13.     lua_pushstring(L, "type");
  14.     lua_pushstring(L, type);
  15.     lua_rawset(L, -3);
  16.  
  17.     lua_pushstring(L, key);
  18.     lua_pushinteger(L, value);
  19.     lua_rawset(L, -3);
  20.  
  21.     lua_pushstring(L, "window");
  22.     lua_pushinteger(L, (lua_Integer)window);
  23.     lua_rawset(L, -3);
  24.  
  25.     lua_pushinteger(L, lua_objlen(L, -2) + 1);
  26.     lua_insert(L, -2);
  27.     lua_rawset(L, -3);
  28. }
  29.  
  30. /*
  31.   key will be the key identifier, action will be GLFW_PRESS or GLFW_RELEASE
  32. */
  33. void handle_key_event(GLFWwindow window, int key, int action) {
  34.     insert_simple_int_event((action == GLFW_PRESS) ? "keydown" : "keyup", "key", key, window);
  35. }
  36.  
  37. /*
  38.   char will be the unicode codepoint of the character typed
  39.  */
  40. void handle_char_event(GLFWwindow window, int chr) {
  41.     insert_simple_int_event("char", "char", chr, window);
  42. }
  43.  
  44. static int l_registerEventListeners(lua_State *L) {
  45.     glfwSetKeyCallback(&handle_key_event);
  46.     glfwSetCharCallback(&handle_char_event);
  47.     return 0;
  48. }
  49.  
  50. static int l_pollEvents(lua_State *L) {
  51.     lua_newtable(L);
  52.     current_L = L;
  53.     glfwPollEvents();
  54.     return 1;
  55. }
  56.  
  57. static const struct luaL_Reg glfw_c [] = {
  58.     {"registerEventListeners", l_registerEventListeners},
  59.     {"pollEvents", l_pollEvents},
  60.     {NULL, NULL}
  61. };
  62.  
  63. int luaopen_c_glfw_c(lua_State *L) {
  64.     luaL_register(L, "glfw_c", glfw_c);
  65.     return 1;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement