Animations/Animator.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_ANIMATOR_HPP
00030 #define THOR_ANIMATOR_HPP
00031 
00032 #include <Thor/Config.hpp>
00033 
00034 #include <SFML/System/Time.hpp>
00035 
00036 #include <map>
00037 #include <functional>
00038 #include <cassert>
00039 #include <cmath>
00040 
00041 
00042 namespace thor
00043 {
00044 
00047 
00051 template <class Animated, typename Id>
00052 class Animator
00053 {
00054     // ---------------------------------------------------------------------------------------------------------------------------
00055     // Public types
00056     public:
00061         typedef std::function<void(Animated&, float)>   AnimationFunction;
00062 
00063 
00064     // ---------------------------------------------------------------------------------------------------------------------------
00065     // Public member functions
00066     public:
00069                                     Animator();
00070 
00073                                     Animator(const Animator& origin);
00074 
00077         Animator&                   operator= (const Animator& origin);
00078 
00081                                     Animator(Animator&& source);
00082 
00085         Animator&                   operator= (Animator&& source);
00086 
00089                                     ~Animator();
00090 
00096         void                        addAnimation(const Id& id, const AnimationFunction& animation, sf::Time duration);
00097 
00101         void                        playAnimation(const Id& id, bool loop = false);
00102 
00106         void                        stopAnimation();
00107 
00111         bool                        isPlayingAnimation() const;
00112 
00116         Id                          getPlayingAnimation() const;
00117 
00120         void                        update(sf::Time dt);
00121 
00125         void                        animate(Animated& animated) const;
00126 
00127 
00128     // ---------------------------------------------------------------------------------------------------------------------------
00129     // Private types
00130     private:
00131         typedef std::pair<AnimationFunction, sf::Time>      ScaledAnimation;
00132         typedef std::map<Id, ScaledAnimation>               AnimationMap;
00133         typedef typename AnimationMap::iterator             AnimationMapIterator;
00134 
00135 
00136     // ---------------------------------------------------------------------------------------------------------------------------
00137     // Private member functions
00138     private:
00139         void                        playAnimation(AnimationMapIterator animation, bool loop);
00140 
00141         template <typename T>
00142         void                        adopt(T& source);
00143 
00144 
00145     // ---------------------------------------------------------------------------------------------------------------------------
00146     // Private variables
00147     private:
00148         AnimationMap                mAnimationMap;
00149         AnimationMapIterator        mPlayingAnimation;
00150         float                       mProgress;
00151         bool                        mLoop;
00152 };
00153 
00155 
00156 } // namespace thor
00157 
00158 #include <Thor/Animations/Detail/Animator.inl>
00159 #endif // THOR_ANIMATOR_HPP