Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ SFML Spritesheet Animation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 aliasc   Members   -  Reputation: 180

Like
4Likes
Like

Posted 09 February 2014 - 11:03 PM

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.

 

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 !

Attached Files


Edited by aliasc, 09 February 2014 - 11:09 PM.


Sponsor:

#2 Dezachu   Members   -  Reputation: 399

Like
0Likes
Like

Posted 10 February 2014 - 04:48 AM

Nice tutorial buddy. Not a fan of your code layout (feels very cramped) but a nice tutorial nonetheless :) 


Studying BSc Computer Games Programming @ De Montfort University, Leicester.

Completed a placement as a Junior Programmer at Exient Ltd in Oxford/Valletta, Malta


#3 Rld_   Members   -  Reputation: 1515

Like
0Likes
Like

Posted 10 February 2014 - 04:57 AM

I'm sure it is meant to be helpfull and with all good intentions, but as stated in the FAQ, this forum is for beginner questions and not for posting tutorials and the like. Threads like this are likely to be closed.

 

It's probably better to post this as an article or something.

 

Good luck though!



#4 aliasc   Members   -  Reputation: 180

Like
0Likes
Like

Posted 10 February 2014 - 12:48 PM

I'm sure it is meant to be helpfull and with all good intentions, but as stated in the FAQ, this forum is for beginner questions and not for posting tutorials and the like. Threads like this are likely to be closed.

 

It's probably better to post this as an article or something.

 

Good luck though!

Ok sorry for that. But this is just basic wrapper class, and i don't have to make an article for it right, since articles need to be more completed and full written. Since this section is for beginners then so this tutorial is intended for beginners, and the class is to help beginners easily animate their sprites.
 

 

Nice tutorial buddy. Not a fan of your code layout (feels very cramped) but a nice tutorial nonetheless smile.png

Well it does the work right. Everyone has its own style of wrapping and writing code and this is mine. I feel more comfortable to write full
lines without using shortcuts so that i can totally understand what i did. And i like to have spaces and clear view between functions brackets etc.
Anyway thanks :) glad it helped

 



#5 Rld_   Members   -  Reputation: 1515

Like
1Likes
Like

Posted 10 February 2014 - 02:50 PM


Ok sorry for that. But this is just basic wrapper class, and i don't have to make an article for it right, since articles need to be more completed and full written. Since this section is for beginners then so this tutorial is intended for beginners, and the class is to help beginners easily animate their sprites.

 

Just mentioning it because I've seen it before (helpful topics that aren't really questions being locked) due to forum policy. Perhaps I'm over thinking it, but I just wanted to give you a heads up it might happen. :)

 

Perhaps an article is a tad to much, and making a post in your announcements is something you can do (in case it really gets locked). Either way, it was just a heads up. :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS