wrap_Shape.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Copyright (c) 2006-2016 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_Shape.h"
  21. #include "common/StringMap.h"
  22. namespace love
  23. {
  24. namespace physics
  25. {
  26. namespace box2d
  27. {
  28. Shape *luax_checkshape(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Shape>(L, idx);
  31. }
  32. int w_Shape_getType(lua_State *L)
  33. {
  34. Shape *t = luax_checkshape(L, 1);
  35. const char *type = "";
  36. Shape::getConstant(t->getType(), type);
  37. lua_pushstring(L, type);
  38. return 1;
  39. }
  40. int w_Shape_getRadius(lua_State *L)
  41. {
  42. Shape *t = luax_checkshape(L, 1);
  43. float radius = t->getRadius();
  44. lua_pushnumber(L, radius);
  45. return 1;
  46. }
  47. int w_Shape_getChildCount(lua_State *L)
  48. {
  49. Shape *t = luax_checkshape(L, 1);
  50. int childCount = t->getChildCount();
  51. lua_pushinteger(L, childCount);
  52. return 1;
  53. }
  54. int w_Shape_testPoint(lua_State *L)
  55. {
  56. Shape *t = luax_checkshape(L, 1);
  57. float x = (float)luaL_checknumber(L, 2);
  58. float y = (float)luaL_checknumber(L, 3);
  59. float r = (float)luaL_checknumber(L, 4);
  60. float px = (float)luaL_checknumber(L, 5);
  61. float py = (float)luaL_checknumber(L, 6);
  62. bool result = t->testPoint(x, y, r, px, py);
  63. lua_pushboolean(L, result);
  64. return 1;
  65. }
  66. int w_Shape_rayCast(lua_State *L)
  67. {
  68. Shape *t = luax_checkshape(L, 1);
  69. lua_remove(L, 1);
  70. int ret = 0;
  71. luax_catchexcept(L, [&](){ ret = t->rayCast(L); });
  72. return ret;
  73. }
  74. int w_Shape_computeAABB(lua_State *L)
  75. {
  76. Shape *t = luax_checkshape(L, 1);
  77. lua_remove(L, 1);
  78. return t->computeAABB(L);
  79. }
  80. int w_Shape_computeMass(lua_State *L)
  81. {
  82. Shape *t = luax_checkshape(L, 1);
  83. lua_remove(L, 1);
  84. return t->computeMass(L);
  85. }
  86. const luaL_Reg w_Shape_functions[] =
  87. {
  88. { "getType", w_Shape_getType },
  89. { "getRadius", w_Shape_getRadius },
  90. { "getChildCount", w_Shape_getChildCount },
  91. { "testPoint", w_Shape_testPoint },
  92. { "rayCast", w_Shape_rayCast },
  93. { "computeAABB", w_Shape_computeAABB },
  94. { "computeMass", w_Shape_computeMass },
  95. { 0, 0 }
  96. };
  97. extern "C" int luaopen_shape(lua_State *L)
  98. {
  99. return luax_register_type(L, &Shape::type, w_Shape_functions, nullptr);
  100. }
  101. } // box2d
  102. } // physics
  103. } // love