/**
* Triangles
* Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* This is based on a wiki entry
* https://github.com/SFML/SFML/wiki/Source:-Particle-System
*/
#include
#include "Particle.hpp"
ParticleSystem::ParticleSystem( int width, int height )
{
_transparent = sf::Color( 0, 0, 0, 0 );
_image.create( width, height, _transparent );
_texture.loadFromImage(_image);
_sprite = sf::Sprite(_texture);
_position.x = 0.5f * width;
_position.y = 0.5f * height;
_particleSpeed = 20.0f;
_dissolutionRate = 60.f;
}
ParticleSystem::~ParticleSystem()
{
for(auto it = _particles.begin(); it != _particles.end(); it++)
{
delete *it;
}
}
void ParticleSystem::fuel(int particles, float angle)
{
Particle* particle;
const double degree = 3.14159265358 / 180.;
for(int i = 0; i < particles; i++)
{
particle = new Particle();
particle->pos.x = _position.x;
particle->pos.y = _position.y;
do
{
particle->vel.x = _randomizer.rnd(0.0f, 15.0f) * -std::sin(angle * degree);
particle->vel.y = _randomizer.rnd(0.0f, 15.0f) * std::cos(angle * degree);
} while(particle->vel.x == 0.0f && particle->vel.y == 0.0f);
particle->color.r = std::rand() % 100 + 150;
particle->color.g = std::rand() % 50 + 150;
particle->color.b = std::rand() % 50;
particle->transparency = 255.f;
particle->color.a = sf::Uint8(particle->transparency);
_particles.push_back(particle);
}
}
#include "iostream"
void ParticleSystem::update(sf::Time delta)
{
float time = delta.asSeconds();
for(auto it = _particles.begin(); it != _particles.end(); it++ )
{
(*it)->pos.x += (*it)->vel.x * time * _particleSpeed;
(*it)->pos.y += (*it)->vel.y * time * _particleSpeed;
(*it)->transparency -= (_dissolutionRate * time);
(*it)->color.a = sf::Uint8((*it)->transparency);
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)
{
delete (*it);
it = _particles.erase(it);
if(it == _particles.end())
return;
}
}
}
void ParticleSystem::render()
{
for(auto it = _particles.begin(); it != _particles.end(); it++ )
{
_image.setPixel( (int)(*it)->pos.x, (int)(*it)->pos.y, (*it)->color );
}
_texture.update(_image);
}
void ParticleSystem::clear()
{
for(auto it = _particles.begin(); it != _particles.end(); it++ )
{
_image.setPixel( (int)(*it)->pos.x, (int)(*it)->pos.y, _transparent );
}
}