Particle.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <sstream>
  22. #include <cmath>
  23. #include "Particle.hpp"
  24. ParticleSystem::ParticleSystem(int width, int height):
  25. _origin(0, 0),
  26. _particleSpeed(20.f),
  27. _dissolutionRate(60.f),
  28. _enabled(true)
  29. {
  30. _colors.push_back(sf::Color::White);
  31. _image.create( width, height, sf::Color::Transparent);
  32. _texture.loadFromImage(_image);
  33. _sprite = sf::Sprite(_texture);
  34. }
  35. ParticleSystem::~ParticleSystem()
  36. {
  37. for(auto it = _particles.begin(); it != _particles.end(); it++)
  38. delete *it;
  39. }
  40. void ParticleSystem::fuel(int particles, float angle)
  41. {
  42. if(!_enabled)
  43. return;
  44. if(particles < 0)
  45. particles = -particles;
  46. if(_particles.size() > 40000)
  47. return;
  48. Particle* particle;
  49. for(int i = 0; i < particles; i++)
  50. {
  51. particle = new Particle();
  52. particle->pos.x = _origin.x;
  53. particle->pos.y = _origin.y;
  54. particle->vel.x = float((rand() % 30000 - 15000) / 1000.f);
  55. particle->vel.y = float((rand() % 30000 - 15000) / 1000.f);
  56. particle->color = _colors[rand() % _colors.size()];
  57. particle->transparency = 255.f;
  58. particle->color.a = sf::Uint8(particle->transparency);
  59. _particles.push_back(particle);
  60. }
  61. }
  62. void ParticleSystem::update(sf::Time delta)
  63. {
  64. if(!_enabled)
  65. return;
  66. float time = delta.asSeconds();
  67. for(auto it = _particles.begin(); it != _particles.end(); it++ )
  68. {
  69. (*it)->pos.x += (*it)->vel.x * time * _particleSpeed;
  70. (*it)->pos.y += (*it)->vel.y * time * _particleSpeed;
  71. (*it)->transparency -= (_dissolutionRate * time);
  72. (*it)->color.a = sf::Uint8((*it)->transparency);
  73. if((*it)->pos.x > _image.getSize().x || (*it)->pos.x < 0 || (*it)->pos.y > _image.getSize().y || (*it)->pos.y < 0 || (*it)->color.a < 10)
  74. {
  75. delete (*it);
  76. it = _particles.erase(it);
  77. if(it == _particles.end())
  78. return;
  79. }
  80. }
  81. }
  82. void ParticleSystem::render()
  83. {
  84. if(!_enabled)
  85. return;
  86. for(auto it = _particles.begin(); it != _particles.end(); it++)
  87. _image.setPixel((int)(*it)->pos.x, (int)(*it)->pos.y, (*it)->color);
  88. _texture.update(_image);
  89. }
  90. void ParticleSystem::clear()
  91. {
  92. if(!_enabled)
  93. return;
  94. for(auto it = _particles.begin(); it != _particles.end(); it++ )
  95. _image.setPixel( (int)(*it)->pos.x, (int)(*it)->pos.y, sf::Color::Transparent);
  96. }
  97. void ParticleSystem::draw(sf::RenderTarget& target, sf::RenderStates states) const
  98. {
  99. target.draw(_sprite);
  100. }