Particles/ParticleSystem.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2015 Jan Haller
00005 // 
00006 // This software is provided 'as-is', without any express or implied
00007 // warranty. In no event will the authors be held liable for any damages
00008 // arising from the use of this software.
00009 // 
00010 // Permission is granted to anyone to use this software for any purpose,
00011 // including commercial applications, and to alter it and redistribute it
00012 // freely, subject to the following restrictions:
00013 // 
00014 // 1. The origin of this software must not be misrepresented; you must not
00015 //    claim that you wrote the original software. If you use this software
00016 //    in a product, an acknowledgment in the product documentation would be
00017 //    appreciated but is not required.
00018 // 
00019 // 2. Altered source versions must be plainly marked as such, and must not be
00020 //    misrepresented as being the original software.
00021 // 
00022 // 3. This notice may not be removed or altered from any source distribution.
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 } // namespace sf
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     // Private types
00079     private:
00080         // Type to store affector or emitter + time until removal + id for removal
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         // Vertex quads, used to cache texture rectangles
00105         typedef std::array<sf::Vertex, 4>                   Quad;
00106 
00107         // Function typedefs
00108         typedef Function<void(Particle&, sf::Time)>         Affector;
00109         typedef Function<void(EmissionInterface&, sf::Time)>    Emitter;
00110 
00111         // Container typedefs
00112         typedef std::vector<Particle>                       ParticleContainer;
00113         typedef std::vector<Affector>                       AffectorContainer;
00114         typedef std::vector<Emitter>                        EmitterContainer;
00115 
00116 
00117     // ---------------------------------------------------------------------------------------------------------------------------
00118     // Public member functions
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     // Private member functions
00197     private:
00201         virtual void                draw(sf::RenderTarget& target, sf::RenderStates states) const;
00202 
00205         virtual void                emitParticle(const Particle& particle);
00206 
00207         // Updates a single particle.
00208         void                        updateParticle(Particle& particle, sf::Time dt);
00209 
00210         // Recomputes the vertex array.
00211         void                        computeVertices() const;
00212 
00213         // Recomputes the cached rectangles (position and texCoords quads)
00214         void                        computeQuads() const;
00215         void                        computeQuad(Quad& quad, const sf::IntRect& textureRect) const;
00216 
00217 
00218     // ---------------------------------------------------------------------------------------------------------------------------
00219     // Private variables
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 } // namespace thor
00237 
00238 #endif // THOR_PARTICLESYSTEM_HPP