Particle.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <vector>
  23. #include <SFML/Graphics.hpp>
  24. #include <iostream>
  25. class ParticleSystem: public sf::Drawable
  26. {
  27. public:
  28. ParticleSystem(int width, int height);
  29. ~ParticleSystem();
  30. void fuel(int particles, float angle);
  31. void update(sf::Time delta);
  32. void render();
  33. void clear();
  34. void enable(bool enabled)
  35. {
  36. _enabled = enabled;
  37. }
  38. const bool isEnabled() const
  39. {
  40. return _enabled;
  41. }
  42. void loadColors(const sf::Image& image)
  43. {
  44. _colors.clear();
  45. for(unsigned i = 0; i < image.getSize().x; ++i)
  46. for(unsigned j = 0; j < image.getSize().y; ++j)
  47. {
  48. sf::Color color = image.getPixel(i, j);
  49. if(color.r > 0 || color.g > 0 || color.b > 0)
  50. _colors.emplace_back(color.r, color.g, color.b, 255);
  51. }
  52. }
  53. void setPosition(const sf::Vector2f position)
  54. {
  55. _sprite.setPosition(position - _origin);
  56. }
  57. void setPosition(float x, float y)
  58. {
  59. setPosition(sf::Vector2f(x, y));
  60. }
  61. void setOrigin(const sf::Vector2f origin)
  62. {
  63. _origin = origin;
  64. }
  65. void setOrigin(float x, float y)
  66. {
  67. setOrigin(sf::Vector2f(x, y));
  68. }
  69. void setParticleSpeed(float particleSpeed)
  70. {
  71. _particleSpeed = particleSpeed;
  72. }
  73. void setDissolutionRate(float dissolutionRate)
  74. {
  75. _dissolutionRate = dissolutionRate;
  76. }
  77. const sf::Sprite& getSprite() const
  78. {
  79. return _sprite;
  80. }
  81. private:
  82. struct Particle
  83. {
  84. sf::Vector2f pos;
  85. sf::Vector2f vel;
  86. sf::Color color;
  87. float transparency;
  88. };
  89. virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
  90. sf::Vector2f _origin;
  91. sf::Clock _clock;
  92. sf::Image _image;
  93. sf::Texture _texture;
  94. sf::Sprite _sprite;
  95. float _particleSpeed;
  96. float _dissolutionRate;
  97. std::vector<Particle*> _particles;
  98. std::vector<sf::Color> _colors;
  99. bool _enabled;
  100. };