AnimatedSprite.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
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     // Set the image
00062     SetImage(Image);
00063 
00064     // Clear the frame array
00065     myFrames.clear();
00066 
00067     // Compute the number of frames in each direction
00068     const unsigned int NbFramesX = SourceRect.GetWidth()  / FrameWidth;
00069     const unsigned int NbFramesY = SourceRect.GetHeight() / FrameHeight;
00070 
00071     // Extract the animation frames from the source image
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         // Insert frame at end
00092         myFrames.push_back(FrameRect);
00093     }
00094     else
00095     {
00096         // Insert frame at the specified position
00097         myFrames.insert(myFrames.begin() + InsertPos, FrameRect);
00098 
00099         // If the inserted frame is before the current one, increment it
00100         if (myCurrentFrame >= InsertPos)
00101         {
00102             myCurrentFrame++;
00103             SetSubRect(myFrames[myCurrentFrame]);
00104         }
00105     }
00106 
00107     // If it is the first frame, we must initialize the subrect
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     // Update the current frame
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     // Let the base class render the current frame
00149     sfSprite::Render(Window);
00150 }