FrameAnimation.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_FRAMEANIMATION_HPP
30 #define THOR_FRAMEANIMATION_HPP
31 
32 #include <Thor/Config.hpp>
33 #include <Aurora/Tools/ForEach.hpp>
34 
35 #include <SFML/Graphics/Rect.hpp>
36 
37 #include <vector>
38 #include <cassert>
39 
40 
41 namespace thor
42 {
43 namespace detail
44 {
45 
46  // Class that stores a single frame of FrameAnimation
47  struct Frame
48  {
49  Frame(float duration, const sf::IntRect& subrect)
50  : duration(duration)
51  , subrect(subrect)
52  , origin()
53  , applyOrigin(false)
54  {
55  }
56 
57  Frame(float duration, const sf::IntRect& subrect, sf::Vector2f origin)
58  : duration(duration)
59  , subrect(subrect)
60  , origin(origin)
61  , applyOrigin(true)
62  {
63  }
64 
65  mutable float duration;
66  sf::IntRect subrect;
67  sf::Vector2f origin;
68  bool applyOrigin;
69  };
70 
71 } // namespace detail
72 
73 // ---------------------------------------------------------------------------------------------------------------------------
74 
75 
78 
82 class THOR_API FrameAnimation
83 {
84  // ---------------------------------------------------------------------------------------------------------------------------
85  // Public member functions
86  public:
90 
94  void addFrame(float relativeDuration, const sf::IntRect& subrect);
95 
100  void addFrame(float relativeDuration, const sf::IntRect& subrect, sf::Vector2f origin);
101 
107  template <class Animated>
108  void operator() (Animated& animated, float progress) const;
109 
110 
111  // ---------------------------------------------------------------------------------------------------------------------------
112  // Private member functions
113  private:
114  void ensureNormalized() const;
115 
116 
117  // ---------------------------------------------------------------------------------------------------------------------------
118  // Private variables
119  private:
120  std::vector<detail::Frame> mFrames;
121  mutable bool mNormalized;
122 };
123 
125 
126 // ---------------------------------------------------------------------------------------------------------------------------
127 
128 
129 template <class Animated>
130 void FrameAnimation::operator() (Animated& target, float progress) const
131 {
132  assert(!mFrames.empty());
133  assert(progress >= 0.f && progress <= 1.f);
134 
135  ensureNormalized();
136  AURORA_FOREACH(const detail::Frame& frame, mFrames)
137  {
138  progress -= frame.duration;
139 
140  if (progress < 0.f)
141  {
142  target.setTextureRect(frame.subrect);
143  if (frame.applyOrigin)
144  target.setOrigin(frame.origin);
145 
146  break;
147  }
148  }
149 }
150 
151 } // namespace thor
152 
153 #endif // THOR_FRAMEANIMATION_HPP
Changes a sprite's subrect over time.
Definition: FrameAnimation.hpp:82
Definition: Animator.hpp:42
Configuration header of the library.
void operator()(Animated &animated, float progress) const
Animates the object.
Definition: FrameAnimation.hpp:130