wrap_ChainShape.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2006-2023 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_ChainShape.h"
  21. #include "wrap_Physics.h"
  22. #include "Physics.h"
  23. namespace love
  24. {
  25. namespace physics
  26. {
  27. namespace box2d
  28. {
  29. ChainShape *luax_checkchainshape(lua_State *L, int idx)
  30. {
  31. return luax_checktype<ChainShape>(L, idx);
  32. }
  33. int w_ChainShape_setNextVertex(lua_State *L)
  34. {
  35. ChainShape *c = luax_checkchainshape(L, 1);
  36. if (lua_isnoneornil(L, 2))
  37. c->setNextVertex();
  38. else
  39. {
  40. float x = (float)luaL_checknumber(L, 2);
  41. float y = (float)luaL_checknumber(L, 3);
  42. luax_catchexcept(L, [&](){ c->setNextVertex(x, y); });
  43. }
  44. return 0;
  45. }
  46. int w_ChainShape_setPreviousVertex(lua_State *L)
  47. {
  48. ChainShape *c = luax_checkchainshape(L, 1);
  49. if (lua_isnoneornil(L, 2))
  50. c->setPreviousVertex();
  51. else
  52. {
  53. float x = (float)luaL_checknumber(L, 2);
  54. float y = (float)luaL_checknumber(L, 3);
  55. luax_catchexcept(L, [&](){ c->setPreviousVertex(x, y); });
  56. }
  57. return 0;
  58. }
  59. int w_ChainShape_getChildEdge(lua_State *L)
  60. {
  61. ChainShape *c = luax_checkchainshape(L, 1);
  62. int index = (int) luaL_checkinteger(L, 2) - 1; // Convert from 1-based index
  63. EdgeShape *e = 0;
  64. luax_catchexcept(L, [&](){ e = c->getChildEdge(index); });
  65. luax_pushtype(L, e);
  66. e->release();
  67. return 1;
  68. }
  69. int w_ChainShape_getVertexCount(lua_State *L)
  70. {
  71. ChainShape *c = luax_checkchainshape(L, 1);
  72. int count = c->getVertexCount();
  73. lua_pushinteger(L, count);
  74. return 1;
  75. }
  76. int w_ChainShape_getPoint(lua_State *L)
  77. {
  78. ChainShape *c = luax_checkchainshape(L, 1);
  79. int index = (int) luaL_checkinteger(L, 2) - 1; // Convert from 1-based index
  80. b2Vec2 v;
  81. luax_catchexcept(L, [&](){ v = c->getPoint(index); });
  82. lua_pushnumber(L, v.x);
  83. lua_pushnumber(L, v.y);
  84. return 2;
  85. }
  86. int w_ChainShape_getNextVertex(lua_State *L)
  87. {
  88. ChainShape *c = luax_checkchainshape(L, 1);
  89. float x, y;
  90. if (c->getNextVertex(x, y))
  91. {
  92. lua_pushnumber(L, x);
  93. lua_pushnumber(L, y);
  94. return 2;
  95. }
  96. return 0;
  97. }
  98. int w_ChainShape_getPreviousVertex(lua_State *L)
  99. {
  100. ChainShape *c = luax_checkchainshape(L, 1);
  101. float x, y;
  102. if (c->getPreviousVertex(x, y))
  103. {
  104. lua_pushnumber(L, x);
  105. lua_pushnumber(L, y);
  106. return 2;
  107. }
  108. return 0;
  109. }
  110. int w_ChainShape_getPoints(lua_State *L)
  111. {
  112. ChainShape *c = luax_checkchainshape(L, 1);
  113. const b2Vec2 *verts = c->getPoints();
  114. int count = c->getVertexCount();
  115. if (!lua_checkstack(L, count*2))
  116. return luaL_error(L, "Too many return values");
  117. for (int i = 0; i < count; i++)
  118. {
  119. b2Vec2 v = Physics::scaleUp(verts[i]);
  120. lua_pushnumber(L, v.x);
  121. lua_pushnumber(L, v.y);
  122. }
  123. return count*2;
  124. }
  125. static const luaL_Reg w_ChainShape_functions[] =
  126. {
  127. { "setNextVertex", w_ChainShape_setNextVertex },
  128. { "setPreviousVertex", w_ChainShape_setPreviousVertex },
  129. { "getNextVertex", w_ChainShape_getNextVertex },
  130. { "getPreviousVertex", w_ChainShape_getPreviousVertex },
  131. { "getChildEdge", w_ChainShape_getChildEdge },
  132. { "getVertexCount", w_ChainShape_getVertexCount },
  133. { "getPoint", w_ChainShape_getPoint },
  134. { "getPoints", w_ChainShape_getPoints },
  135. { 0, 0 }
  136. };
  137. extern "C" int luaopen_chainshape(lua_State *L)
  138. {
  139. return luax_register_type(L, &ChainShape::type, w_Shape_functions, w_ChainShape_functions, nullptr);
  140. }
  141. } // box2d
  142. } // physics
  143. } // love