AnimationMap.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2016 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_ANIMATIONMAP_HPP
30 #define THOR_ANIMATIONMAP_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 namespace detail
45 {
46 
47  template <class Animated>
48  struct TimedAnimation
49  {
50  // @brief Functor to animate the objects.
51  // @details Signature: <b>void (Animated& animated, float progress)</b>
52  // @arg @a animated is the object being animated.
53  // @arg @a progress is a number in [0,1] determining the animation state.
54  typedef std::function<void(Animated&, float)> AnimationFunction;
55 
56  TimedAnimation(AnimationFunction animation, sf::Time duration)
57  : function(std::move(animation))
58  , duration(duration)
59  {
60  }
61 
62  AnimationFunction function;
63  sf::Time duration;
64  };
65 
66 } // namespace detail
67 
68 
71 
72 template <class Animated, typename Id>
73 class Animator;
74 
81 template <class Animated, typename Id>
83 {
84  // ---------------------------------------------------------------------------------------------------------------------------
85  // Private types
86  private:
87  typedef detail::TimedAnimation<Animated> TimedAnimation;
88 
89 
90  // ---------------------------------------------------------------------------------------------------------------------------
91  // Public member functions
92  public:
95  AnimationMap();
96 
107  void addAnimation(Id id, std::function<void(Animated&, float)> animation, sf::Time duration);
108 
109 
110  // ---------------------------------------------------------------------------------------------------------------------------
111  // Private member functions (accessible through friend)
112  private:
113  const TimedAnimation& getAnimation(const Id& id) const;
114 
115 
116  // ---------------------------------------------------------------------------------------------------------------------------
117  // Private variables
118  private:
119  std::map<Id, TimedAnimation> mAnimationMap;
120 
121 
122  // ---------------------------------------------------------------------------------------------------------------------------
123  // Friends
124  friend class Animator<Animated, Id>;
125 };
126 
128 
129 } // namespace thor
130 
131 #include <Thor/Animations/Detail/AnimationMap.inl>
132 #endif // THOR_ANIMATIONMAP_HPP
Class that stores multiple animations.
Definition: AnimationMap.hpp:82
Definition: AnimationMap.hpp:42
Class to animate objects.
Definition: AnimationMap.hpp:73
Configuration header of the library.
void addAnimation(Id id, std::function< void(Animated &, float)> animation, sf::Time duration)
Registers an animation with a given identifier.
AnimationMap()
Default constructor.