Hello GDnet. While studying and working on my small projects i created basic wrapper class for SFML to load and animate spritesheets.
I will post it here so beginners like me could find it helpful.
First of all this class implements Sprite class and Texture class, two standard SFML classes for loading and displaying sprites.
I implemented functions from these two classes in one single class which loads and animates sprites.
C_Sprite class loads and draws sprites.
Animation class animates spritesheets.
C_Sprite class inherits public data from Animation class.
Also the Animation class uses same code from this tutorial http://www.sdltutorials.com/sdl-animation.
Its not coded by me so i must give credit, although i dropped some unnecessary stuff and adopted it to work with SFML.
Include sprite.h in your project
Load spritesheets by creating new C_Sprite object like this
C_Sprite mySprite = new C_Sprite("pathtofile", positionX, positionY);
If your sprite contains multiple frames for animation you must set the StartFrame and TotalFrames before you draw like this :
First parameter is start frame where you want your animation to begin, and the second parameter is the total frames your spritesheet contains.
Use this function right after you load your spritesheet.
In your update logic before drawing your sprite use this function to animate:
And now to draw your animated sprite:
mySprite->DrawSpriteAnim(Window, mySprite->GetCurrentFrame(), 0, 32, 32);
The first parameter is the render window. The second parameter gets the current frame and uses it as x position to crop a rectangle.
The second parameter is the y position where you want to crop your rectangle.
If your spritesheets frames are horizontally aligned y will always be 0, and if vertically you set x to 0 and y to mySprite->GetCurrentFrame().
The last two parameters are the width and the height of the rectangle to crop.
If you have static sprites with no animation you dont have to set the animation frames with C_Sprite::SetAnim() function you just load the sprite :
C_Sprite background = new C_Sprite("background.png", 0.0f, 0.0f);
And to draw it use the DrawSprite() function.
Note that this class does not implement all of the functions from SFML Sprite and Texture classes.
If you want to further manipulate your sprite with these two classes you can use GetSprite() function to retrieve the address of the sprite.
For example if you want to move your sprite you can do it like this:
The setPosition() function is implemented in the default Sprite class of SFML.
The C_Sprite::GetSprite() function returns the address of the C_Sprite m_sprite member which is an object of the SFML class.
You can use this address to further manipulate your sprite with other SFML functions.
Also be aware that the Animation class uses its own timing to control the sprite animation speed.
So if you have implemented other algorithms for controlling frame rate before draw and update logic you may see lags or slower animation.
I know its not 100% robust and it can be improved much more, but i'm happy to see my sprites have a dance on screen Thanks !
Edited by aliasc, 09 February 2014 - 11:09 PM.