ParticleSystem.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2015 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_PARTICLESYSTEM_HPP
30 #define THOR_PARTICLESYSTEM_HPP
31 
35 #include <Thor/Config.hpp>
36 
37 #include <SFML/System/NonCopyable.hpp>
38 #include <SFML/System/Vector2.hpp>
39 #include <SFML/Graphics/Rect.hpp>
40 #include <SFML/Graphics/Drawable.hpp>
41 #include <SFML/Graphics/VertexArray.hpp>
42 
43 #include <vector>
44 #include <utility>
45 #include <functional>
46 #include <memory>
47 #include <array>
48 
49 
50 namespace sf
51 {
52 
53  class RenderWindow;
54  class Image;
55  class Texture;
56 
57 } // namespace sf
58 
59 
60 namespace thor
61 {
62 namespace detail
63 {
64  class AbstractConnectionImpl;
65 }
66 
69 
75 class THOR_API ParticleSystem : public sf::Drawable, private sf::NonCopyable, private EmissionInterface
76 {
77  // ---------------------------------------------------------------------------------------------------------------------------
78  // Private types
79  private:
80  // Type to store affector or emitter + time until removal + id for removal
81  template <typename Signature>
82  struct Function
83  {
84  Function(std::function<Signature> function, sf::Time timeUntilRemoval)
85  : function(std::move(function))
86  , timeUntilRemoval(timeUntilRemoval)
87  , id(nextId())
88  , tracker()
89  {
90  }
91 
92  static unsigned int nextId()
93  {
94  static unsigned int next = 0;
95  return next++;
96  }
97 
98  std::function<Signature> function;
99  sf::Time timeUntilRemoval;
100  unsigned int id;
101  std::shared_ptr<detail::AbstractConnectionImpl> tracker;
102  };
103 
104  // Vertex quads, used to cache texture rectangles
105  typedef std::array<sf::Vertex, 4> Quad;
106 
107  // Function typedefs
108  typedef Function<void(Particle&, sf::Time)> Affector;
109  typedef Function<void(EmissionInterface&, sf::Time)> Emitter;
110 
111  // Container typedefs
112  typedef std::vector<Particle> ParticleContainer;
113  typedef std::vector<Affector> AffectorContainer;
114  typedef std::vector<Emitter> EmitterContainer;
115 
116 
117  // ---------------------------------------------------------------------------------------------------------------------------
118  // Public member functions
119  public:
122  ParticleSystem();
123 
126  ParticleSystem(ParticleSystem&& source);
127 
130  ParticleSystem& operator= (ParticleSystem&& source);
131 
137  void setTexture(const sf::Texture& texture);
138 
145  unsigned int addTextureRect(const sf::IntRect& textureRect);
146 
153  Connection addAffector(std::function<void(Particle&, sf::Time)> affector);
154 
162  Connection addAffector(std::function<void(Particle&, sf::Time)> affector, sf::Time timeUntilRemoval);
163 
166  void clearAffectors();
167 
171  Connection addEmitter(std::function<void(EmissionInterface&, sf::Time)> emitter);
172 
177  Connection addEmitter(std::function<void(EmissionInterface&, sf::Time)> emitter, sf::Time timeUntilRemoval);
178 
182  void clearEmitters();
183 
188  void update(sf::Time dt);
189 
192  void clearParticles();
193 
194 
195  // ---------------------------------------------------------------------------------------------------------------------------
196  // Private member functions
197  private:
201  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
202 
205  virtual void emitParticle(const Particle& particle);
206 
207  // Updates a single particle.
208  void updateParticle(Particle& particle, sf::Time dt);
209 
210  // Recomputes the vertex array.
211  void computeVertices() const;
212 
213  // Recomputes the cached rectangles (position and texCoords quads)
214  void computeQuads() const;
215  void computeQuad(Quad& quad, const sf::IntRect& textureRect) const;
216 
217 
218  // ---------------------------------------------------------------------------------------------------------------------------
219  // Private variables
220  private:
221  ParticleContainer mParticles;
222  AffectorContainer mAffectors;
223  EmitterContainer mEmitters;
224 
225  const sf::Texture* mTexture;
226  std::vector<sf::IntRect> mTextureRects;
227 
228  mutable sf::VertexArray mVertices;
229  mutable bool mNeedsVertexUpdate;
230  mutable std::vector<Quad> mQuads;
231  mutable bool mNeedsQuadUpdate;
232 };
233 
235 
236 } // namespace thor
237 
238 #endif // THOR_PARTICLESYSTEM_HPP
Class that connects emitters with their corresponding particle system.
Definition: EmissionInterface.hpp:46
Definition: Animator.hpp:42
Class thor::EmissionInterface.
Configuration header of the library.
Classes thor::Connection, thor::ScopedConnection.
Class that maintains control over a registered object.
Definition: Connection.hpp:57
Structure thor::Particle.
Definition: BigSprite.hpp:41
Class for particle systems.
Definition: ParticleSystem.hpp:75
Particle class
Definition: Particle.hpp:49