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(const sfImage* Img) :
00036 sfSprite (Img),
00037 myCurrentFrame(0),
00038 Timing (0.1f)
00039 {
00040
00041 }
00042
00043
00047 sfAnimatedSprite::sfAnimatedSprite(const sfImage* Img, const sfIntRect& SourceRect, unsigned int FrameWidth, unsigned int FrameHeight) :
00048 sfSprite (Img),
00049 myCurrentFrame(0),
00050 Timing (0.1f)
00051 {
00052 CreateAnimation(Img, SourceRect, FrameWidth, FrameHeight);
00053 }
00054
00055
00059 void sfAnimatedSprite::CreateAnimation(const sfImage* Img, const sfIntRect& SourceRect, unsigned int FrameWidth, unsigned int FrameHeight)
00060 {
00061 Image = Img;
00062
00063 if (Image)
00064 {
00065
00066 myFrames.clear();
00067
00068
00069 const unsigned int NbFramesX = SourceRect.GetWidth() / FrameWidth;
00070 const unsigned int NbFramesY = SourceRect.GetHeight() / FrameHeight;
00071
00072
00073 for (unsigned int i = 0; i < NbFramesX; ++i)
00074 for (unsigned int j = 0; j < NbFramesY; ++j)
00075 {
00076 unsigned int X = SourceRect.Left + i * FrameWidth;
00077 unsigned int Y = SourceRect.Top + j * FrameHeight;
00078 AddFrame(sfIntRect(X, Y, X + FrameWidth, Y + FrameHeight));
00079 }
00080 }
00081 }
00082
00083
00087 void sfAnimatedSprite::AddFrame(const sfIntRect& FrameRect, int Index)
00088 {
00089 unsigned int InsertPos = static_cast<unsigned int>(Index);
00090
00091 if (InsertPos > myFrames.size())
00092 {
00093
00094 myFrames.push_back(FrameRect);
00095 }
00096 else
00097 {
00098
00099 myFrames.insert(myFrames.begin() + InsertPos, FrameRect);
00100
00101
00102 if (myCurrentFrame >= InsertPos)
00103 {
00104 myCurrentFrame++;
00105 SubRect = myFrames[myCurrentFrame];
00106 }
00107 }
00108
00109
00110 if (myFrames.size() == 1)
00111 {
00112 myCurrentFrame = 0;
00113 SubRect = myFrames[myCurrentFrame];
00114 }
00115 }
00116
00117
00121 void sfAnimatedSprite::Render(sfRenderWindow& Window)
00122 {
00123
00124 unsigned int NbFrames = static_cast<unsigned int>(myTimer.GetElapsedTime() / Timing);
00125 if (NbFrames)
00126 {
00127 myCurrentFrame = (myCurrentFrame + NbFrames) % myFrames.size();
00128 SubRect = myFrames[myCurrentFrame];
00129 myTimer.Reset();
00130 }
00131
00132
00133 sfSprite::Render(Window);
00134 }