wrap_ParticleSystem.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. // LOVE
  21. #include "wrap_ParticleSystem.h"
  22. #include "common/Vector.h"
  23. #include "Image.h"
  24. #include "Canvas.h"
  25. #include "wrap_Texture.h"
  26. // C
  27. #include <cstring>
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. ParticleSystem *luax_checkparticlesystem(lua_State *L, int idx)
  33. {
  34. return luax_checktype<ParticleSystem>(L, idx);
  35. }
  36. int w_ParticleSystem_clone(lua_State *L)
  37. {
  38. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  39. ParticleSystem *clone = nullptr;
  40. luax_catchexcept(L, [&](){ clone = t->clone(); });
  41. luax_pushtype(L, clone);
  42. clone->release();
  43. return 1;
  44. }
  45. int w_ParticleSystem_setTexture(lua_State *L)
  46. {
  47. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  48. Texture *tex = luax_checktexture(L, 2);
  49. luax_catchexcept(L, [&](){ t->setTexture(tex); });
  50. return 0;
  51. }
  52. int w_ParticleSystem_getTexture(lua_State *L)
  53. {
  54. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  55. Texture *tex = t->getTexture();
  56. // FIXME: big hack right here.
  57. if (dynamic_cast<Image *>(tex) != nullptr)
  58. luax_pushtype(L, Image::type, tex);
  59. else if (dynamic_cast<Canvas *>(tex) != nullptr)
  60. luax_pushtype(L, Canvas::type, tex);
  61. else
  62. return luaL_error(L, "Unable to determine texture type.");
  63. return 1;
  64. }
  65. int w_ParticleSystem_setBufferSize(lua_State *L)
  66. {
  67. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  68. lua_Number arg1 = luaL_checknumber(L, 2);
  69. if (arg1 < 1.0 || arg1 > ParticleSystem::MAX_PARTICLES)
  70. return luaL_error(L, "Invalid buffer size");
  71. luax_catchexcept(L, [&](){ t->setBufferSize((uint32) arg1); });
  72. return 0;
  73. }
  74. int w_ParticleSystem_getBufferSize(lua_State *L)
  75. {
  76. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  77. lua_pushinteger(L, t->getBufferSize());
  78. return 1;
  79. }
  80. int w_ParticleSystem_setInsertMode(lua_State *L)
  81. {
  82. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  83. ParticleSystem::InsertMode mode;
  84. const char *str = luaL_checkstring(L, 2);
  85. if (!ParticleSystem::getConstant(str, mode))
  86. return luax_enumerror(L, "insert mode", ParticleSystem::getConstants(mode), str);
  87. t->setInsertMode(mode);
  88. return 0;
  89. }
  90. int w_ParticleSystem_getInsertMode(lua_State *L)
  91. {
  92. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  93. ParticleSystem::InsertMode mode;
  94. mode = t->getInsertMode();
  95. const char *str;
  96. if (!ParticleSystem::getConstant(mode, str))
  97. return luaL_error(L, "Unknown insert mode");
  98. lua_pushstring(L, str);
  99. return 1;
  100. }
  101. int w_ParticleSystem_setEmissionRate(lua_State *L)
  102. {
  103. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  104. float arg1 = (float) luaL_checknumber(L, 2);
  105. luax_catchexcept(L, [&](){ t->setEmissionRate(arg1); });
  106. return 0;
  107. }
  108. int w_ParticleSystem_getEmissionRate(lua_State *L)
  109. {
  110. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  111. lua_pushnumber(L, t->getEmissionRate());
  112. return 1;
  113. }
  114. int w_ParticleSystem_setEmitterLifetime(lua_State *L)
  115. {
  116. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  117. float arg1 = (float)luaL_checknumber(L, 2);
  118. t->setEmitterLifetime(arg1);
  119. return 0;
  120. }
  121. int w_ParticleSystem_getEmitterLifetime(lua_State *L)
  122. {
  123. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  124. lua_pushnumber(L, t->getEmitterLifetime());
  125. return 1;
  126. }
  127. int w_ParticleSystem_setParticleLifetime(lua_State *L)
  128. {
  129. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  130. float arg1 = (float)luaL_checknumber(L, 2);
  131. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  132. if (arg1 < 0.0f || arg2 < 0.0f)
  133. return luaL_error(L, "Invalid particle lifetime (must be >= 0)");
  134. t->setParticleLifetime(arg1, arg2);
  135. return 0;
  136. }
  137. int w_ParticleSystem_getParticleLifetime(lua_State *L)
  138. {
  139. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  140. float min, max;
  141. t->getParticleLifetime(min, max);
  142. lua_pushnumber(L, min);
  143. lua_pushnumber(L, max);
  144. return 2;
  145. }
  146. int w_ParticleSystem_setPosition(lua_State *L)
  147. {
  148. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  149. float arg1 = (float)luaL_checknumber(L, 2);
  150. float arg2 = (float)luaL_checknumber(L, 3);
  151. t->setPosition(arg1, arg2);
  152. return 0;
  153. }
  154. int w_ParticleSystem_getPosition(lua_State *L)
  155. {
  156. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  157. love::Vector2 pos = t->getPosition();
  158. lua_pushnumber(L, pos.x);
  159. lua_pushnumber(L, pos.y);
  160. return 2;
  161. }
  162. int w_ParticleSystem_moveTo(lua_State *L)
  163. {
  164. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  165. float arg1 = (float)luaL_checknumber(L, 2);
  166. float arg2 = (float)luaL_checknumber(L, 3);
  167. t->moveTo(arg1, arg2);
  168. return 0;
  169. }
  170. int w_ParticleSystem_setEmissionArea(lua_State *L)
  171. {
  172. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  173. ParticleSystem::AreaSpreadDistribution distribution = ParticleSystem::DISTRIBUTION_NONE;
  174. float x = 0.f, y = 0.f;
  175. float angle = 0.0f;
  176. bool directionRelativeToCenter = false;
  177. const char *str = lua_isnoneornil(L, 2) ? nullptr : luaL_checkstring(L, 2);
  178. if (str && !ParticleSystem::getConstant(str, distribution))
  179. return luax_enumerror(L, "particle distribution", ParticleSystem::getConstants(distribution), str);
  180. if (distribution != ParticleSystem::DISTRIBUTION_NONE)
  181. {
  182. x = (float) luaL_checknumber(L, 3);
  183. y = (float) luaL_checknumber(L, 4);
  184. if (x < 0.0f || y < 0.0f)
  185. return luaL_error(L, "Invalid area spread parameters (must be >= 0)");
  186. angle = (float) luaL_optnumber(L, 5, 0.0f);
  187. directionRelativeToCenter = luax_optboolean(L, 6, false);
  188. }
  189. t->setEmissionArea(distribution, x, y, angle, directionRelativeToCenter);
  190. return 0;
  191. }
  192. int w_ParticleSystem_getEmissionArea(lua_State *L)
  193. {
  194. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  195. love::Vector2 p;
  196. float angle;
  197. bool directionRelativeToCenter;
  198. ParticleSystem::AreaSpreadDistribution distribution = t->getEmissionArea(p, angle, directionRelativeToCenter);
  199. const char *str;
  200. ParticleSystem::getConstant(distribution, str);
  201. lua_pushstring(L, str);
  202. lua_pushnumber(L, p.x);
  203. lua_pushnumber(L, p.y);
  204. lua_pushnumber(L, angle);
  205. luax_pushboolean(L, directionRelativeToCenter);
  206. return 5;
  207. }
  208. int w_ParticleSystem_setDirection(lua_State *L)
  209. {
  210. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  211. float arg1 = (float)luaL_checknumber(L, 2);
  212. t->setDirection(arg1);
  213. return 0;
  214. }
  215. int w_ParticleSystem_getDirection(lua_State *L)
  216. {
  217. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  218. lua_pushnumber(L, t->getDirection());
  219. return 1;
  220. }
  221. int w_ParticleSystem_setSpread(lua_State *L)
  222. {
  223. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  224. float arg1 = (float)luaL_checknumber(L, 2);
  225. t->setSpread(arg1);
  226. return 0;
  227. }
  228. int w_ParticleSystem_getSpread(lua_State *L)
  229. {
  230. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  231. lua_pushnumber(L, t->getSpread());
  232. return 1;
  233. }
  234. int w_ParticleSystem_setSpeed(lua_State *L)
  235. {
  236. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  237. float arg1 = (float)luaL_checknumber(L, 2);
  238. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  239. t->setSpeed(arg1, arg2);
  240. return 0;
  241. }
  242. int w_ParticleSystem_getSpeed(lua_State *L)
  243. {
  244. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  245. float min, max;
  246. t->getSpeed(min, max);
  247. lua_pushnumber(L, min);
  248. lua_pushnumber(L, max);
  249. return 2;
  250. }
  251. int w_ParticleSystem_setLinearAcceleration(lua_State *L)
  252. {
  253. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  254. float xmin = (float) luaL_checknumber(L, 2);
  255. float ymin = (float) luaL_checknumber(L, 3);
  256. float xmax = (float) luaL_optnumber(L, 4, xmin);
  257. float ymax = (float) luaL_optnumber(L, 5, ymin);
  258. t->setLinearAcceleration(xmin, ymin, xmax, ymax);
  259. return 0;
  260. }
  261. int w_ParticleSystem_getLinearAcceleration(lua_State *L)
  262. {
  263. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  264. love::Vector2 min, max;
  265. t->getLinearAcceleration(min, max);
  266. lua_pushnumber(L, min.x);
  267. lua_pushnumber(L, min.y);
  268. lua_pushnumber(L, max.x);
  269. lua_pushnumber(L, max.y);
  270. return 4;
  271. }
  272. int w_ParticleSystem_setRadialAcceleration(lua_State *L)
  273. {
  274. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  275. float arg1 = (float)luaL_checknumber(L, 2);
  276. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  277. t->setRadialAcceleration(arg1, arg2);
  278. return 0;
  279. }
  280. int w_ParticleSystem_getRadialAcceleration(lua_State *L)
  281. {
  282. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  283. float min, max;
  284. t->getRadialAcceleration(min, max);
  285. lua_pushnumber(L, min);
  286. lua_pushnumber(L, max);
  287. return 2;
  288. }
  289. int w_ParticleSystem_setTangentialAcceleration(lua_State *L)
  290. {
  291. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  292. float arg1 = (float)luaL_checknumber(L, 2);
  293. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  294. t->setTangentialAcceleration(arg1, arg2);
  295. return 0;
  296. }
  297. int w_ParticleSystem_getTangentialAcceleration(lua_State *L)
  298. {
  299. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  300. float min, max;
  301. t->getTangentialAcceleration(min, max);
  302. lua_pushnumber(L, min);
  303. lua_pushnumber(L, max);
  304. return 2;
  305. }
  306. int w_ParticleSystem_setLinearDamping(lua_State *L)
  307. {
  308. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  309. float arg1 = (float)luaL_checknumber(L, 2);
  310. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  311. t->setLinearDamping(arg1, arg2);
  312. return 0;
  313. }
  314. int w_ParticleSystem_getLinearDamping(lua_State *L)
  315. {
  316. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  317. float min, max;
  318. t->getLinearDamping(min, max);
  319. lua_pushnumber(L, min);
  320. lua_pushnumber(L, max);
  321. return 2;
  322. }
  323. int w_ParticleSystem_setSizes(lua_State *L)
  324. {
  325. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  326. size_t nSizes = lua_gettop(L) - 1;
  327. if (nSizes > 8)
  328. return luaL_error(L, "At most eight (8) sizes may be used.");
  329. if (nSizes <= 1)
  330. {
  331. float size = luax_checkfloat(L, 2);
  332. t->setSize(size);
  333. }
  334. else
  335. {
  336. std::vector<float> sizes(nSizes);
  337. for (size_t i = 0; i < nSizes; ++i)
  338. sizes[i] = luax_checkfloat(L, (int) (1 + i + 1));
  339. t->setSizes(sizes);
  340. }
  341. return 0;
  342. }
  343. int w_ParticleSystem_getSizes(lua_State *L)
  344. {
  345. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  346. const std::vector<float> &sizes = t->getSizes();
  347. for (size_t i = 0; i < sizes.size(); i++)
  348. lua_pushnumber(L, sizes[i]);
  349. return (int) sizes.size();
  350. }
  351. int w_ParticleSystem_setSizeVariation(lua_State *L)
  352. {
  353. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  354. float arg1 = (float)luaL_checknumber(L, 2);
  355. if (arg1 < 0.0f || arg1 > 1.0f)
  356. return luaL_error(L, "Size variation has to be between 0 and 1, inclusive.");
  357. t->setSizeVariation(arg1);
  358. return 0;
  359. }
  360. int w_ParticleSystem_getSizeVariation(lua_State *L)
  361. {
  362. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  363. lua_pushnumber(L, t->getSizeVariation());
  364. return 1;
  365. }
  366. int w_ParticleSystem_setRotation(lua_State *L)
  367. {
  368. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  369. float arg1 = (float)luaL_checknumber(L, 2);
  370. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  371. t->setRotation(arg1, arg2);
  372. return 0;
  373. }
  374. int w_ParticleSystem_getRotation(lua_State *L)
  375. {
  376. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  377. float min, max;
  378. t->getRotation(min, max);
  379. lua_pushnumber(L, min);
  380. lua_pushnumber(L, max);
  381. return 2;
  382. }
  383. int w_ParticleSystem_setSpin(lua_State *L)
  384. {
  385. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  386. float arg1 = (float)luaL_checknumber(L, 2);
  387. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  388. t->setSpin(arg1, arg2);
  389. return 0;
  390. }
  391. int w_ParticleSystem_getSpin(lua_State *L)
  392. {
  393. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  394. float start, end;
  395. t->getSpin(start, end);
  396. lua_pushnumber(L, start);
  397. lua_pushnumber(L, end);
  398. return 2;
  399. }
  400. int w_ParticleSystem_setSpinVariation(lua_State *L)
  401. {
  402. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  403. float arg1 = (float)luaL_checknumber(L, 2);
  404. t->setSpinVariation(arg1);
  405. return 0;
  406. }
  407. int w_ParticleSystem_getSpinVariation(lua_State *L)
  408. {
  409. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  410. lua_pushnumber(L, t->getSpinVariation());
  411. return 1;
  412. }
  413. int w_ParticleSystem_setOffset(lua_State *L)
  414. {
  415. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  416. float x = (float)luaL_checknumber(L, 2);
  417. float y = (float)luaL_checknumber(L, 3);
  418. t->setOffset(x, y);
  419. return 0;
  420. }
  421. int w_ParticleSystem_getOffset(lua_State *L)
  422. {
  423. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  424. love::Vector2 offset = t->getOffset();
  425. lua_pushnumber(L, offset.x);
  426. lua_pushnumber(L, offset.y);
  427. return 2;
  428. }
  429. int w_ParticleSystem_setColors(lua_State *L)
  430. {
  431. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  432. if (lua_istable(L, 2)) // setColors({r,g,b,a}, {r,g,b,a}, ...)
  433. {
  434. int nColors = (int) lua_gettop(L) - 1;
  435. if (nColors > 8)
  436. return luaL_error(L, "At most eight (8) colors may be used.");
  437. std::vector<Colorf> colors(nColors);
  438. for (int i = 0; i < nColors; i++)
  439. {
  440. luaL_checktype(L, i + 2, LUA_TTABLE);
  441. if (luax_objlen(L, i + 2) < 3)
  442. return luaL_argerror(L, i + 2, "expected 4 color components");
  443. for (int j = 0; j < 4; j++)
  444. // push args[i+2][j+1] onto the stack
  445. lua_rawgeti(L, i + 2, j + 1);
  446. colors[i].r = (float) luaL_checknumber(L, -4);
  447. colors[i].g = (float) luaL_checknumber(L, -3);
  448. colors[i].b = (float) luaL_checknumber(L, -2);
  449. colors[i].a = (float) luaL_optnumber(L, -1, 1.0);
  450. // pop the color components from the stack
  451. lua_pop(L, 4);
  452. }
  453. t->setColor(colors);
  454. }
  455. else // setColors(r,g,b,a, r,g,b,a, ...)
  456. {
  457. int cargs = lua_gettop(L) - 1;
  458. int nColors = (cargs + 3) / 4; // nColors = ceil(color_args / 4)
  459. if (cargs != 3 && (cargs % 4 != 0 || cargs == 0))
  460. return luaL_error(L, "Expected red, green, blue, and alpha. Only got %d of 4 components.", cargs % 4);
  461. if (nColors > 8)
  462. return luaL_error(L, "At most eight (8) colors may be used.");
  463. std::vector<Colorf> colors(nColors);
  464. for (int i = 0; i < nColors; ++i)
  465. {
  466. colors[i].r = (float) luaL_checknumber(L, 1 + i*4 + 1);
  467. colors[i].g = (float) luaL_checknumber(L, 1 + i*4 + 2);
  468. colors[i].b = (float) luaL_checknumber(L, 1 + i*4 + 3);
  469. colors[i].a = (float) luaL_checknumber(L, 1 + i*4 + 4);
  470. }
  471. t->setColor(colors);
  472. }
  473. return 0;
  474. }
  475. int w_ParticleSystem_getColors(lua_State *L)
  476. {
  477. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  478. const std::vector<Colorf> &colors =t->getColor();
  479. for (size_t i = 0; i < colors.size(); i++)
  480. {
  481. lua_createtable(L, 4, 0);
  482. lua_pushnumber(L, colors[i].r);
  483. lua_rawseti(L, -2, 1);
  484. lua_pushnumber(L, colors[i].g);
  485. lua_rawseti(L, -2, 2);
  486. lua_pushnumber(L, colors[i].b);
  487. lua_rawseti(L, -2, 3);
  488. lua_pushnumber(L, colors[i].a);
  489. lua_rawseti(L, -2, 4);
  490. }
  491. return (int) colors.size();
  492. }
  493. int w_ParticleSystem_setQuads(lua_State *L)
  494. {
  495. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  496. std::vector<Quad *> quads;
  497. if (lua_istable(L, 2))
  498. {
  499. for (int i = 1; i <= (int) luax_objlen(L, 2); i++)
  500. {
  501. lua_rawgeti(L, 2, i);
  502. Quad *q = luax_checktype<Quad>(L, -1);
  503. quads.push_back(q);
  504. lua_pop(L, 1);
  505. }
  506. }
  507. else
  508. {
  509. for (int i = 2; i <= lua_gettop(L); i++)
  510. {
  511. Quad *q = luax_checktype<Quad>(L, i);
  512. quads.push_back(q);
  513. }
  514. }
  515. t->setQuads(quads);
  516. return 0;
  517. }
  518. int w_ParticleSystem_getQuads(lua_State *L)
  519. {
  520. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  521. const std::vector<Quad *> quads = t->getQuads();
  522. lua_createtable(L, (int) quads.size(), 0);
  523. for (int i = 0; i < (int) quads.size(); i++)
  524. {
  525. luax_pushtype(L, quads[i]);
  526. lua_rawseti(L, -2, i + 1);
  527. }
  528. return 1;
  529. }
  530. int w_ParticleSystem_setRelativeRotation(lua_State *L)
  531. {
  532. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  533. t->setRelativeRotation(luax_checkboolean(L, 2));
  534. return 0;
  535. }
  536. int w_ParticleSystem_hasRelativeRotation(lua_State *L)
  537. {
  538. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  539. luax_pushboolean(L, t->hasRelativeRotation());
  540. return 1;
  541. }
  542. int w_ParticleSystem_getCount(lua_State *L)
  543. {
  544. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  545. lua_pushnumber(L, t->getCount());
  546. return 1;
  547. }
  548. int w_ParticleSystem_start(lua_State *L)
  549. {
  550. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  551. t->start();
  552. return 0;
  553. }
  554. int w_ParticleSystem_stop(lua_State *L)
  555. {
  556. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  557. t->stop();
  558. return 0;
  559. }
  560. int w_ParticleSystem_pause(lua_State *L)
  561. {
  562. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  563. t->pause();
  564. return 0;
  565. }
  566. int w_ParticleSystem_reset(lua_State *L)
  567. {
  568. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  569. t->reset();
  570. return 0;
  571. }
  572. int w_ParticleSystem_emit(lua_State *L)
  573. {
  574. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  575. int num = (int) luaL_checkinteger(L, 2);
  576. t->emit(num);
  577. return 0;
  578. }
  579. int w_ParticleSystem_isActive(lua_State *L)
  580. {
  581. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  582. luax_pushboolean(L, t->isActive());
  583. return 1;
  584. }
  585. int w_ParticleSystem_isPaused(lua_State *L)
  586. {
  587. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  588. luax_pushboolean(L, t->isPaused());
  589. return 1;
  590. }
  591. int w_ParticleSystem_isStopped(lua_State *L)
  592. {
  593. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  594. luax_pushboolean(L, t->isStopped());
  595. return 1;
  596. }
  597. int w_ParticleSystem_update(lua_State *L)
  598. {
  599. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  600. float dt = (float)luaL_checknumber(L, 2);
  601. t->update(dt);
  602. return 0;
  603. }
  604. // Deprecated functions.
  605. int w_ParticleSystem_setAreaSpread(lua_State *L)
  606. {
  607. luax_markdeprecated(L, "ParticleSystem:setAreaSpread", API_METHOD, DEPRECATED_REPLACED, "ParticleSystem:setEmissionArea");
  608. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  609. ParticleSystem::AreaSpreadDistribution distribution = ParticleSystem::DISTRIBUTION_NONE;
  610. float x = 0.f, y = 0.f;
  611. const char *str = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
  612. if (str && !ParticleSystem::getConstant(str, distribution))
  613. return luax_enumerror(L, "particle distribution", ParticleSystem::getConstants(distribution), str);
  614. if (distribution != ParticleSystem::DISTRIBUTION_NONE)
  615. {
  616. x = (float) luaL_checknumber(L, 3);
  617. y = (float) luaL_checknumber(L, 4);
  618. if (x < 0.0f || y < 0.0f)
  619. return luaL_error(L, "Invalid area spread parameters (must be >= 0)");
  620. }
  621. t->setEmissionArea(distribution, x, y, 0.0f, false);
  622. return 0;
  623. }
  624. int w_ParticleSystem_getAreaSpread(lua_State *L)
  625. {
  626. luax_markdeprecated(L, "ParticleSystem:getAreaSpread", API_METHOD, DEPRECATED_REPLACED, "ParticleSystem:getEmissionArea");
  627. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  628. love::Vector2 p;
  629. float angle;
  630. bool unused;
  631. ParticleSystem::AreaSpreadDistribution distribution = t->getEmissionArea(p, angle, unused);
  632. const char *str;
  633. ParticleSystem::getConstant(distribution, str);
  634. lua_pushstring(L, str);
  635. lua_pushnumber(L, p.x);
  636. lua_pushnumber(L, p.y);
  637. return 3;
  638. }
  639. static const luaL_Reg w_ParticleSystem_functions[] =
  640. {
  641. { "clone", w_ParticleSystem_clone },
  642. { "setTexture", w_ParticleSystem_setTexture },
  643. { "getTexture", w_ParticleSystem_getTexture },
  644. { "setBufferSize", w_ParticleSystem_setBufferSize },
  645. { "getBufferSize", w_ParticleSystem_getBufferSize },
  646. { "setInsertMode", w_ParticleSystem_setInsertMode },
  647. { "getInsertMode", w_ParticleSystem_getInsertMode },
  648. { "setEmissionRate", w_ParticleSystem_setEmissionRate },
  649. { "getEmissionRate", w_ParticleSystem_getEmissionRate },
  650. { "setEmitterLifetime", w_ParticleSystem_setEmitterLifetime },
  651. { "getEmitterLifetime", w_ParticleSystem_getEmitterLifetime },
  652. { "setParticleLifetime", w_ParticleSystem_setParticleLifetime },
  653. { "getParticleLifetime", w_ParticleSystem_getParticleLifetime },
  654. { "setPosition", w_ParticleSystem_setPosition },
  655. { "getPosition", w_ParticleSystem_getPosition },
  656. { "moveTo", w_ParticleSystem_moveTo },
  657. { "setEmissionArea", w_ParticleSystem_setEmissionArea },
  658. { "getEmissionArea", w_ParticleSystem_getEmissionArea },
  659. { "setDirection", w_ParticleSystem_setDirection },
  660. { "getDirection", w_ParticleSystem_getDirection },
  661. { "setSpread", w_ParticleSystem_setSpread },
  662. { "getSpread", w_ParticleSystem_getSpread },
  663. { "setSpeed", w_ParticleSystem_setSpeed },
  664. { "getSpeed", w_ParticleSystem_getSpeed },
  665. { "setLinearAcceleration", w_ParticleSystem_setLinearAcceleration },
  666. { "getLinearAcceleration", w_ParticleSystem_getLinearAcceleration },
  667. { "setRadialAcceleration", w_ParticleSystem_setRadialAcceleration },
  668. { "getRadialAcceleration", w_ParticleSystem_getRadialAcceleration },
  669. { "setTangentialAcceleration", w_ParticleSystem_setTangentialAcceleration },
  670. { "getTangentialAcceleration", w_ParticleSystem_getTangentialAcceleration },
  671. { "setLinearDamping", w_ParticleSystem_setLinearDamping },
  672. { "getLinearDamping", w_ParticleSystem_getLinearDamping },
  673. { "setSizes", w_ParticleSystem_setSizes },
  674. { "getSizes", w_ParticleSystem_getSizes },
  675. { "setSizeVariation", w_ParticleSystem_setSizeVariation },
  676. { "getSizeVariation", w_ParticleSystem_getSizeVariation },
  677. { "setRotation", w_ParticleSystem_setRotation },
  678. { "getRotation", w_ParticleSystem_getRotation },
  679. { "setSpin", w_ParticleSystem_setSpin },
  680. { "getSpin", w_ParticleSystem_getSpin },
  681. { "setSpinVariation", w_ParticleSystem_setSpinVariation },
  682. { "getSpinVariation", w_ParticleSystem_getSpinVariation },
  683. { "setColors", w_ParticleSystem_setColors },
  684. { "getColors", w_ParticleSystem_getColors },
  685. { "setQuads", w_ParticleSystem_setQuads },
  686. { "getQuads", w_ParticleSystem_getQuads },
  687. { "setOffset", w_ParticleSystem_setOffset },
  688. { "getOffset", w_ParticleSystem_getOffset },
  689. { "setRelativeRotation", w_ParticleSystem_setRelativeRotation },
  690. { "hasRelativeRotation", w_ParticleSystem_hasRelativeRotation },
  691. { "getCount", w_ParticleSystem_getCount },
  692. { "start", w_ParticleSystem_start },
  693. { "stop", w_ParticleSystem_stop },
  694. { "pause", w_ParticleSystem_pause },
  695. { "reset", w_ParticleSystem_reset },
  696. { "emit", w_ParticleSystem_emit },
  697. { "isActive", w_ParticleSystem_isActive },
  698. { "isPaused", w_ParticleSystem_isPaused },
  699. { "isStopped", w_ParticleSystem_isStopped },
  700. { "update", w_ParticleSystem_update },
  701. // Deprecated.
  702. { "setAreaSpread", w_ParticleSystem_setAreaSpread },
  703. { "getAreaSpread", w_ParticleSystem_getAreaSpread },
  704. { 0, 0 }
  705. };
  706. extern "C" int luaopen_particlesystem(lua_State *L)
  707. {
  708. return luax_register_type(L, &ParticleSystem::type, w_ParticleSystem_functions, nullptr);
  709. }
  710. } // graphics
  711. } // love