Bromeon
Particles/ParticleSystem.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2012 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/ParticleInterfaces.hpp>
00033 #include <Thor/Particles/Particle.hpp>
00034 #include <Thor/Resources/ResourcePtr.hpp>
00035 #include <Thor/Tools/NonCopyable.hpp>
00036 #include <Thor/Detail/Swap.hpp>
00037 #include <Thor/Config.hpp>
00038 
00039 #include <SFML/Graphics/Rect.hpp>
00040 #include <SFML/System/Vector2.hpp>
00041 
00042 #include THOR_TR1_HEADER(functional)
00043 #include <vector>
00044 #include <utility>
00045 
00046 
00047 namespace sf
00048 {
00049 
00050     class RenderWindow;
00051     class Image;
00052     class Texture;
00053 
00054 } // namespace sf
00055 
00056 
00057 namespace thor
00058 {
00059 
00062 
00067 class THOR_API ParticleSystem : private NonCopyable, private Emitter::Adder
00068 {       
00069     // ---------------------------------------------------------------------------------------------------------------------------
00070     // Private types
00071     private:
00072         // Container typedefs
00073         typedef std::vector< Particle >                                     ParticleContainer;
00074         typedef std::vector< std::pair<Affector::Ptr, sf::Time> >           AffectorContainer;
00075         typedef std::vector< std::pair<Emitter::Ptr, sf::Time> >            EmitterContainer;
00076 
00077 
00078     // ---------------------------------------------------------------------------------------------------------------------------
00079     // Public member functions
00080     public:
00083         explicit                    ParticleSystem(ResourcePtr<const sf::Texture> texture);
00084 
00088                                     ParticleSystem(ResourcePtr<const sf::Texture> texture, const sf::IntRect& textureRect);
00089 
00092         void                        Swap(ParticleSystem& other);
00093                 
00100         void                        AddAffector(Affector::Ptr affector);
00101 
00109         void                        AddAffector(Affector::Ptr affector, sf::Time timeUntilRemoval);
00110 
00114         void                        RemoveAffector(Affector::Ptr affector);
00115 
00119         void                        ClearAffectors();
00120 
00123         bool                        ContainsAffector(Affector::Ptr affector) const;
00124 
00128         void                        AddEmitter(Emitter::Ptr emitter);
00129 
00134         void                        AddEmitter(Emitter::Ptr emitter, sf::Time timeUntilRemoval);
00135 
00139         void                        RemoveEmitter(Emitter::Ptr emitter);
00140 
00144         void                        ClearEmitters();
00145         
00148         bool                        ContainsEmitter(Emitter::Ptr emitter) const;
00149 
00154         void                        Update(sf::Time dt);
00155         
00158         void                        Draw(sf::RenderWindow& target) const;
00159         
00162         void                        ClearParticles();
00163         
00167         void                        SetGlowing(bool glow);
00168         
00171         bool                        IsGlowing() const;
00172 
00173 
00174     // ---------------------------------------------------------------------------------------------------------------------------
00175     // Private member functions
00176     private:
00179         virtual void                AddParticle(const Particle& particle);
00180                 
00181         // Saves SFML's OpenGL state and prepares the particle system for rendering.
00182         void                        PushOpenGLStates(sf::RenderWindow& target) const;
00183         
00184         // Restores SFML's OpenGL state.
00185         void                        PopOpenGLStates() const;
00186             
00187         // Updates a single particle.
00188         void                        UpdateParticle(Particle& particle, sf::Time dt);
00189 
00190         // Draws a single particle.
00191         void                        DrawParticle(const Particle& particle) const;
00192 
00193 
00194     // ---------------------------------------------------------------------------------------------------------------------------
00195     // Private variables
00196     private:
00197         ParticleContainer               mParticles;
00198         AffectorContainer               mAffectors;
00199         EmitterContainer                mEmitters;
00200 
00201         ResourcePtr<const sf::Texture>  mTexture;
00202         sf::Vector2f                    mTexCoordsBegin;
00203         sf::Vector2f                    mTexCoordsEnd;
00204         sf::Vector2f                    mHalfSize;
00205         bool                            mGlow;
00206 };
00207 
00210 THOR_GLOBAL_SWAP(ParticleSystem)
00211 
00212 
00213 
00214 } // namespace thor
00215 
00216 #endif // THOR_PARTICLESYSTEM_HPP