Animations/FrameAnimation.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_FRAMEANIMATION_HPP
00030 #define THOR_FRAMEANIMATION_HPP
00031 
00032 #include <Thor/Config.hpp>
00033 #include <Aurora/Tools/ForEach.hpp>
00034 
00035 #include <SFML/Graphics/Rect.hpp>
00036 
00037 #include <vector>
00038 #include <cassert>
00039 
00040 
00041 namespace thor
00042 {
00043 namespace detail
00044 {
00045 
00046     // Class that stores a single frame of FrameAnimation
00047     struct Frame
00048     {
00049         Frame(float duration, const sf::IntRect& subrect)
00050         : duration(duration)
00051         , subrect(subrect)
00052         , origin()
00053         , applyOrigin(false)
00054         {
00055         }
00056                 
00057         Frame(float duration, const sf::IntRect& subrect, sf::Vector2f origin)
00058         : duration(duration)
00059         , subrect(subrect)
00060         , origin(origin)
00061         , applyOrigin(true)
00062         {
00063         }
00064 
00065         mutable float           duration;
00066         sf::IntRect             subrect;
00067         sf::Vector2f            origin;
00068         bool                    applyOrigin;
00069     };
00070 
00071 } // namespace detail
00072 
00073 // ---------------------------------------------------------------------------------------------------------------------------
00074 
00075 
00078 
00082 class THOR_API FrameAnimation
00083 {
00084     // ---------------------------------------------------------------------------------------------------------------------------
00085     // Public member functions
00086     public:
00089                                     FrameAnimation();
00090 
00094         void                        addFrame(float relativeDuration, const sf::IntRect& subrect);
00095 
00100         void                        addFrame(float relativeDuration, const sf::IntRect& subrect, sf::Vector2f origin);
00101 
00107         template <class Animated>
00108         void                        operator() (Animated& animated, float progress) const;
00109 
00110 
00111     // ---------------------------------------------------------------------------------------------------------------------------
00112     // Private member functions
00113     private:
00114         void                        ensureNormalized() const;
00115 
00116 
00117     // ---------------------------------------------------------------------------------------------------------------------------
00118     // Private variables
00119     private:
00120         std::vector<detail::Frame>  mFrames;
00121         mutable bool                mNormalized;
00122 };
00123 
00125 
00126 // ---------------------------------------------------------------------------------------------------------------------------
00127 
00128 
00129 template <class Animated>
00130 void FrameAnimation::operator() (Animated& target, float progress) const
00131 {
00132     assert(!mFrames.empty());
00133     assert(progress >= 0.f && progress <= 1.f);
00134 
00135     ensureNormalized();
00136     AURORA_FOREACH(const detail::Frame& frame, mFrames)
00137     {
00138         progress -= frame.duration;
00139 
00140         if (progress < 0.f)
00141         {
00142             target.setTextureRect(frame.subrect);
00143             if (frame.applyOrigin)
00144                 target.setOrigin(frame.origin);
00145 
00146             break;
00147         }
00148     }
00149 }
00150 
00151 } // namespace thor
00152 
00153 #endif // THOR_FRAMEANIMATION_HPP