wrap_ParticleSystem.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /**
  2. * Copyright (c) 2006-2013 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_ParticleSystem.h"
  21. #include "wrap_Quad.h"
  22. #include "common/Vector.h"
  23. #include <cstring>
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace opengl
  29. {
  30. ParticleSystem *luax_checkparticlesystem(lua_State *L, int idx)
  31. {
  32. return luax_checktype<ParticleSystem>(L, idx, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T);
  33. }
  34. int w_ParticleSystem_setSprite(lua_State *L)
  35. {
  36. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  37. Image *i = luax_checkimage(L, 2);
  38. t->setSprite(i);
  39. return 0;
  40. }
  41. int w_ParticleSystem_setBufferSize(lua_State *L)
  42. {
  43. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  44. int arg1 = luaL_checkint(L, 2);
  45. t->setBufferSize((unsigned int)arg1);
  46. return 0;
  47. }
  48. int w_ParticleSystem_setEmissionRate(lua_State *L)
  49. {
  50. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  51. int arg1 = luaL_checkint(L, 2);
  52. t->setEmissionRate((unsigned int)arg1);
  53. return 0;
  54. }
  55. int w_ParticleSystem_setLifetime(lua_State *L)
  56. {
  57. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  58. float arg1 = (float)luaL_checknumber(L, 2);
  59. t->setLifetime(arg1);
  60. return 0;
  61. }
  62. int w_ParticleSystem_setParticleLife(lua_State *L)
  63. {
  64. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  65. float arg1 = (float)luaL_checknumber(L, 2);
  66. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  67. t->setParticleLife(arg1, arg2);
  68. return 0;
  69. }
  70. int w_ParticleSystem_setPosition(lua_State *L)
  71. {
  72. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  73. float arg1 = (float)luaL_checknumber(L, 2);
  74. float arg2 = (float)luaL_checknumber(L, 3);
  75. t->setPosition(arg1, arg2);
  76. return 0;
  77. }
  78. int w_ParticleSystem_setAreaSpread(lua_State *L)
  79. {
  80. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  81. ParticleSystem::AreaSpreadDistribution distribution;
  82. const char *str = luaL_checkstring(L, 2);
  83. if (!ParticleSystem::getConstant(str, distribution))
  84. return luaL_error(L, "Invalid distribution: '%s'", str);
  85. float x = (float)luaL_checknumber(L, 3);
  86. float y = (float)luaL_checknumber(L, 4);
  87. if (x < 0.0f || y < 0.0f)
  88. return luaL_error(L, "Invalid area spread parameters (must be >= 0)");
  89. t->setAreaSpread(distribution, x, y);
  90. return 0;
  91. }
  92. int w_ParticleSystem_setDirection(lua_State *L)
  93. {
  94. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  95. float arg1 = (float)luaL_checknumber(L, 2);
  96. t->setDirection(arg1);
  97. return 0;
  98. }
  99. int w_ParticleSystem_setSpread(lua_State *L)
  100. {
  101. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  102. float arg1 = (float)luaL_checknumber(L, 2);
  103. t->setSpread(arg1);
  104. return 0;
  105. }
  106. int w_ParticleSystem_setRelativeDirection(lua_State *L)
  107. {
  108. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  109. bool arg1 = (bool)luax_toboolean(L, 2);
  110. t->setRelativeDirection(arg1);
  111. return 0;
  112. }
  113. int w_ParticleSystem_setSpeed(lua_State *L)
  114. {
  115. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  116. float arg1 = (float)luaL_checknumber(L, 2);
  117. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  118. t->setSpeed(arg1, arg2);
  119. return 0;
  120. }
  121. int w_ParticleSystem_setGravity(lua_State *L)
  122. {
  123. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  124. float arg1 = (float)luaL_checknumber(L, 2);
  125. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  126. t->setGravity(arg1, arg2);
  127. return 0;
  128. }
  129. int w_ParticleSystem_setRadialAcceleration(lua_State *L)
  130. {
  131. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  132. float arg1 = (float)luaL_checknumber(L, 2);
  133. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  134. t->setRadialAcceleration(arg1, arg2);
  135. return 0;
  136. }
  137. int w_ParticleSystem_setTangentialAcceleration(lua_State *L)
  138. {
  139. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  140. float arg1 = (float)luaL_checknumber(L, 2);
  141. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  142. t->setTangentialAcceleration(arg1, arg2);
  143. return 0;
  144. }
  145. int w_ParticleSystem_setSizes(lua_State *L)
  146. {
  147. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  148. size_t nSizes = lua_gettop(L) - 1;
  149. if (nSizes > 8)
  150. return luaL_error(L, "At most eight (8) sizes may be used.");
  151. if (nSizes <= 1)
  152. {
  153. float size = luax_checkfloat(L, 2);
  154. t->setSize(size);
  155. }
  156. else
  157. {
  158. std::vector<float> sizes(nSizes);
  159. for (size_t i = 0; i < nSizes; ++i)
  160. sizes[i] = luax_checkfloat(L, 1 + i + 1);
  161. t->setSize(sizes);
  162. }
  163. return 0;
  164. }
  165. int w_ParticleSystem_setSizeVariation(lua_State *L)
  166. {
  167. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  168. float arg1 = (float)luaL_checknumber(L, 2);
  169. if (arg1 < 0.0f || arg1 > 1.0f)
  170. return luaL_error(L, "Size variation has to be between 0 and 1, inclusive.");
  171. t->setSizeVariation(arg1);
  172. return 0;
  173. }
  174. int w_ParticleSystem_setRotation(lua_State *L)
  175. {
  176. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  177. float arg1 = (float)luaL_checknumber(L, 2);
  178. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  179. t->setRotation(arg1, arg2);
  180. return 0;
  181. }
  182. int w_ParticleSystem_setSpin(lua_State *L)
  183. {
  184. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  185. float arg1 = (float)luaL_checknumber(L, 2);
  186. float arg2 = (float)luaL_optnumber(L, 3, arg1);
  187. float arg3 = (float)luaL_optnumber(L, 4, 0);
  188. t->setSpin(arg1, arg2, arg3);
  189. return 0;
  190. }
  191. int w_ParticleSystem_setSpinVariation(lua_State *L)
  192. {
  193. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  194. float arg1 = (float)luaL_checknumber(L, 2);
  195. t->setSpinVariation(arg1);
  196. return 0;
  197. }
  198. int w_ParticleSystem_setColors(lua_State *L)
  199. {
  200. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  201. int cargs = lua_gettop(L) - 1;
  202. size_t nColors = (cargs + 3) / 4; // nColors = ceil(color_args / 4)
  203. if (cargs % 4 != 0 || cargs == 0)
  204. return luaL_error(L, "Expected red, green, blue, and alpha. Only got %d of 4 components.", cargs % 4);
  205. if (nColors > 8)
  206. return luaL_error(L, "At most eight (8) colors may be used.");
  207. if (nColors == 1)
  208. {
  209. int r = luaL_checkint(L, 2);
  210. int g = luaL_checkint(L, 3);
  211. int b = luaL_checkint(L, 4);
  212. int a = luaL_checkint(L, 5);
  213. t->setColor(Color(r,g,b,a));
  214. }
  215. else
  216. {
  217. std::vector<Color> colors(nColors);
  218. for (size_t i = 0; i < nColors; ++i)
  219. {
  220. int r = luaL_checkint(L, 1 + i*4 + 1);
  221. int g = luaL_checkint(L, 1 + i*4 + 2);
  222. int b = luaL_checkint(L, 1 + i*4 + 3);
  223. int a = luaL_checkint(L, 1 + i*4 + 4);
  224. colors[i] = Color(r,g,b,a);
  225. }
  226. t->setColor(colors);
  227. }
  228. return 0;
  229. }
  230. int w_ParticleSystem_setQuads(lua_State *L)
  231. {
  232. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  233. int nQuads = lua_gettop(L) - 1;
  234. if (lua_istable(L, 2))
  235. nQuads = lua_objlen(L, 2);
  236. // Remove all quads if no argument is given.
  237. if (nQuads == 0)
  238. {
  239. t->setQuads();
  240. return 0;
  241. }
  242. std::vector<love::graphics::Quad *> quads(nQuads);
  243. if (lua_istable(L, 2))
  244. {
  245. for (int i = 0; i < nQuads; i++)
  246. {
  247. lua_pushnumber(L, i + 1); // array index
  248. lua_gettable(L, 2);
  249. quads[i] = luax_checkframe(L, -1);
  250. lua_pop(L, 1);
  251. }
  252. }
  253. else
  254. {
  255. for (int i = 0; i < nQuads; i++)
  256. quads[i] = luax_checkframe(L, i + 2);
  257. }
  258. t->setQuads(quads);
  259. return 0;
  260. }
  261. int w_ParticleSystem_setOffset(lua_State *L)
  262. {
  263. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  264. float x = (float)luaL_checknumber(L, 2);
  265. float y = (float)luaL_checknumber(L, 3);
  266. t->setOffset(x, y);
  267. return 0;
  268. }
  269. int w_ParticleSystem_getX(lua_State *L)
  270. {
  271. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  272. lua_pushnumber(L, t->getX());
  273. return 1;
  274. }
  275. int w_ParticleSystem_getY(lua_State *L)
  276. {
  277. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  278. lua_pushnumber(L, t->getY());
  279. return 1;
  280. }
  281. int w_ParticleSystem_getPosition(lua_State *L)
  282. {
  283. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  284. const love::Vector &p = t->getPosition();
  285. lua_pushnumber(L, p.x);
  286. lua_pushnumber(L, p.y);
  287. return 2;
  288. }
  289. int w_ParticleSystem_getAreaSpread(lua_State *L)
  290. {
  291. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  292. ParticleSystem::AreaSpreadDistribution distribution = t-> getAreaSpreadDistribution();
  293. const char *str;
  294. ParticleSystem::getConstant(distribution, str);
  295. const love::Vector &p = t->getAreaSpreadParameters();
  296. lua_pushstring(L, str);
  297. lua_pushnumber(L, p.x);
  298. lua_pushnumber(L, p.y);
  299. return 3;
  300. }
  301. int w_ParticleSystem_getDirection(lua_State *L)
  302. {
  303. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  304. lua_pushnumber(L, t->getDirection());
  305. return 1;
  306. }
  307. int w_ParticleSystem_getSpread(lua_State *L)
  308. {
  309. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  310. lua_pushnumber(L, t->getSpread());
  311. return 1;
  312. }
  313. int w_ParticleSystem_getOffsetX(lua_State *L)
  314. {
  315. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  316. lua_pushnumber(L, t->getOffsetX());
  317. return 1;
  318. }
  319. int w_ParticleSystem_getOffsetY(lua_State *L)
  320. {
  321. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  322. lua_pushnumber(L, t->getOffsetY());
  323. return 1;
  324. }
  325. int w_ParticleSystem_count(lua_State *L)
  326. {
  327. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  328. lua_pushnumber(L, t->count());
  329. return 1;
  330. }
  331. int w_ParticleSystem_start(lua_State *L)
  332. {
  333. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  334. t->start();
  335. return 0;
  336. }
  337. int w_ParticleSystem_stop(lua_State *L)
  338. {
  339. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  340. t->stop();
  341. return 0;
  342. }
  343. int w_ParticleSystem_pause(lua_State *L)
  344. {
  345. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  346. t->pause();
  347. return 0;
  348. }
  349. int w_ParticleSystem_reset(lua_State *L)
  350. {
  351. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  352. t->reset();
  353. return 0;
  354. }
  355. int w_ParticleSystem_isActive(lua_State *L)
  356. {
  357. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  358. luax_pushboolean(L, t->isActive());
  359. return 1;
  360. }
  361. int w_ParticleSystem_isEmpty(lua_State *L)
  362. {
  363. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  364. luax_pushboolean(L, t->isEmpty());
  365. return 1;
  366. }
  367. int w_ParticleSystem_isFull(lua_State *L)
  368. {
  369. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  370. luax_pushboolean(L, t->isFull());
  371. return 1;
  372. }
  373. int w_ParticleSystem_update(lua_State *L)
  374. {
  375. ParticleSystem *t = luax_checkparticlesystem(L, 1);
  376. float dt = (float)luaL_checknumber(L, 2);
  377. t->update(dt);
  378. return 0;
  379. }
  380. static const luaL_Reg functions[] =
  381. {
  382. { "setSprite", w_ParticleSystem_setSprite },
  383. { "setBufferSize", w_ParticleSystem_setBufferSize },
  384. { "setEmissionRate", w_ParticleSystem_setEmissionRate },
  385. { "setLifetime", w_ParticleSystem_setLifetime },
  386. { "setParticleLife", w_ParticleSystem_setParticleLife },
  387. { "setPosition", w_ParticleSystem_setPosition },
  388. { "setAreaSpread", w_ParticleSystem_setAreaSpread },
  389. { "setDirection", w_ParticleSystem_setDirection },
  390. { "setSpread", w_ParticleSystem_setSpread },
  391. { "setRelativeDirection", w_ParticleSystem_setRelativeDirection },
  392. { "setSpeed", w_ParticleSystem_setSpeed },
  393. { "setGravity", w_ParticleSystem_setGravity },
  394. { "setRadialAcceleration", w_ParticleSystem_setRadialAcceleration },
  395. { "setTangentialAcceleration", w_ParticleSystem_setTangentialAcceleration },
  396. { "setSizes", w_ParticleSystem_setSizes },
  397. { "setSizeVariation", w_ParticleSystem_setSizeVariation },
  398. { "setRotation", w_ParticleSystem_setRotation },
  399. { "setSpin", w_ParticleSystem_setSpin },
  400. { "setSpinVariation", w_ParticleSystem_setSpinVariation },
  401. { "setColors", w_ParticleSystem_setColors },
  402. { "setQuads", w_ParticleSystem_setQuads },
  403. { "setOffset", w_ParticleSystem_setOffset },
  404. { "getX", w_ParticleSystem_getX },
  405. { "getY", w_ParticleSystem_getY },
  406. { "getPosition", w_ParticleSystem_getPosition },
  407. { "getAreaSpread", w_ParticleSystem_getAreaSpread },
  408. { "getDirection", w_ParticleSystem_getDirection },
  409. { "getSpread", w_ParticleSystem_getSpread },
  410. { "getOffsetX", w_ParticleSystem_getOffsetX },
  411. { "getOffsetY", w_ParticleSystem_getOffsetY },
  412. { "count", w_ParticleSystem_count },
  413. { "start", w_ParticleSystem_start },
  414. { "stop", w_ParticleSystem_stop },
  415. { "pause", w_ParticleSystem_pause },
  416. { "reset", w_ParticleSystem_reset },
  417. { "isActive", w_ParticleSystem_isActive },
  418. { "isEmpty", w_ParticleSystem_isEmpty },
  419. { "isFull", w_ParticleSystem_isFull },
  420. { "update", w_ParticleSystem_update },
  421. { 0, 0 }
  422. };
  423. extern "C" int luaopen_particlesystem(lua_State *L)
  424. {
  425. return luax_register_type(L, "ParticleSystem", functions);
  426. }
  427. } // opengl
  428. } // graphics
  429. } // love