Animator.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_ANIMATOR_HPP
30 #define THOR_ANIMATOR_HPP
31 
32 #include <Thor/Config.hpp>
35 
36 #include <Aurora/Tools/Optional.hpp>
37 #include <Aurora/Tools/Algorithms.hpp>
38 
39 #include <SFML/System/Time.hpp>
40 
41 #include <queue>
42 #include <functional>
43 #include <cassert>
44 #include <cmath>
45 
46 
47 namespace thor
48 {
49 
52 
66 template <class Animated, typename Id>
67 class Animator
68 {
69  // ---------------------------------------------------------------------------------------------------------------------------
70  // Public types
71  public:
76  typedef std::function<void(Animated&, float)> AnimationFunction;
77 
78 
79  // ---------------------------------------------------------------------------------------------------------------------------
80  // Public member functions
81  public:
85  explicit Animator(const AnimationMap<Animated, Id>& animations);
86 
96  AURORA_IMPL_DEF(detail::PlayingQueue<Id>) play();
97 
107  AURORA_IMPL_DEF(detail::PlayingQueue<Id>) queue();
108 
112  void stop();
113 
121  void update(sf::Time dt);
122 
128  void animate(Animated& animated) const;
129 
130 
131  // ---------------------------------------------------------------------------------------------------------------------------
132  // Private types
133  private:
134  typedef detail::TimedAnimation<Animated> TimedAnimation;
135 
136 
137  // ---------------------------------------------------------------------------------------------------------------------------
138  // Private variables
139  private:
140  const AnimationMap<Animated, Id>* mAnimations;
141  std::queue<detail::PlaybackScheme<Id>> mQueuedIds;
142  std::vector<const TimedAnimation*> mPlayingAnimations;
143  sf::Time mProgressedTime;
144 };
145 
147 
148 } // namespace thor
149 
150 #include <Thor/Animations/Detail/Animator.inl>
151 #endif // THOR_ANIMATOR_HPP
Animator(const AnimationMap< Animated, Id > &animations)
Construct from animation map.
void stop()
Stops all playing and queued animations.
Class that stores multiple animations.
Definition: AnimationMap.hpp:82
Definition: AnimationMap.hpp:42
Configuration header of the library.
std::function< void(Animated &, float)> AnimationFunction
Functor to animate the objects.
Definition: Animator.hpp:76
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.
Namespace thor::Playback.
Definition: BigSprite.hpp:41
Class template thor::AnimationMap.
ImplementationDefined play()
Plays one or more animations, discarding the current queue.
ImplementationDefined queue()
Plays one or more animations, appending to current queue.