wrap_Event.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Copyright (c) 2006-2013 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 "wrap_Event.h"
  21. // LOVE
  22. #include "common/runtime.h"
  23. // sdlevent
  24. #include "Event.h"
  25. namespace love
  26. {
  27. namespace event
  28. {
  29. namespace sdl
  30. {
  31. static Event *instance = 0;
  32. static int poll_i(lua_State *L)
  33. {
  34. Message *m;
  35. while (instance->poll(m))
  36. {
  37. int args = m->toLua(L);
  38. m->release();
  39. return args;
  40. }
  41. // No pending events.
  42. return 0;
  43. }
  44. int w_pump(lua_State *)
  45. {
  46. instance->pump();
  47. return 0;
  48. }
  49. int w_poll(lua_State *L)
  50. {
  51. lua_pushcclosure(L, &poll_i, 0);
  52. return 1;
  53. }
  54. int w_wait(lua_State *L)
  55. {
  56. static Message *m;
  57. if ((m = instance->wait()))
  58. {
  59. int args = m->toLua(L);
  60. m->release();
  61. return args;
  62. }
  63. return 0;
  64. }
  65. int w_push(lua_State *L)
  66. {
  67. static Message *m;
  68. bool success = (m = Message::fromLua(L, 1)) != NULL;
  69. luax_pushboolean(L, success);
  70. if (!success)
  71. return 1;
  72. instance->push(m);
  73. m->release();
  74. return 1;
  75. }
  76. int w_clear(lua_State *)
  77. {
  78. instance->clear();
  79. return 0;
  80. }
  81. int w_quit(lua_State *L)
  82. {
  83. Message *m = new Message("quit");
  84. instance->push(m);
  85. m->release();
  86. luax_pushboolean(L, true);
  87. return 1;
  88. }
  89. // List of functions to wrap.
  90. static const luaL_Reg functions[] =
  91. {
  92. { "pump", w_pump },
  93. { "poll", w_poll },
  94. { "wait", w_wait },
  95. { "push", w_push },
  96. { "clear", w_clear },
  97. { "quit", w_quit },
  98. { 0, 0 }
  99. };
  100. extern "C" int luaopen_love_event(lua_State *L)
  101. {
  102. if (instance == 0)
  103. {
  104. try
  105. {
  106. instance = new Event();
  107. }
  108. catch (love::Exception &e)
  109. {
  110. return luaL_error(L, "%s", e.what());
  111. }
  112. }
  113. else
  114. instance->retain();
  115. WrappedModule w;
  116. w.module = instance;
  117. w.name = "event";
  118. w.flags = MODULE_T;
  119. w.functions = functions;
  120. w.types = 0;
  121. return luax_register_module(L, w);
  122. }
  123. } // sdl
  124. } // event
  125. } // love