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