/src/bindings/bind_input.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 77 lines · 65 code · 12 blank · 0 comment · 0 complexity · f82f623814c865779415424dfb019497 MD5 · raw file

  1. #include "common.h"
  2. #include "bind_all.h"
  3. #include "app/app.h"
  4. namespace luab
  5. {
  6. static int bind_input_get_key_state(lua_State *L)
  7. {
  8. lua_pushnumber(L, (lua_Number)app::input::get_key_state((uint16)lua_tonumber(L, 1)));
  9. return 1;
  10. }
  11. static int bind_input_get_button_state(lua_State *L)
  12. {
  13. lua_pushnumber(L, (lua_Number)app::input::get_button_state((uint16)lua_tonumber(L, 1)));
  14. return 1;
  15. }
  16. static int bind_input_get_wheel_rel(lua_State *L)
  17. {
  18. lua_pushnumber(L, (lua_Number)app::input::get_wheel_rel());
  19. return 1;
  20. }
  21. static int bind_input_get_cursor_pos(lua_State *L)
  22. {
  23. int16 x, y;
  24. app::input::get_cursor_pos(&x, &y);
  25. lua_pushnumber(L, (lua_Number)x);
  26. lua_pushnumber(L, (lua_Number)y);
  27. return 2;
  28. }
  29. static int bind_input_get_cursor_rel(lua_State *L)
  30. {
  31. int16 xr, yr;
  32. app::input::get_cursor_rel(&xr, &yr);
  33. lua_pushnumber(L, (lua_Number)xr);
  34. lua_pushnumber(L, (lua_Number)yr);
  35. return 2;
  36. }
  37. static int bind_input_tick(lua_State *L)
  38. {
  39. lua_pushboolean(L, app::input::tick());
  40. return 1;
  41. }
  42. static void bind_input_constants(lua_State *L)
  43. {
  44. lua_newtable(L);
  45. lua_pushnumber(L, app::input::UP); lua_setfield(L, -2, "UP");
  46. lua_pushnumber(L, app::input::DOWN); lua_setfield(L, -2, "DOWN");
  47. lua_pushnumber(L, app::input::PRESSED); lua_setfield(L, -2, "PRESSED");
  48. lua_setglobal(L, "input");
  49. }
  50. static const struct luaL_reg funcs[] =
  51. {
  52. {"get_key_state", bind_input_get_key_state},
  53. {"get_button_state", bind_input_get_button_state},
  54. {"get_wheel_rel", bind_input_get_wheel_rel},
  55. {"get_cursor_pos", bind_input_get_cursor_pos},
  56. {"get_cursor_rel", bind_input_get_cursor_rel},
  57. {"tick", bind_input_tick},
  58. {0, 0}
  59. };
  60. void bind_input(lua_State *L)
  61. {
  62. bind_input_constants(L);
  63. luaL_register(L, "input", funcs);
  64. }
  65. }