00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
00028 #include <SFML/AdvancedGraphics/AnimatedSprite.hpp>
00029 #include <SFML/Graphics/Image.hpp>
00030
00031
00035 sfAnimatedSprite::sfAnimatedSprite() :
00036 sfSprite (),
00037 myTiming (0.1f),
00038 myCurrentFrame(0)
00039 {
00040
00041 }
00042
00043
00047 sfAnimatedSprite::sfAnimatedSprite(const sfImage& Image, const sfIntRect& SourceRect, unsigned int FrameWidth, unsigned int FrameHeight) :
00048 sfSprite (Image),
00049 myTiming (0.1f),
00050 myCurrentFrame(0)
00051 {
00052 CreateAnimation(Image, SourceRect, FrameWidth, FrameHeight);
00053 }
00054
00055
00059 void sfAnimatedSprite::CreateAnimation(const sfImage& Image, const sfIntRect& SourceRect, unsigned int FrameWidth, unsigned int FrameHeight)
00060 {
00061
00062 SetImage(Image);
00063
00064
00065 myFrames.clear();
00066
00067
00068 const unsigned int NbFramesX = SourceRect.GetWidth() / FrameWidth;
00069 const unsigned int NbFramesY = SourceRect.GetHeight() / FrameHeight;
00070
00071
00072 for (unsigned int i = 0; i < NbFramesX; ++i)
00073 for (unsigned int j = 0; j < NbFramesY; ++j)
00074 {
00075 unsigned int X = SourceRect.Left + i * FrameWidth;
00076 unsigned int Y = SourceRect.Top + j * FrameHeight;
00077 AddFrame(sfIntRect(X, Y, X + FrameWidth, Y + FrameHeight));
00078 }
00079 }
00080
00081
00085 void sfAnimatedSprite::AddFrame(const sfIntRect& FrameRect, int Index)
00086 {
00087 unsigned int InsertPos = static_cast<unsigned int>(Index);
00088
00089 if (InsertPos > myFrames.size())
00090 {
00091
00092 myFrames.push_back(FrameRect);
00093 }
00094 else
00095 {
00096
00097 myFrames.insert(myFrames.begin() + InsertPos, FrameRect);
00098
00099
00100 if (myCurrentFrame >= InsertPos)
00101 {
00102 myCurrentFrame++;
00103 SetSubRect(myFrames[myCurrentFrame]);
00104 }
00105 }
00106
00107
00108 if (myFrames.size() == 1)
00109 {
00110 myCurrentFrame = 0;
00111 SetSubRect(myFrames[myCurrentFrame]);
00112 }
00113 }
00114
00115
00119 void sfAnimatedSprite::SetTiming(float Timing)
00120 {
00121 myTiming = Timing;
00122 }
00123
00124
00128 float sfAnimatedSprite::GetTiming() const
00129 {
00130 return myTiming;
00131 }
00132
00133
00137 void sfAnimatedSprite::Render(sfRenderWindow& Window)
00138 {
00139
00140 unsigned int NbFrames = static_cast<unsigned int>(myTimer.GetElapsedTime() / myTiming);
00141 if (NbFrames)
00142 {
00143 myCurrentFrame = (myCurrentFrame + NbFrames) % myFrames.size();
00144 SetSubRect(myFrames[myCurrentFrame]);
00145 myTimer.Reset();
00146 }
00147
00148
00149 sfSprite::Render(Window);
00150 }