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