Animator.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2015 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_ANIMATOR_HPP
30 #define THOR_ANIMATOR_HPP
31 
32 #include <Thor/Config.hpp>
33 
34 #include <SFML/System/Time.hpp>
35 
36 #include <map>
37 #include <functional>
38 #include <cassert>
39 #include <cmath>
40 
41 
42 namespace thor
43 {
44 
47 
51 template <class Animated, typename Id>
52 class Animator
53 {
54  // ---------------------------------------------------------------------------------------------------------------------------
55  // Public types
56  public:
61  typedef std::function<void(Animated&, float)> AnimationFunction;
62 
63 
64  // ---------------------------------------------------------------------------------------------------------------------------
65  // Public member functions
66  public:
69  Animator();
70 
73  Animator(const Animator& origin);
74 
77  Animator& operator= (const Animator& origin);
78 
81  Animator(Animator&& source);
82 
85  Animator& operator= (Animator&& source);
86 
89  ~Animator();
90 
96  void addAnimation(const Id& id, const AnimationFunction& animation, sf::Time duration);
97 
101  void playAnimation(const Id& id, bool loop = false);
102 
106  void stopAnimation();
107 
111  bool isPlayingAnimation() const;
112 
116  Id getPlayingAnimation() const;
117 
120  void update(sf::Time dt);
121 
125  void animate(Animated& animated) const;
126 
127 
128  // ---------------------------------------------------------------------------------------------------------------------------
129  // Private types
130  private:
131  typedef std::pair<AnimationFunction, sf::Time> ScaledAnimation;
132  typedef std::map<Id, ScaledAnimation> AnimationMap;
133  typedef typename AnimationMap::iterator AnimationMapIterator;
134 
135 
136  // ---------------------------------------------------------------------------------------------------------------------------
137  // Private member functions
138  private:
139  void playAnimation(AnimationMapIterator animation, bool loop);
140 
141  template <typename T>
142  void adopt(T& source);
143 
144 
145  // ---------------------------------------------------------------------------------------------------------------------------
146  // Private variables
147  private:
148  AnimationMap mAnimationMap;
149  AnimationMapIterator mPlayingAnimation;
150  float mProgress;
151  bool mLoop;
152 };
153 
155 
156 } // namespace thor
157 
158 #include <Thor/Animations/Detail/Animator.inl>
159 #endif // THOR_ANIMATOR_HPP
void addAnimation(const Id &id, const AnimationFunction &animation, sf::Time duration)
Registers an animation with a given name.
Definition: Animator.hpp:42
~Animator()
Destructor.
void playAnimation(const Id &id, bool loop=false)
Plays the animation with the given name.
Class that stores the progress of an object's animation.
Definition: Animator.hpp:52
Configuration header of the library.
std::function< void(Animated &, float)> AnimationFunction
Functor to animate the objects.
Definition: Animator.hpp:61
bool isPlayingAnimation() const
Checks whether an animation is currently playing.
void animate(Animated &animated) const
Applies the stored animations to an object.
void update(sf::Time dt)
Updates the animator's progress. You should call this method each frame.
Animator()
Default constructor.
Animator & operator=(const Animator &origin)
Copy assignment operator.
Id getPlayingAnimation() const
Returns the ID of the currently playing animation.
void stopAnimation()
Interrupts the animation that is currently active.