Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00025
00028
00029 #ifndef THOR_PARTICLESYSTEM_HPP
00030 #define THOR_PARTICLESYSTEM_HPP
00031
00032 #include <Thor/Particles/Particle.hpp>
00033 #include <Thor/Particles/EmissionInterface.hpp>
00034 #include <Thor/Input/Connection.hpp>
00035 #include <Thor/Config.hpp>
00036
00037 #include <SFML/System/NonCopyable.hpp>
00038 #include <SFML/System/Vector2.hpp>
00039 #include <SFML/Graphics/Rect.hpp>
00040 #include <SFML/Graphics/Drawable.hpp>
00041 #include <SFML/Graphics/VertexArray.hpp>
00042
00043 #include <vector>
00044 #include <utility>
00045 #include <functional>
00046 #include <memory>
00047 #include <array>
00048
00049
00050 namespace sf
00051 {
00052
00053 class RenderWindow;
00054 class Image;
00055 class Texture;
00056
00057 }
00058
00059
00060 namespace thor
00061 {
00062 namespace detail
00063 {
00064 class AbstractConnectionImpl;
00065 }
00066
00069
00075 class THOR_API ParticleSystem : public sf::Drawable, private sf::NonCopyable, private EmissionInterface
00076 {
00077
00078
00079 private:
00080
00081 template <typename Signature>
00082 struct Function
00083 {
00084 Function(std::function<Signature> function, sf::Time timeUntilRemoval)
00085 : function(std::move(function))
00086 , timeUntilRemoval(timeUntilRemoval)
00087 , id(nextId())
00088 , tracker()
00089 {
00090 }
00091
00092 static unsigned int nextId()
00093 {
00094 static unsigned int next = 0;
00095 return next++;
00096 }
00097
00098 std::function<Signature> function;
00099 sf::Time timeUntilRemoval;
00100 unsigned int id;
00101 std::shared_ptr<detail::AbstractConnectionImpl> tracker;
00102 };
00103
00104
00105 typedef std::array<sf::Vertex, 4> Quad;
00106
00107
00108 typedef Function<void(Particle&, sf::Time)> Affector;
00109 typedef Function<void(EmissionInterface&, sf::Time)> Emitter;
00110
00111
00112 typedef std::vector<Particle> ParticleContainer;
00113 typedef std::vector<Affector> AffectorContainer;
00114 typedef std::vector<Emitter> EmitterContainer;
00115
00116
00117
00118
00119 public:
00122 ParticleSystem();
00123
00126 ParticleSystem(ParticleSystem&& source);
00127
00130 ParticleSystem& operator= (ParticleSystem&& source);
00131
00137 void setTexture(const sf::Texture& texture);
00138
00145 unsigned int addTextureRect(const sf::IntRect& textureRect);
00146
00153 Connection addAffector(std::function<void(Particle&, sf::Time)> affector);
00154
00162 Connection addAffector(std::function<void(Particle&, sf::Time)> affector, sf::Time timeUntilRemoval);
00163
00166 void clearAffectors();
00167
00171 Connection addEmitter(std::function<void(EmissionInterface&, sf::Time)> emitter);
00172
00177 Connection addEmitter(std::function<void(EmissionInterface&, sf::Time)> emitter, sf::Time timeUntilRemoval);
00178
00182 void clearEmitters();
00183
00188 void update(sf::Time dt);
00189
00192 void clearParticles();
00193
00194
00195
00196
00197 private:
00201 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
00202
00205 virtual void emitParticle(const Particle& particle);
00206
00207
00208 void updateParticle(Particle& particle, sf::Time dt);
00209
00210
00211 void computeVertices() const;
00212
00213
00214 void computeQuads() const;
00215 void computeQuad(Quad& quad, const sf::IntRect& textureRect) const;
00216
00217
00218
00219
00220 private:
00221 ParticleContainer mParticles;
00222 AffectorContainer mAffectors;
00223 EmitterContainer mEmitters;
00224
00225 const sf::Texture* mTexture;
00226 std::vector<sf::IntRect> mTextureRects;
00227
00228 mutable sf::VertexArray mVertices;
00229 mutable bool mNeedsVertexUpdate;
00230 mutable std::vector<Quad> mQuads;
00231 mutable bool mNeedsQuadUpdate;
00232 };
00233
00235
00236 }
00237
00238 #endif // THOR_PARTICLESYSTEM_HPP