Particle.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "Particle.hpp"
  23. ParticleSystem::ParticleSystem( int width, int height )
  24. {
  25. _transparent = sf::Color( 0, 0, 0, 0 );
  26. _image.create( width, height, _transparent );
  27. _texture.loadFromImage(_image);
  28. _sprite = sf::Sprite(_texture);
  29. _position.x = 0.5f * width;
  30. _position.y = 0.5f * height;
  31. _particleSpeed = 20.0f;
  32. _dissolutionRate = 60.f;
  33. }
  34. ParticleSystem::~ParticleSystem()
  35. {
  36. for(auto it = _particles.begin(); it != _particles.end(); it++)
  37. {
  38. delete *it;
  39. }
  40. }
  41. void ParticleSystem::fuel(int particles, float angle)
  42. {
  43. Particle* particle;
  44. const double degree = 3.14159265358 / 180.;
  45. for(int i = 0; i < particles; i++)
  46. {
  47. particle = new Particle();
  48. particle->pos.x = _position.x;
  49. particle->pos.y = _position.y;
  50. do
  51. {
  52. particle->vel.x = _randomizer.rnd(0.0f, 15.0f) * -std::sin(angle * degree);
  53. particle->vel.y = _randomizer.rnd(0.0f, 15.0f) * std::cos(angle * degree);
  54. } while(particle->vel.x == 0.0f && particle->vel.y == 0.0f);
  55. particle->color.r = std::rand() % 100 + 150;
  56. particle->color.g = std::rand() % 50 + 150;
  57. particle->color.b = std::rand() % 50;
  58. particle->transparency = 255.f;
  59. particle->color.a = sf::Uint8(particle->transparency);
  60. _particles.push_back(particle);
  61. }
  62. }
  63. #include "iostream"
  64. void ParticleSystem::update(sf::Time delta)
  65. {
  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. for(auto it = _particles.begin(); it != _particles.end(); it++ )
  85. {
  86. _image.setPixel( (int)(*it)->pos.x, (int)(*it)->pos.y, (*it)->color );
  87. }
  88. _texture.update(_image);
  89. }
  90. void ParticleSystem::clear()
  91. {
  92. for(auto it = _particles.begin(); it != _particles.end(); it++ )
  93. {
  94. _image.setPixel( (int)(*it)->pos.x, (int)(*it)->pos.y, _transparent );
  95. }
  96. }