ParticleSystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**
  2. * Copyright (c) 2006-2012 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 "ParticleSystem.h"
  21. #include "common/math.h"
  22. #include "GLee.h"
  23. #include <cmath>
  24. #include <cstdlib>
  25. namespace love
  26. {
  27. namespace graphics
  28. {
  29. namespace opengl
  30. {
  31. namespace
  32. {
  33. Colorf colorToFloat(const Color &c)
  34. {
  35. return Colorf((GLfloat)c.r/255.0f, (GLfloat)c.g/255.0f, (GLfloat)c.b/255.0f, (GLfloat)c.a/255.0f);
  36. }
  37. }
  38. float calculate_variation(float inner, float outer, float var)
  39. {
  40. float low = inner - (outer/2.0f)*var;
  41. float high = inner + (outer/2.0f)*var;
  42. float r = random();
  43. return low*(1-r)+high*r;
  44. }
  45. StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM>::Entry ParticleSystem::distributionsEntries[] = {
  46. { "none", ParticleSystem::DISTRIBUTION_NONE },
  47. { "uniform", ParticleSystem::DISTRIBUTION_UNIFORM },
  48. { "normal", ParticleSystem::DISTRIBUTION_NORMAL },
  49. };
  50. StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM> ParticleSystem::distributions(ParticleSystem::distributionsEntries, sizeof(ParticleSystem::distributionsEntries));
  51. ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer)
  52. : pStart(0)
  53. , pLast(0)
  54. , pEnd(0)
  55. , active(true)
  56. , emissionRate(0)
  57. , emitCounter(0)
  58. , areaSpreadDistribution(DISTRIBUTION_NONE)
  59. , lifetime(-1)
  60. , life(0)
  61. , particleLifeMin(0)
  62. , particleLifeMax(0)
  63. , direction(0)
  64. , spread(0)
  65. , relative(false)
  66. , speedMin(0)
  67. , speedMax(0)
  68. , gravityMin(0)
  69. , gravityMax(0)
  70. , radialAccelerationMin(0)
  71. , radialAccelerationMax(0)
  72. , tangentialAccelerationMin(0)
  73. , tangentialAccelerationMax(0)
  74. , sizeVariation(0)
  75. , rotationMin(0)
  76. , rotationMax(0)
  77. , spinStart(0)
  78. , spinEnd(0)
  79. , spinVariation(0)
  80. , offsetX(sprite->getWidth()*0.5f)
  81. , offsetY(sprite->getHeight()*0.5f)
  82. {
  83. this->sprite = sprite;
  84. sprite->retain();
  85. sizes.push_back(1.0f);
  86. colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
  87. setBufferSize(buffer);
  88. }
  89. ParticleSystem::~ParticleSystem()
  90. {
  91. if (this->sprite != 0)
  92. this->sprite->release();
  93. if (pStart != 0)
  94. delete [] pStart;
  95. }
  96. void ParticleSystem::add()
  97. {
  98. if (isFull()) return;
  99. float min,max;
  100. min = particleLifeMin;
  101. max = particleLifeMax;
  102. if (min == max)
  103. pLast->life = min;
  104. else
  105. pLast->life = random(min, max);
  106. pLast->lifetime = pLast->life;
  107. pLast->position[0] = position.getX();
  108. pLast->position[1] = position.getY();
  109. switch (areaSpreadDistribution)
  110. {
  111. case DISTRIBUTION_UNIFORM:
  112. pLast->position[0] += random(-areaSpread.getX(), areaSpread.getX());
  113. pLast->position[1] += random(-areaSpread.getY(), areaSpread.getY());
  114. break;
  115. case DISTRIBUTION_NORMAL:
  116. pLast->position[0] += random_normal(areaSpread.getX());
  117. pLast->position[1] += random_normal(areaSpread.getY());
  118. break;
  119. case DISTRIBUTION_NONE:
  120. default:
  121. break;
  122. }
  123. min = direction - spread/2.0f;
  124. max = direction + spread/2.0f;
  125. pLast->direction = random(min, max);
  126. pLast->origin = position;
  127. min = speedMin;
  128. max = speedMax;
  129. float speed = random(min, max);
  130. pLast->speed = love::Vector(cos(pLast->direction), sin(pLast->direction));
  131. pLast->speed *= speed;
  132. min = gravityMin;
  133. max = gravityMax;
  134. pLast->gravity = random(min, max);
  135. min = radialAccelerationMin;
  136. max = radialAccelerationMax;
  137. pLast->radialAcceleration = random(min, max);
  138. min = tangentialAccelerationMin;
  139. max = tangentialAccelerationMax;
  140. pLast->tangentialAcceleration = random(min, max);
  141. pLast->sizeOffset = random(sizeVariation); // time offset for size change
  142. pLast->sizeIntervalSize = (1.0f - random(sizeVariation)) - pLast->sizeOffset;
  143. pLast->size = sizes[(size_t)(pLast->sizeOffset - .5f) * (sizes.size() - 1)];
  144. min = rotationMin;
  145. max = rotationMax;
  146. pLast->spinStart = calculate_variation(spinStart, spinEnd, spinVariation);
  147. pLast->spinEnd = calculate_variation(spinEnd, spinStart, spinVariation);
  148. pLast->rotation = random(min, max);
  149. pLast->color = colors[0];
  150. pLast++;
  151. }
  152. void ParticleSystem::remove(particle *p)
  153. {
  154. if (!isEmpty())
  155. {
  156. *p = *(--pLast);
  157. }
  158. }
  159. void ParticleSystem::setSprite(Image *image)
  160. {
  161. if (sprite != 0)
  162. sprite->release();
  163. sprite = image;
  164. sprite->retain();
  165. }
  166. void ParticleSystem::setBufferSize(unsigned int size)
  167. {
  168. // delete previous data
  169. delete [] pStart;
  170. pLast = pStart = new particle[size];
  171. pEnd = pStart + size;
  172. }
  173. void ParticleSystem::setEmissionRate(int rate)
  174. {
  175. emissionRate = rate;
  176. }
  177. void ParticleSystem::setLifetime(float life)
  178. {
  179. this->life = lifetime = life;
  180. }
  181. void ParticleSystem::setParticleLife(float min, float max)
  182. {
  183. particleLifeMin = min;
  184. if (max == 0)
  185. particleLifeMax = min;
  186. else
  187. particleLifeMax = max;
  188. }
  189. void ParticleSystem::setPosition(float x, float y)
  190. {
  191. position = love::Vector(x, y);
  192. }
  193. void ParticleSystem::setAreaSpread(AreaSpreadDistribution distribution, float x, float y)
  194. {
  195. areaSpread = love::Vector(x, y);
  196. areaSpreadDistribution = distribution;
  197. }
  198. void ParticleSystem::setDirection(float direction)
  199. {
  200. this->direction = direction;
  201. }
  202. void ParticleSystem::setSpread(float spread)
  203. {
  204. this->spread = spread;
  205. }
  206. void ParticleSystem::setRelativeDirection(bool relative)
  207. {
  208. this->relative = relative;
  209. }
  210. void ParticleSystem::setSpeed(float speed)
  211. {
  212. speedMin = speedMax = speed;
  213. }
  214. void ParticleSystem::setSpeed(float min, float max)
  215. {
  216. speedMin = min;
  217. speedMax = max;
  218. }
  219. void ParticleSystem::setGravity(float gravity)
  220. {
  221. gravityMin = gravityMax = gravity;
  222. }
  223. void ParticleSystem::setGravity(float min, float max)
  224. {
  225. gravityMin = min;
  226. gravityMax = max;
  227. }
  228. void ParticleSystem::setRadialAcceleration(float acceleration)
  229. {
  230. radialAccelerationMin = radialAccelerationMax = acceleration;
  231. }
  232. void ParticleSystem::setRadialAcceleration(float min, float max)
  233. {
  234. radialAccelerationMin = min;
  235. radialAccelerationMax = max;
  236. }
  237. void ParticleSystem::setTangentialAcceleration(float acceleration)
  238. {
  239. tangentialAccelerationMin = tangentialAccelerationMax = acceleration;
  240. }
  241. void ParticleSystem::setTangentialAcceleration(float min, float max)
  242. {
  243. tangentialAccelerationMin = min;
  244. tangentialAccelerationMax = max;
  245. }
  246. void ParticleSystem::setSize(float size)
  247. {
  248. sizes.resize(1);
  249. sizes[0] = size;
  250. }
  251. void ParticleSystem::setSize(const std::vector<float> &newSizes, float variation)
  252. {
  253. sizes = newSizes;
  254. sizeVariation = variation;
  255. }
  256. void ParticleSystem::setSizeVariation(float variation)
  257. {
  258. sizeVariation = variation;
  259. }
  260. void ParticleSystem::setRotation(float rotation)
  261. {
  262. rotationMin = rotationMax = rotation;
  263. }
  264. void ParticleSystem::setRotation(float min, float max)
  265. {
  266. rotationMin = min;
  267. rotationMax = max;
  268. }
  269. void ParticleSystem::setSpin(float spin)
  270. {
  271. spinStart = spin;
  272. spinEnd = spin;
  273. }
  274. void ParticleSystem::setSpin(float start, float end)
  275. {
  276. spinStart = start;
  277. spinEnd = end;
  278. }
  279. void ParticleSystem::setSpin(float start, float end, float variation)
  280. {
  281. spinStart = start;
  282. spinEnd = end;
  283. spinVariation = variation;
  284. }
  285. void ParticleSystem::setSpinVariation(float variation)
  286. {
  287. spinVariation = variation;
  288. }
  289. void ParticleSystem::setColor(const Color &color)
  290. {
  291. colors.resize(1);
  292. colors[0] = colorToFloat(color);
  293. }
  294. void ParticleSystem::setColor(const std::vector<Color> &newColors)
  295. {
  296. colors.resize(newColors.size());
  297. for (size_t i = 0; i < newColors.size(); ++i)
  298. colors[i] = colorToFloat(newColors[i]);
  299. }
  300. void ParticleSystem::setOffset(float x, float y)
  301. {
  302. offsetX = x;
  303. offsetY = y;
  304. }
  305. float ParticleSystem::getX() const
  306. {
  307. return position.getX();
  308. }
  309. float ParticleSystem::getY() const
  310. {
  311. return position.getY();
  312. }
  313. const love::Vector &ParticleSystem::getPosition() const
  314. {
  315. return position;
  316. }
  317. ParticleSystem::AreaSpreadDistribution ParticleSystem::getAreaSpreadDistribution() const
  318. {
  319. return areaSpreadDistribution;
  320. }
  321. const love::Vector &ParticleSystem::getAreaSpreadParameters() const
  322. {
  323. return areaSpread;
  324. }
  325. float ParticleSystem::getDirection() const
  326. {
  327. return direction;
  328. }
  329. float ParticleSystem::getSpread() const
  330. {
  331. return spread;
  332. }
  333. float ParticleSystem::getOffsetX() const
  334. {
  335. return offsetX;
  336. }
  337. float ParticleSystem::getOffsetY() const
  338. {
  339. return offsetY;
  340. }
  341. int ParticleSystem::count() const
  342. {
  343. return (int)(pLast - pStart);
  344. }
  345. void ParticleSystem::start()
  346. {
  347. active = true;
  348. }
  349. void ParticleSystem::stop()
  350. {
  351. active = false;
  352. life = lifetime;
  353. emitCounter = 0;
  354. }
  355. void ParticleSystem::pause()
  356. {
  357. active = false;
  358. }
  359. void ParticleSystem::reset()
  360. {
  361. pLast = pStart;
  362. life = lifetime;
  363. emitCounter = 0;
  364. }
  365. bool ParticleSystem::isActive() const
  366. {
  367. return active;
  368. }
  369. bool ParticleSystem::isEmpty() const
  370. {
  371. return pStart == pLast;
  372. }
  373. bool ParticleSystem::isFull() const
  374. {
  375. return pLast == pEnd;
  376. }
  377. void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
  378. {
  379. if (sprite == 0) return; // just in case of failure
  380. glPushMatrix();
  381. glPushAttrib(GL_CURRENT_BIT);
  382. Matrix t;
  383. t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  384. glMultMatrixf((const GLfloat *)t.getElements());
  385. particle *p = pStart;
  386. while (p != pLast)
  387. {
  388. glPushMatrix();
  389. glColor4f(p->color.r, p->color.g, p->color.b, p->color.a);
  390. sprite->draw(p->position[0], p->position[1], p->rotation, p->size, p->size, offsetX, offsetY, 0.0f, 0.0f);
  391. glPopMatrix();
  392. p++;
  393. }
  394. glPopAttrib();
  395. glPopMatrix();
  396. }
  397. void ParticleSystem::update(float dt)
  398. {
  399. // Traverse all particles and update.
  400. particle *p = pStart;
  401. // Make some more particles.
  402. if (active)
  403. {
  404. float rate = 1.0f / emissionRate; // the amount of time between each particle emit
  405. emitCounter += dt;
  406. while (emitCounter > rate)
  407. {
  408. add();
  409. emitCounter -= rate;
  410. }
  411. /*int particles = (int)(emissionRate * dt);
  412. for (int i = 0; i != particles; i++)
  413. add();*/
  414. life -= dt;
  415. if (lifetime != -1 && life < 0)
  416. stop();
  417. }
  418. while (p != pLast)
  419. {
  420. // Decrease lifespan.
  421. p->life -= dt;
  422. if (p->life > 0)
  423. {
  424. // Temp variables.
  425. love::Vector radial, tangential, gravity(0, p->gravity);
  426. love::Vector ppos(p->position[0], p->position[1]);
  427. // Get vector from particle center to particle.
  428. radial = ppos - p->origin;
  429. radial.normalize();
  430. tangential = radial;
  431. // Resize radial acceleration.
  432. radial *= p->radialAcceleration;
  433. // Calculate tangential acceleration.
  434. {
  435. float a = tangential.getX();
  436. tangential.setX(-tangential.getY());
  437. tangential.setY(a);
  438. }
  439. // Resize tangential.
  440. tangential *= p->tangentialAcceleration;
  441. // Update position.
  442. p->speed += (radial+tangential+gravity)*dt;
  443. // Modify position.
  444. ppos += p->speed * dt;
  445. p->position[0] = ppos.getX();
  446. p->position[1] = ppos.getY();
  447. const float t = 1.0f - p->life / p->lifetime;
  448. // Rotate.
  449. p->rotation += (p->spinStart * (1.0f - t) + p->spinEnd * t)*dt;
  450. // Change size according to given intervals:
  451. // i = 0 1 2 3 n-1
  452. // |-------|-------|------|--- ... ---|
  453. // t = 0 1/(n-1) 3/(n-1) 1
  454. //
  455. // `s' is the interpolation variable scaled to the current
  456. // interval width, e.g. if n = 5 and t = 0.3, then the current
  457. // indices are 1,2 and s = 0.3 - 0.25 = 0.05
  458. float s = p->sizeOffset + t * p->sizeIntervalSize; // size variation
  459. s *= (float)(sizes.size() - 1); // 0 <= s < sizes.size()
  460. size_t i = (size_t)s;
  461. size_t k = (i == sizes.size() - 1) ? i : i + 1; // boundary check (prevents failing on t = 1.0f)
  462. s -= (float)i; // transpose s to be in interval [0:1]: i <= s < i + 1 ~> 0 <= s < 1
  463. p->size = sizes[i] * (1.0f - s) + sizes[k] * s;
  464. // Update color according to given intervals (as above)
  465. s = t * (float)(colors.size() - 1);
  466. i = (size_t)s;
  467. k = (i == colors.size() - 1) ? i : i + 1;
  468. s -= (float)i; // 0 <= s <= 1
  469. p->color = colors[i] * (1.0f - s) + colors[k] * s;
  470. // Next particle.
  471. p++;
  472. }
  473. else
  474. {
  475. remove(p);
  476. if (p >= pLast)
  477. return;
  478. } // else
  479. } // while
  480. }
  481. bool ParticleSystem::getConstant(const char *in, AreaSpreadDistribution &out)
  482. {
  483. return distributions.find(in, out);
  484. }
  485. bool ParticleSystem::getConstant(AreaSpreadDistribution in, const char *&out)
  486. {
  487. return distributions.find(in, out);
  488. }
  489. } // opengl
  490. } // graphics
  491. } // love