Web Design,Programming,Mobile,iOS/Android,GameDev,IT Businness,eCommerce,Social Media

Advertisement

Monday 10 February 2014

C++ SFML 2.0 Spritesheet Animation Class

16:34

Loading and displaying sprites with SFML 2.0 is not hard work to do, its just two functions however when it comes to animating them you will need to actually delve a little bit more deeper. I've created a basic wrapper class for SFML 2.0 which loads displays and animates spritesheets with just few lines of code.

Beginners who are finding trouble with animating their sprites could find this 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.

Usage:

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 :

mySprite->SetAnim(0, 11);

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:

mySprite->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.

background->DrawSprite(Window); 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:

mySprite->GetSprite()->setPosition(30.0f, 0.0f);

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 smile.png  Thanks !

You can download the source files from here  https://drive.google.com/file/d/0B3fy5QHeNYYRRHlmNmxiR3BZTGs/edit?usp=sharing

Written by

Thank you for taking your time to browse through our blog. We try to build as much rich content base as we can. We focus in modern technologies, computer science design and business.

0 comments :

Post a Comment

 

© 2013 smartnoob . All rights resevered. Designed by Templateism