123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- /**
- * 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 <http://www.gnu.org/licenses/>.
- */
-
- /**
- * This is based on a wiki entry
- * https://github.com/SFML/SFML/wiki/Source:-Particle-System
- */
- #pragma once
- #include <SFML/System/Vector2.hpp>
- #include <SFML/System/Clock.hpp>
- #include <SFML/Graphics/Color.hpp>
- #include <SFML/Graphics/Image.hpp>
- #include <SFML/Graphics/Sprite.hpp>
- #include <SFML/Graphics/Texture.hpp>
- #include <vector>
- #include <random>
- class Randomizer
- {
- public:
- Randomizer() : device_(), engine_(device_()){};
- int rnd(int a, int b) {
- std::uniform_int_distribution<int> uni_dist(a, b);
- return uni_dist(engine_);
- };
- double rnd(double a, double b) {
- std::uniform_real_distribution<double> uni_dist(a, b);
- return uni_dist(engine_);
- };
- private:
- std::random_device device_;
- std::default_random_engine engine_;
- };
- struct Particle
- {
- sf::Vector2f pos;
- sf::Vector2f vel;
- sf::Color color;
- float transparency;
- };
- class ParticleSystem
- {
- public:
- ParticleSystem( int width, int height );
- ~ParticleSystem();
- void fuel(int particles, float angle);
- void update(sf::Time delta);
- void render();
- void clear();
- void setPosition(const sf::Vector2f position)
- {
- _position = position;
- }
- void setPosition(float x, float y)
- {
- setPosition(sf::Vector2f(x, y));
- }
- void setParticleSpeed(float particleSpeed)
- {
- _particleSpeed = particleSpeed;
- }
- void setDissolutionRate(float dissolutionRate)
- {
- _dissolutionRate = dissolutionRate;
- }
- const sf::Sprite& getSprite() const
- {
- return _sprite;
- }
- private:
- sf::Vector2f _position; // Particle origin (pixel co-ordinates)
- sf::Clock _clock; // Used to scale particle motion
- sf::Color _transparent; // sf::Color( 0, 0, 0, 0 )
- sf::Image _image; // See render() and remove()
- sf::Texture _texture;
- Randomizer _randomizer;
- sf::Sprite _sprite; // Connected to m_image
- float _particleSpeed;// Pixels per second (at most)
- float _dissolutionRate;
- std::vector<Particle*> _particles;
- };
|