123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /**
- * 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 <vector>
- #include <SFML/Graphics.hpp>
- #include <iostream>
- class ParticleSystem: public sf::Drawable
- {
- public:
- ParticleSystem(int width, int height);
- ~ParticleSystem();
- void fuel(int particles, float angle);
- void update(sf::Time delta);
- void render();
- void clear();
-
- void enable(bool enabled)
- {
- _enabled = enabled;
- }
- const bool isEnabled() const
- {
- return _enabled;
- }
- void loadColors(const sf::Image& image)
- {
- _colors.clear();
- for(unsigned i = 0; i < image.getSize().x; ++i)
- for(unsigned j = 0; j < image.getSize().y; ++j)
- {
- sf::Color color = image.getPixel(i, j);
- if(color.r > 0 || color.g > 0 || color.b > 0)
- _colors.emplace_back(color.r, color.g, color.b, 255);
- }
- }
- void setPosition(const sf::Vector2f position)
- {
- _sprite.setPosition(position - _origin);
- }
- void setPosition(float x, float y)
- {
- setPosition(sf::Vector2f(x, y));
- }
- void setOrigin(const sf::Vector2f origin)
- {
- _origin = origin;
- }
- void setOrigin(float x, float y)
- {
- setOrigin(sf::Vector2f(x, y));
- }
- void setParticleSpeed(float particleSpeed)
- {
- _particleSpeed = particleSpeed;
- }
- void setDissolutionRate(float dissolutionRate)
- {
- _dissolutionRate = dissolutionRate;
- }
- const sf::Sprite& getSprite() const
- {
- return _sprite;
- }
-
- private:
- struct Particle
- {
- sf::Vector2f pos;
- sf::Vector2f vel;
- sf::Color color;
- float transparency;
- };
- virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
- sf::Vector2f _origin;
- sf::Clock _clock;
- sf::Image _image;
- sf::Texture _texture;
- sf::Sprite _sprite;
- float _particleSpeed;
- float _dissolutionRate;
- std::vector<Particle*> _particles;
- std::vector<sf::Color> _colors;
- bool _enabled;
- };
|