ParticleSystem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #ifndef LOVE_GRAPHICS_OPENGL_PARTICLE_SYSTEM_H
  21. #define LOVE_GRAPHICS_OPENGL_PARTICLE_SYSTEM_H
  22. // LOVE
  23. #include "common/math.h"
  24. #include "common/Vector.h"
  25. #include "graphics/Drawable.h"
  26. #include "graphics/Color.h"
  27. #include "Image.h"
  28. #include <vector>
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. namespace opengl
  34. {
  35. // Represents a single particle.
  36. struct particle
  37. {
  38. float lifetime;
  39. float life;
  40. float position[2];
  41. float direction;
  42. // Particles gravitate towards this point.
  43. love::Vector origin;
  44. love::Vector speed;
  45. float gravity;
  46. float radialAcceleration;
  47. float tangentialAcceleration;
  48. float size;
  49. float sizeOffset;
  50. float sizeIntervalSize;
  51. float rotation;
  52. float spinStart;
  53. float spinEnd;
  54. Colorf color;
  55. };
  56. /**
  57. * A class for creating, moving and drawing particles.
  58. * A big thanks to bobthebloke.org
  59. **/
  60. class ParticleSystem : public Drawable
  61. {
  62. public:
  63. /**
  64. * Type of distribution new particles are drawn from: None, uniform, normal.
  65. */
  66. enum AreaSpreadDistribution
  67. {
  68. DISTRIBUTION_NONE,
  69. DISTRIBUTION_UNIFORM,
  70. DISTRIBUTION_NORMAL,
  71. DISTRIBUTION_MAX_ENUM,
  72. };
  73. /**
  74. * Creates a particle system with the specified buffersize and sprite.
  75. **/
  76. ParticleSystem(Image *sprite, unsigned int buffer);
  77. /**
  78. * Deletes any allocated memory.
  79. **/
  80. virtual ~ParticleSystem();
  81. /**
  82. * Sets the sprite used in the particle system.
  83. * @param sprite The new sprite.
  84. **/
  85. void setSprite(Image *image);
  86. /**
  87. * Clears the current buffer and allocates the appropriate amount of space for the buffer.
  88. * @param size The new buffer size.
  89. **/
  90. void setBufferSize(unsigned int size);
  91. /**
  92. * Sets the emission rate.
  93. * @param rate The amount of particles per second.
  94. **/
  95. void setEmissionRate(int rate);
  96. /**
  97. * Sets the lifetime of the particle emitter (-1 means eternal)
  98. * @param life The lifetime (in seconds).
  99. **/
  100. void setLifetime(float life);
  101. /**
  102. * Sets the life range of the particles.
  103. * @param lifeMin The minimum life.
  104. * @param lifeMax The maximum life (if 0, then becomes the same as minimum life).
  105. **/
  106. void setParticleLife(float min, float max = 0);
  107. /**
  108. * Sets the position of the center of the emitter and the direction (if set to relative).
  109. * Used to move the emitter without changing the position of already existing particles.
  110. * @param x The x-coordinate.
  111. * @param y The y-coordinate.
  112. **/
  113. void setPosition(float x, float y);
  114. /**
  115. * Sets the emission area spread parameters and distribution type. The interpretation of
  116. * the parameters depends on the distribution type:
  117. *
  118. * * None: Parameters are ignored. No area spread.
  119. * * Uniform: Parameters denote maximal (symmetric) displacement from emitter position.
  120. * * Normal: Parameters denote the standard deviation in x and y direction. x and y are assumed to be uncorrelated.
  121. * @param x First parameter. Interpretation depends on distribution type.
  122. * @param y Second parameter. Interpretation depends on distribution type.
  123. * @param distribution Distribution type
  124. * */
  125. void setAreaSpread(AreaSpreadDistribution distribution, float x, float y);
  126. /**
  127. * Sets the direction and the spread of the particle emitter.
  128. * @param direction The direction (in degrees).
  129. **/
  130. void setDirection(float direction);
  131. /**
  132. * Sets the spread of the particle emitter.
  133. * @param spread The spread (in degrees).
  134. **/
  135. void setSpread(float spread);
  136. /**
  137. * Sets whether the direction should be relative to the particle emitters movement. Used in conjunction with setPosition.
  138. * @param relative Whether to have relative direction.
  139. **/
  140. void setRelativeDirection(bool relative);
  141. /**
  142. * Sets the speed of the particles.
  143. * @param speed The speed.
  144. **/
  145. void setSpeed(float speed);
  146. /**
  147. * Sets the speed of the particles.
  148. * @param min The minimum speed.
  149. * @param max The maximum speed.
  150. **/
  151. void setSpeed(float min, float max);
  152. /**
  153. * Sets the gravity of the particles (the acceleration along the y-axis).
  154. * @param gravity The amount of gravity.
  155. **/
  156. void setGravity(float gravity);
  157. /**
  158. * Sets the gravity of the particles (the acceleration along the y-axis).
  159. * @param min The minimum gravity.
  160. * @param max The maximum gravity.
  161. **/
  162. void setGravity(float min, float max);
  163. /**
  164. * Sets the radial acceleration (the acceleration towards the particle emitter).
  165. * @param acceleration The amount of acceleration.
  166. **/
  167. void setRadialAcceleration(float acceleration);
  168. /**
  169. * Sets the radial acceleration (the acceleration towards the particle emitter).
  170. * @param min The minimum acceleration.
  171. * @param max The maximum acceleration.
  172. **/
  173. void setRadialAcceleration(float min, float max);
  174. /**
  175. * Sets the tangential acceleration (the acceleration perpendicular to the particle's direction).
  176. * @param acceleration The amount of acceleration.
  177. **/
  178. void setTangentialAcceleration(float acceleration);
  179. /**
  180. * Sets the tangential acceleration (the acceleration perpendicular to the particle's direction).
  181. * @param min The minimum acceleration.
  182. * @param max The maximum acceleration.
  183. **/
  184. void setTangentialAcceleration(float min, float max);
  185. /**
  186. * Sets the size of the sprite (1.0 being the default size).
  187. * @param size The size of the sprite.
  188. **/
  189. void setSize(float size);
  190. /**
  191. * Sets the size of the sprite upon creation and upon death (1.0 being the default size) and any variation.
  192. * @param newSizes Array of sizes
  193. * @param variation The amount of variation on the starting size (0 being no variation and 1.0 a random size between start and end).
  194. **/
  195. void setSize(const std::vector<float> &newSizes, float variation = 0.0f);
  196. /**
  197. * Sets the amount of variation to the sprite's beginning size (0 being no variation and 1.0 a random size between start and end).
  198. * @param variation The amount of variation.
  199. **/
  200. void setSizeVariation(float variation);
  201. /**
  202. * Sets the amount of rotation a sprite starts out with.
  203. * @param rotation The amount of rotation.
  204. **/
  205. void setRotation(float rotation);
  206. /**
  207. * Sets the amount of rotation a sprite starts out with (a random value between min and max).
  208. * @param min The minimum amount of rotation.
  209. * @param max The maximum amount of rotation.
  210. **/
  211. void setRotation(float min, float max);
  212. /**
  213. * Sets the spin of the sprite.
  214. * @param spin The spin of the sprite (in degrees).
  215. **/
  216. void setSpin(float spin);
  217. /**
  218. * Sets the spin of the sprite upon particle creation and death.
  219. * @param start The spin of the sprite upon creation (in degrees).
  220. * @param end The spin of the sprite upon death (in degrees).
  221. **/
  222. void setSpin(float start, float end);
  223. /**
  224. * Sets the spin of the sprite upon particle creation and death and the variation.
  225. * @param start The spin of the sprite upon creation (in degrees).
  226. * @param end The spin of the sprite upon death (in degrees).
  227. * @param variation The variation of the start spin (0 being no variation and 1 beign a random spin between start and end).
  228. **/
  229. void setSpin(float start, float end, float variation);
  230. /**
  231. * Sets the variation of the start spin (0 being no variation and 1 beign a random spin between start and end).
  232. * @param variation The variation in degrees.
  233. **/
  234. void setSpinVariation(float variation);
  235. /**
  236. * Sets the color of the particles.
  237. * @param color The color.
  238. **/
  239. void setColor(const Color &color);
  240. /**
  241. * Sets the particles' offsets for rotation.
  242. * @param x The x offset.
  243. * @param y The y offset.
  244. **/
  245. void setOffset(float x, float y);
  246. /**
  247. * Sets the color of the particles.
  248. * @param newColors Array of colors
  249. **/
  250. void setColor(const std::vector<Color> &newColors);
  251. /**
  252. * Returns the x-coordinate of the emitter's position.
  253. **/
  254. float getX() const;
  255. /**
  256. * Returns the y-coordinate of the emitter's position.
  257. **/
  258. float getY() const;
  259. /**
  260. * Returns the position of the emitter.
  261. **/
  262. const love::Vector &getPosition() const;
  263. /**
  264. * Returns area spread distribution type.
  265. */
  266. AreaSpreadDistribution getAreaSpreadDistribution() const;
  267. /**
  268. * Returns area spread parameters.
  269. */
  270. const love::Vector &getAreaSpreadParameters() const;
  271. /**
  272. * Returns the direction of the emitter (in degrees).
  273. **/
  274. float getDirection() const;
  275. /**
  276. * Returns the directional spread of the emitter (in degrees).
  277. **/
  278. float getSpread() const;
  279. /**
  280. * Returns the X offset of the particles.
  281. **/
  282. float getOffsetX() const;
  283. /**
  284. * Returns the Y offset of the particles.
  285. **/
  286. float getOffsetY() const;
  287. /**
  288. * Returns the amount of particles that are currently active in the system.
  289. **/
  290. int count() const;
  291. /**
  292. * Starts/resumes the particle emitter.
  293. **/
  294. void start();
  295. /**
  296. * Stops the particle emitter and resets.
  297. **/
  298. void stop();
  299. /**
  300. * Pauses the particle emitter.
  301. **/
  302. void pause();
  303. /**
  304. * Resets the particle emitter.
  305. **/
  306. void reset();
  307. /**
  308. * Returns whether the particle emitter is active.
  309. **/
  310. bool isActive() const;
  311. /**
  312. * Returns whether the particle system is empty of particles or not.
  313. **/
  314. bool isEmpty() const;
  315. /**
  316. * Returns whether the amount of particles has reached the buffer limit or not.
  317. **/
  318. bool isFull() const;
  319. /**
  320. * Draws the particle emitter at the specified position.
  321. * @param x The x-coordinate.
  322. * @param y The y-coordinate.
  323. **/
  324. virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
  325. /**
  326. * Updates the particle system.
  327. * @param dt Time since last update.
  328. **/
  329. void update(float dt);
  330. static bool getConstant(const char *in, AreaSpreadDistribution &out);
  331. static bool getConstant(AreaSpreadDistribution in, const char *&out);
  332. protected:
  333. // The max amount of particles.
  334. unsigned int bufferSize;
  335. // Pointer to the first particle.
  336. particle *pStart;
  337. // Pointer to the next available free space.
  338. particle *pLast;
  339. // Pointer to the end of the memory allocation.
  340. particle *pEnd;
  341. // array of transformed vertex data for all particles, for drawing
  342. vertex * particleVerts;
  343. // The sprite to be drawn.
  344. Image *sprite;
  345. // Whether the particle emitter is active.
  346. bool active;
  347. // The emission rate (particles/sec).
  348. int emissionRate;
  349. // Used to determine when a particle should be emitted.
  350. float emitCounter;
  351. // The relative position of the particle emitter.
  352. love::Vector position;
  353. // Emission area spread.
  354. AreaSpreadDistribution areaSpreadDistribution;
  355. love::Vector areaSpread;
  356. // The lifetime of the particle emitter (-1 means infinite) and the life it has left.
  357. float lifetime;
  358. float life;
  359. // The particle life.
  360. float particleLifeMin;
  361. float particleLifeMax;
  362. // The direction (and spread) the particles will be emitted in. Measured in radians.
  363. float direction;
  364. float spread;
  365. // Whether the direction should be relative to the emitter's movement.
  366. bool relative;
  367. // The speed.
  368. float speedMin;
  369. float speedMax;
  370. // Acceleration towards the bottom of the screen
  371. float gravityMin;
  372. float gravityMax;
  373. // Acceleration towards the emitter's center
  374. float radialAccelerationMin;
  375. float radialAccelerationMax;
  376. // Acceleration perpendicular to the particle's direction.
  377. float tangentialAccelerationMin;
  378. float tangentialAccelerationMax;
  379. // Size.
  380. std::vector<float> sizes;
  381. float sizeVariation;
  382. // Rotation
  383. float rotationMin;
  384. float rotationMax;
  385. // Spin.
  386. float spinStart;
  387. float spinEnd;
  388. float spinVariation;
  389. // Offsets
  390. float offsetX;
  391. float offsetY;
  392. // Color.
  393. std::vector<Colorf> colors;
  394. void add();
  395. void remove(particle *p);
  396. static StringMap<AreaSpreadDistribution, DISTRIBUTION_MAX_ENUM>::Entry distributionsEntries[];
  397. static StringMap<AreaSpreadDistribution, DISTRIBUTION_MAX_ENUM> distributions;
  398. };
  399. } // opengl
  400. } // graphics
  401. } // love
  402. #endif // LOVE_GRAPHICS_OPENGL_PARTICLE_SYSTEM_H