ParticleSystem.h 11 KB

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