Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00025
00028
00029 #ifndef THOR_FRAMEANIMATION_HPP
00030 #define THOR_FRAMEANIMATION_HPP
00031
00032 #include <Thor/Config.hpp>
00033 #include <Aurora/Tools/ForEach.hpp>
00034
00035 #include <SFML/Graphics/Rect.hpp>
00036
00037 #include <vector>
00038 #include <cassert>
00039
00040
00041 namespace thor
00042 {
00043 namespace detail
00044 {
00045
00046
00047 struct Frame
00048 {
00049 Frame(float duration, const sf::IntRect& subrect)
00050 : duration(duration)
00051 , subrect(subrect)
00052 , origin()
00053 , applyOrigin(false)
00054 {
00055 }
00056
00057 Frame(float duration, const sf::IntRect& subrect, sf::Vector2f origin)
00058 : duration(duration)
00059 , subrect(subrect)
00060 , origin(origin)
00061 , applyOrigin(true)
00062 {
00063 }
00064
00065 mutable float duration;
00066 sf::IntRect subrect;
00067 sf::Vector2f origin;
00068 bool applyOrigin;
00069 };
00070
00071 }
00072
00073
00074
00075
00078
00082 class THOR_API FrameAnimation
00083 {
00084
00085
00086 public:
00089 FrameAnimation();
00090
00094 void addFrame(float relativeDuration, const sf::IntRect& subrect);
00095
00100 void addFrame(float relativeDuration, const sf::IntRect& subrect, sf::Vector2f origin);
00101
00107 template <class Animated>
00108 void operator() (Animated& animated, float progress) const;
00109
00110
00111
00112
00113 private:
00114 void ensureNormalized() const;
00115
00116
00117
00118
00119 private:
00120 std::vector<detail::Frame> mFrames;
00121 mutable bool mNormalized;
00122 };
00123
00125
00126
00127
00128
00129 template <class Animated>
00130 void FrameAnimation::operator() (Animated& target, float progress) const
00131 {
00132 assert(!mFrames.empty());
00133 assert(progress >= 0.f && progress <= 1.f);
00134
00135 ensureNormalized();
00136 AURORA_FOREACH(const detail::Frame& frame, mFrames)
00137 {
00138 progress -= frame.duration;
00139
00140 if (progress < 0.f)
00141 {
00142 target.setTextureRect(frame.subrect);
00143 if (frame.applyOrigin)
00144 target.setOrigin(frame.origin);
00145
00146 break;
00147 }
00148 }
00149 }
00150
00151 }
00152
00153 #endif // THOR_FRAMEANIMATION_HPP