wrap_World.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2006-2022 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_World.h"
  21. namespace love
  22. {
  23. namespace physics
  24. {
  25. namespace box2d
  26. {
  27. World *luax_checkworld(lua_State *L, int idx)
  28. {
  29. World *w = luax_checktype<World>(L, idx);
  30. if (!w->isValid())
  31. luaL_error(L, "Attempt to use destroyed world.");
  32. return w;
  33. }
  34. int w_World_update(lua_State *L)
  35. {
  36. World *t = luax_checkworld(L, 1);
  37. float dt = (float)luaL_checknumber(L, 2);
  38. // Make sure the world callbacks are using the calling Lua thread.
  39. t->setCallbacksL(L);
  40. if (lua_isnoneornil(L, 3))
  41. luax_catchexcept(L, [&](){ t->update(dt); });
  42. else
  43. {
  44. int velocityiterations = (int) luaL_checkinteger(L, 3);
  45. int positioniterations = (int) luaL_checkinteger(L, 4);
  46. luax_catchexcept(L, [&](){ t->update(dt, velocityiterations, positioniterations); });
  47. }
  48. return 0;
  49. }
  50. int w_World_setCallbacks(lua_State *L)
  51. {
  52. World *t = luax_checkworld(L, 1);
  53. lua_remove(L, 1);
  54. return t->setCallbacks(L);
  55. }
  56. int w_World_getCallbacks(lua_State *L)
  57. {
  58. World *t = luax_checkworld(L, 1);
  59. lua_remove(L, 1);
  60. return t->getCallbacks(L);
  61. }
  62. int w_World_setContactFilter(lua_State *L)
  63. {
  64. World *t = luax_checkworld(L, 1);
  65. lua_remove(L, 1);
  66. return t->setContactFilter(L);
  67. }
  68. int w_World_getContactFilter(lua_State *L)
  69. {
  70. World *t = luax_checkworld(L, 1);
  71. lua_remove(L, 1);
  72. return t->getContactFilter(L);
  73. }
  74. int w_World_setGravity(lua_State *L)
  75. {
  76. World *t = luax_checkworld(L, 1);
  77. float arg1 = (float)luaL_checknumber(L, 2);
  78. float arg2 = (float)luaL_checknumber(L, 3);
  79. t->setGravity(arg1, arg2);
  80. return 0;
  81. }
  82. int w_World_getGravity(lua_State *L)
  83. {
  84. World *t = luax_checkworld(L, 1);
  85. lua_remove(L, 1);
  86. return t->getGravity(L);
  87. }
  88. int w_World_translateOrigin(lua_State *L)
  89. {
  90. World *t = luax_checkworld(L, 1);
  91. float arg1 = (float)luaL_checknumber(L, 2);
  92. float arg2 = (float)luaL_checknumber(L, 3);
  93. luax_catchexcept(L, [&](){ t->translateOrigin(arg1, arg2); });
  94. return 0;
  95. }
  96. int w_World_setSleepingAllowed(lua_State *L)
  97. {
  98. World *t = luax_checkworld(L, 1);
  99. bool b = luax_checkboolean(L, 2);
  100. t->setSleepingAllowed(b);
  101. return 0;
  102. }
  103. int w_World_isSleepingAllowed(lua_State *L)
  104. {
  105. World *t = luax_checkworld(L, 1);
  106. luax_pushboolean(L, t->isSleepingAllowed());
  107. return 1;
  108. }
  109. int w_World_isLocked(lua_State *L)
  110. {
  111. World *t = luax_checkworld(L, 1);
  112. luax_pushboolean(L, t->isLocked());
  113. return 1;
  114. }
  115. int w_World_getBodyCount(lua_State *L)
  116. {
  117. World *t = luax_checkworld(L, 1);
  118. lua_pushinteger(L, t->getBodyCount());
  119. return 1;
  120. }
  121. int w_World_getJointCount(lua_State *L)
  122. {
  123. World *t = luax_checkworld(L, 1);
  124. lua_pushinteger(L, t->getJointCount());
  125. return 1;
  126. }
  127. int w_World_getContactCount(lua_State *L)
  128. {
  129. World *t = luax_checkworld(L, 1);
  130. lua_pushinteger(L, t->getContactCount());
  131. return 1;
  132. }
  133. int w_World_getBodies(lua_State *L)
  134. {
  135. World *t = luax_checkworld(L, 1);
  136. lua_remove(L, 1);
  137. int ret = 0;
  138. luax_catchexcept(L, [&](){ ret = t->getBodies(L); });
  139. return ret;
  140. }
  141. int w_World_getJoints(lua_State *L)
  142. {
  143. World *t = luax_checkworld(L, 1);
  144. lua_remove(L, 1);
  145. int ret = 0;
  146. luax_catchexcept(L, [&](){ ret = t->getJoints(L); });
  147. return ret;
  148. }
  149. int w_World_getContacts(lua_State *L)
  150. {
  151. World *t = luax_checkworld(L, 1);
  152. lua_remove(L, 1);
  153. int ret = 0;
  154. luax_catchexcept(L, [&](){ ret = t->getContacts(L); });
  155. return ret;
  156. }
  157. int w_World_queryBoundingBox(lua_State *L)
  158. {
  159. World *t = luax_checkworld(L, 1);
  160. lua_remove(L, 1);
  161. return t->queryBoundingBox(L);
  162. }
  163. int w_World_getFixturesInArea(lua_State *L)
  164. {
  165. World *t = luax_checkworld(L, 1);
  166. lua_remove(L, 1);
  167. int ret = 0;
  168. luax_catchexcept(L, [&](){ ret = t->getFixturesInArea(L); });
  169. return ret;
  170. }
  171. int w_World_rayCast(lua_State *L)
  172. {
  173. World *t = luax_checkworld(L, 1);
  174. lua_remove(L, 1);
  175. int ret = 0;
  176. luax_catchexcept(L, [&](){ ret = t->rayCast(L); });
  177. return ret;
  178. }
  179. int w_World_destroy(lua_State *L)
  180. {
  181. World *t = luax_checkworld(L, 1);
  182. luax_catchexcept(L, [&](){ t->destroy(); });
  183. return 0;
  184. }
  185. int w_World_isDestroyed(lua_State *L)
  186. {
  187. World *w = luax_checktype<World>(L, 1);
  188. luax_pushboolean(L, !w->isValid());
  189. return 1;
  190. }
  191. static const luaL_Reg w_World_functions[] =
  192. {
  193. { "update", w_World_update },
  194. { "setCallbacks", w_World_setCallbacks },
  195. { "getCallbacks", w_World_getCallbacks },
  196. { "setContactFilter", w_World_setContactFilter },
  197. { "getContactFilter", w_World_getContactFilter },
  198. { "setGravity", w_World_setGravity },
  199. { "getGravity", w_World_getGravity },
  200. { "translateOrigin", w_World_translateOrigin },
  201. { "setSleepingAllowed", w_World_setSleepingAllowed },
  202. { "isSleepingAllowed", w_World_isSleepingAllowed },
  203. { "isLocked", w_World_isLocked },
  204. { "getBodyCount", w_World_getBodyCount },
  205. { "getJointCount", w_World_getJointCount },
  206. { "getContactCount", w_World_getContactCount },
  207. { "getBodies", w_World_getBodies },
  208. { "getJoints", w_World_getJoints },
  209. { "getContacts", w_World_getContacts },
  210. { "queryBoundingBox", w_World_queryBoundingBox },
  211. { "getFixturesInArea", w_World_getFixturesInArea },
  212. { "rayCast", w_World_rayCast },
  213. { "destroy", w_World_destroy },
  214. { "isDestroyed", w_World_isDestroyed },
  215. { 0, 0 }
  216. };
  217. extern "C" int luaopen_world(lua_State *L)
  218. {
  219. return luax_register_type(L, &World::type, w_World_functions, nullptr);
  220. }
  221. } // box2d
  222. } // physics
  223. } // love