Bromeon
Animation/FrameAnimation.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_FRAMEANIMATION_HPP
00030 #define THOR_FRAMEANIMATION_HPP
00031 
00032 #include <Thor/Animation/Animation.hpp>
00033 #include <Thor/Resources/ResourcePtr.hpp>
00034 #include <Thor/Config.hpp>
00035 
00036 #include <SFML/Graphics/Sprite.hpp>
00037 
00038 #include <vector>
00039 
00040 
00041 namespace thor
00042 {
00043 
00046 
00050 class THOR_API FrameAnimation : public Animation
00051 {
00052     // ---------------------------------------------------------------------------------------------------------------------------
00053     // Public types
00054     public:
00057         typedef std::tr1::shared_ptr<FrameAnimation>    Ptr;
00058         
00059         
00060     // ---------------------------------------------------------------------------------------------------------------------------
00061     // Public static member functions
00062     public:
00065         static Ptr                  Create();
00066         
00067         
00068     // ---------------------------------------------------------------------------------------------------------------------------
00069     // Public member functions
00070     public:
00073                                     FrameAnimation();
00074 
00078         void                        AddFrame(float relativeDuration, const sf::IntRect& subrect);
00079 
00084         void                        AddFrame(float relativeDuration, ResourcePtr<const sf::Texture> texture,
00085                                         const sf::IntRect& subrect);
00086 
00087         virtual void                Apply(sf::Sprite& target, float progress) const;
00088 
00089 
00090     // ---------------------------------------------------------------------------------------------------------------------------
00091     // Private types
00092     private:
00093         // Frame with sub-rectangle and duration
00094         struct Frame
00095         {
00096                                             Frame(float duration, ResourcePtr<const sf::Texture> texture, const sf::IntRect& subrect);
00097 
00098             mutable float                   duration;
00099             sf::IntRect                     subrect;
00100             ResourcePtr<const sf::Texture>  texture;
00101         };
00102 
00103         // Functor to find current frame
00104         struct ExhaustTime
00105         {
00106             explicit                    ExhaustTime(float time);
00107             bool                        operator() (const Frame& frame);
00108 
00109             float                       time;
00110         };
00111 
00112 
00113     // ---------------------------------------------------------------------------------------------------------------------------
00114     // Private member functions
00115     private:
00116         void                            EnsureNormalized() const;
00117 
00118 
00119     // ---------------------------------------------------------------------------------------------------------------------------
00120     // Private variables
00121     private:
00122         std::vector<Frame>          mFrames;
00123         mutable bool                mNormalized;
00124 };
00125 
00127 
00128 } // namespace thor
00129 
00130 #endif // THOR_FRAMEANIMATION_HPP