Particle.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Triangles
  3. * Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * This is based on a wiki entry
  19. * https://github.com/SFML/SFML/wiki/Source:-Particle-System
  20. */
  21. #pragma once
  22. #include <SFML/System/Vector2.hpp>
  23. #include <SFML/System/Clock.hpp>
  24. #include <SFML/Graphics/Color.hpp>
  25. #include <SFML/Graphics/Image.hpp>
  26. #include <SFML/Graphics/Sprite.hpp>
  27. #include <SFML/Graphics/Texture.hpp>
  28. #include <vector>
  29. #include <random>
  30. class Randomizer
  31. {
  32. public:
  33. Randomizer() : device_(), engine_(device_()){};
  34. int rnd(int a, int b) {
  35. std::uniform_int_distribution<int> uni_dist(a, b);
  36. return uni_dist(engine_);
  37. };
  38. double rnd(double a, double b) {
  39. std::uniform_real_distribution<double> uni_dist(a, b);
  40. return uni_dist(engine_);
  41. };
  42. private:
  43. std::random_device device_;
  44. std::default_random_engine engine_;
  45. };
  46. struct Particle
  47. {
  48. sf::Vector2f pos;
  49. sf::Vector2f vel;
  50. sf::Color color;
  51. float transparency;
  52. };
  53. class ParticleSystem
  54. {
  55. public:
  56. ParticleSystem( int width, int height );
  57. ~ParticleSystem();
  58. void fuel(int particles, float angle);
  59. void update(sf::Time delta);
  60. void render();
  61. void clear();
  62. void setPosition(const sf::Vector2f position)
  63. {
  64. _position = position;
  65. }
  66. void setPosition(float x, float y)
  67. {
  68. setPosition(sf::Vector2f(x, y));
  69. }
  70. void setParticleSpeed(float particleSpeed)
  71. {
  72. _particleSpeed = particleSpeed;
  73. }
  74. void setDissolutionRate(float dissolutionRate)
  75. {
  76. _dissolutionRate = dissolutionRate;
  77. }
  78. const sf::Sprite& getSprite() const
  79. {
  80. return _sprite;
  81. }
  82. private:
  83. sf::Vector2f _position; // Particle origin (pixel co-ordinates)
  84. sf::Clock _clock; // Used to scale particle motion
  85. sf::Color _transparent; // sf::Color( 0, 0, 0, 0 )
  86. sf::Image _image; // See render() and remove()
  87. sf::Texture _texture;
  88. Randomizer _randomizer;
  89. sf::Sprite _sprite; // Connected to m_image
  90. float _particleSpeed;// Pixels per second (at most)
  91. float _dissolutionRate;
  92. std::vector<Particle*> _particles;
  93. };