wrap_ChainShape.cpp 4.0 KB

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