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 
21 #include "wrap_Fixture.h"
22 #include "common/StringMap.h"
23 
24 namespace love
25 {
26 namespace physics
27 {
28 namespace box2d
29 {
30 
luax_checkfixture(lua_State * L,int idx)31 Fixture *luax_checkfixture(lua_State *L, int idx)
32 {
33 	Fixture *f = luax_checktype<Fixture>(L, idx, PHYSICS_FIXTURE_ID);
34 	if (!f->isValid())
35 		luaL_error(L, "Attempt to use destroyed fixture.");
36 	return f;
37 }
38 
w_Fixture_getType(lua_State * L)39 int w_Fixture_getType(lua_State *L)
40 {
41 	Fixture *t = luax_checkfixture(L, 1);
42 	const char *type = "";
43 	Shape::getConstant(t->getType(), type);
44 	lua_pushstring(L, type);
45 	return 1;
46 }
47 
w_Fixture_setFriction(lua_State * L)48 int w_Fixture_setFriction(lua_State *L)
49 {
50 	Fixture *t = luax_checkfixture(L, 1);
51 	float arg1 = (float)luaL_checknumber(L, 2);
52 	t->setFriction(arg1);
53 	return 0;
54 }
55 
w_Fixture_setRestitution(lua_State * L)56 int w_Fixture_setRestitution(lua_State *L)
57 {
58 	Fixture *t = luax_checkfixture(L, 1);
59 	float arg1 = (float)luaL_checknumber(L, 2);
60 	t->setRestitution(arg1);
61 	return 0;
62 }
63 
w_Fixture_setDensity(lua_State * L)64 int w_Fixture_setDensity(lua_State *L)
65 {
66 	Fixture *t = luax_checkfixture(L, 1);
67 	float arg1 = (float)luaL_checknumber(L, 2);
68 	luax_catchexcept(L, [&](){ t->setDensity(arg1); });
69 	return 0;
70 }
71 
w_Fixture_setSensor(lua_State * L)72 int w_Fixture_setSensor(lua_State *L)
73 {
74 	Fixture *t = luax_checkfixture(L, 1);
75 	bool arg1 = luax_toboolean(L, 2);
76 	t->setSensor(arg1);
77 	return 0;
78 }
79 
w_Fixture_getFriction(lua_State * L)80 int w_Fixture_getFriction(lua_State *L)
81 {
82 	Fixture *t = luax_checkfixture(L, 1);
83 	lua_pushnumber(L, t->getFriction());
84 	return 1;
85 }
86 
w_Fixture_getRestitution(lua_State * L)87 int w_Fixture_getRestitution(lua_State *L)
88 {
89 	Fixture *t = luax_checkfixture(L, 1);
90 	lua_pushnumber(L, t->getRestitution());
91 	return 1;
92 }
93 
w_Fixture_getDensity(lua_State * L)94 int w_Fixture_getDensity(lua_State *L)
95 {
96 	Fixture *t = luax_checkfixture(L, 1);
97 	lua_pushnumber(L, t->getDensity());
98 	return 1;
99 }
100 
w_Fixture_isSensor(lua_State * L)101 int w_Fixture_isSensor(lua_State *L)
102 {
103 	Fixture *t = luax_checkfixture(L, 1);
104 	luax_pushboolean(L, t->isSensor());
105 	return 1;
106 }
107 
w_Fixture_getBody(lua_State * L)108 int w_Fixture_getBody(lua_State *L)
109 {
110 	Fixture *t = luax_checkfixture(L, 1);
111 	Body *body = t->getBody();
112 	if (body == 0)
113 		return 0;
114 	luax_pushtype(L, PHYSICS_BODY_ID, body);
115 	return 1;
116 }
117 
w_Fixture_getShape(lua_State * L)118 int w_Fixture_getShape(lua_State *L)
119 {
120 	Fixture *t = luax_checkfixture(L, 1);
121 	StrongRef<Shape> shape(t->getShape(), Acquire::NORETAIN);
122 	if (shape.get() == nullptr)
123 		return 0;
124 	switch (shape->getType())
125 	{
126 	case Shape::SHAPE_EDGE:
127 		luax_pushtype(L, PHYSICS_EDGE_SHAPE_ID, shape);
128 		break;
129 	case Shape::SHAPE_CHAIN:
130 		luax_pushtype(L, PHYSICS_CHAIN_SHAPE_ID, shape);
131 		break;
132 	case Shape::SHAPE_CIRCLE:
133 		luax_pushtype(L, PHYSICS_CIRCLE_SHAPE_ID, shape);
134 		break;
135 	case Shape::SHAPE_POLYGON:
136 		luax_pushtype(L, PHYSICS_POLYGON_SHAPE_ID, shape);
137 		break;
138 	default:
139 		luax_pushtype(L, PHYSICS_SHAPE_ID, shape);
140 		break;
141 	}
142 	return 1;
143 }
144 
w_Fixture_testPoint(lua_State * L)145 int w_Fixture_testPoint(lua_State *L)
146 {
147 	Fixture *t = luax_checkfixture(L, 1);
148 	float x = (float)luaL_checknumber(L, 2);
149 	float y = (float)luaL_checknumber(L, 3);
150 	luax_pushboolean(L, t->testPoint(x, y));
151 	return 1;
152 }
153 
w_Fixture_rayCast(lua_State * L)154 int w_Fixture_rayCast(lua_State *L)
155 {
156 	Fixture *t = luax_checkfixture(L, 1);
157 	lua_remove(L, 1);
158 	int ret = 0;
159 	luax_catchexcept(L, [&](){ ret = t->rayCast(L); });
160 	return ret;
161 }
162 
w_Fixture_setFilterData(lua_State * L)163 int w_Fixture_setFilterData(lua_State *L)
164 {
165 	Fixture *t = luax_checkfixture(L, 1);
166 	int v[3];
167 	v[0] = (int) luaL_checknumber(L, 2);
168 	v[1] = (int) luaL_checknumber(L, 3);
169 	v[2] = (int) luaL_checknumber(L, 4);
170 	t->setFilterData(v);
171 	return 0;
172 }
173 
w_Fixture_getFilterData(lua_State * L)174 int w_Fixture_getFilterData(lua_State *L)
175 {
176 	Fixture *t = luax_checkfixture(L, 1);
177 	int v[3];
178 	t->getFilterData(v);
179 	lua_pushinteger(L, v[0]);
180 	lua_pushinteger(L, v[1]);
181 	lua_pushinteger(L, v[2]);
182 	return 3;
183 }
184 
w_Fixture_setCategory(lua_State * L)185 int w_Fixture_setCategory(lua_State *L)
186 {
187 	Fixture *t = luax_checkfixture(L, 1);
188 	lua_remove(L, 1);
189 	return t->setCategory(L);
190 }
191 
w_Fixture_getCategory(lua_State * L)192 int w_Fixture_getCategory(lua_State *L)
193 {
194 	Fixture *t = luax_checkfixture(L, 1);
195 	lua_remove(L, 1);
196 	return t->getCategory(L);
197 }
198 
w_Fixture_setMask(lua_State * L)199 int w_Fixture_setMask(lua_State *L)
200 {
201 	Fixture *t = luax_checkfixture(L, 1);
202 	lua_remove(L, 1);
203 	return t->setMask(L);
204 }
205 
w_Fixture_getMask(lua_State * L)206 int w_Fixture_getMask(lua_State *L)
207 {
208 	Fixture *t = luax_checkfixture(L, 1);
209 	lua_remove(L, 1);
210 	return t->getMask(L);
211 }
212 
w_Fixture_setUserData(lua_State * L)213 int w_Fixture_setUserData(lua_State *L)
214 {
215 	Fixture *t = luax_checkfixture(L, 1);
216 	lua_remove(L, 1);
217 	return t->setUserData(L);
218 }
219 
w_Fixture_getUserData(lua_State * L)220 int w_Fixture_getUserData(lua_State *L)
221 {
222 	Fixture *t = luax_checkfixture(L, 1);
223 	lua_remove(L, 1);
224 	return t->getUserData(L);
225 }
226 
w_Fixture_getBoundingBox(lua_State * L)227 int w_Fixture_getBoundingBox(lua_State *L)
228 {
229 	Fixture *t = luax_checkfixture(L, 1);
230 	lua_remove(L, 1);
231 	return t->getBoundingBox(L);
232 }
233 
w_Fixture_getMassData(lua_State * L)234 int w_Fixture_getMassData(lua_State *L)
235 {
236 	Fixture *t = luax_checkfixture(L, 1);
237 	lua_remove(L, 1);
238 	return t->getMassData(L);
239 }
240 
w_Fixture_getGroupIndex(lua_State * L)241 int w_Fixture_getGroupIndex(lua_State *L)
242 {
243 	Fixture *t = luax_checkfixture(L, 1);
244 	int i = t->getGroupIndex();
245 	lua_pushinteger(L, i);
246 	return 1;
247 }
248 
w_Fixture_setGroupIndex(lua_State * L)249 int w_Fixture_setGroupIndex(lua_State *L)
250 {
251 	Fixture *t = luax_checkfixture(L, 1);
252 	int i = (int) luaL_checknumber(L, 2);
253 	t->setGroupIndex(i);
254 	return 0;
255 }
256 
w_Fixture_destroy(lua_State * L)257 int w_Fixture_destroy(lua_State *L)
258 {
259 	Fixture *t = luax_checkfixture(L, 1);
260 	luax_catchexcept(L, [&](){ t->destroy(); });
261 	return 0;
262 }
263 
w_Fixture_isDestroyed(lua_State * L)264 int w_Fixture_isDestroyed(lua_State *L)
265 {
266 	Fixture *f = luax_checktype<Fixture>(L, 1, PHYSICS_FIXTURE_ID);
267 	luax_pushboolean(L, !f->isValid());
268 	return 1;
269 }
270 
271 static const luaL_Reg w_Fixture_functions[] =
272 {
273 	{ "getType", w_Fixture_getType },
274 	{ "setFriction", w_Fixture_setFriction },
275 	{ "setRestitution", w_Fixture_setRestitution },
276 	{ "setDensity", w_Fixture_setDensity },
277 	{ "setSensor", w_Fixture_setSensor },
278 	{ "getFriction", w_Fixture_getFriction },
279 	{ "getRestitution", w_Fixture_getRestitution },
280 	{ "getDensity", w_Fixture_getDensity },
281 	{ "getBody", w_Fixture_getBody },
282 	{ "getShape", w_Fixture_getShape },
283 	{ "isSensor", w_Fixture_isSensor },
284 	{ "testPoint", w_Fixture_testPoint },
285 	{ "rayCast", w_Fixture_rayCast },
286 	{ "setFilterData", w_Fixture_setFilterData },
287 	{ "getFilterData", w_Fixture_getFilterData },
288 	{ "setCategory", w_Fixture_setCategory },
289 	{ "getCategory", w_Fixture_getCategory },
290 	{ "setMask", w_Fixture_setMask },
291 	{ "getMask", w_Fixture_getMask },
292 	{ "setUserData", w_Fixture_setUserData },
293 	{ "getUserData", w_Fixture_getUserData },
294 	{ "getBoundingBox", w_Fixture_getBoundingBox },
295 	{ "getMassData", w_Fixture_getMassData },
296 	{ "getGroupIndex", w_Fixture_getGroupIndex },
297 	{ "setGroupIndex", w_Fixture_setGroupIndex },
298 	{ "destroy", w_Fixture_destroy },
299 	{ "isDestroyed", w_Fixture_isDestroyed },
300 	{ 0, 0 }
301 };
302 
luaopen_fixture(lua_State * L)303 extern "C" int luaopen_fixture(lua_State *L)
304 {
305 	return luax_register_type(L, PHYSICS_FIXTURE_ID, "Fixture", w_Fixture_functions, nullptr);
306 }
307 
308 } // box2d
309 } // physics
310 } // love
311 
312