wrap_Touch.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "common/config.h"
  21. // LOVE
  22. #include "wrap_Touch.h"
  23. #include "sdl/Touch.h"
  24. namespace love
  25. {
  26. namespace touch
  27. {
  28. #define instance() (Module::getInstance<Touch>(Module::M_TOUCH))
  29. int w_getTouchCount(lua_State *L)
  30. {
  31. lua_pushinteger(L, instance()->getTouchCount());
  32. return 1;
  33. }
  34. int w_getTouch(lua_State *L)
  35. {
  36. int index = luaL_checkint(L, 1) - 1;
  37. Touch::TouchInfo info;
  38. luax_catchexcept(L, [&](){ info = instance()->getTouch(index); });
  39. // Lets hope the ID can be accurately represented in a Lua number...
  40. lua_pushnumber(L, (lua_Number) info.id);
  41. lua_pushnumber(L, info.x);
  42. lua_pushnumber(L, info.y);
  43. return 3;
  44. }
  45. static const luaL_Reg functions[] =
  46. {
  47. { "getTouchCount", w_getTouchCount },
  48. { "getTouch", w_getTouch },
  49. { 0, 0 }
  50. };
  51. extern "C" int luaopen_love_touch(lua_State *L)
  52. {
  53. Touch *instance = instance();
  54. if (instance == nullptr)
  55. {
  56. luax_catchexcept(L, [&](){ instance = new love::touch::sdl::Touch(); });
  57. }
  58. else
  59. instance->retain();
  60. WrappedModule w;
  61. w.module = instance;
  62. w.name = "touch";
  63. w.flags = MODULE_T;
  64. w.functions = functions;
  65. w.types = nullptr;
  66. return luax_register_module(L, w);
  67. }
  68. } // touch
  69. } // love