D:/Programmation/Cpp/SFML/src/SFML/AdvancedGraphics/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(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         // Clear frames array
00066         myFrames.clear();
00067 
00068         // Compute number of frames
00069         const unsigned int NbFramesX = SourceRect.GetWidth()  / FrameWidth;
00070         const unsigned int NbFramesY = SourceRect.GetHeight() / FrameHeight;
00071 
00072         // Extract animation frames from source image
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         // Insert frame at end
00094         myFrames.push_back(FrameRect);
00095     }
00096     else
00097     {
00098         // Insert frame at specified position
00099         myFrames.insert(myFrames.begin() + InsertPos, FrameRect);
00100 
00101         // Is inserted frame is before current frame, increment it
00102         if (myCurrentFrame >= InsertPos)
00103         {
00104             myCurrentFrame++;
00105             SubRect = myFrames[myCurrentFrame];
00106         }
00107     }
00108 
00109     // If first frame, initialize the subrect
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     // Update current frame
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     // Let base class render the current frame
00133     sfSprite::Render(Window);
00134 }

Generated for SFML by  doxygen 1.5.2