SFML community forums

Help => Graphics => Topic started by: CodingNewbie on October 13, 2013, 07:28:29 am

Title: Sprite animation with inconsistant sprite sizes
Post by: CodingNewbie on October 13, 2013, 07:28:29 am
Hello everyone.  I'm currently trying to animate a sprite with SFML, but I'm running into an issue because all the guides I can find on it assume that every sprite is the same (16x16, 32x32, etc).  Mine are any 3 of these sizes:
 
 - 60x76
 - 56x76
 - 56x72

Any help with this would be greatly appreciated.  In case any of you are wondering, I attached the sprite sheet I'm using (making a Pallet Town clone from Pokemon Fire Red):



 
Title: Re: Sprite animation with inconsistant sprite sizes
Post by: Ixrec on October 13, 2013, 08:04:36 am
Why not just add some blank pixels to the pallette until every sprite is the same square size?  That's probably a lot easier than complicating your rendering code to deal with variable-sized sprites.

If you really want variable sizes then you'll just have to write the more complex logic yourself.  You have to define the texture rectangles for each sprite in the sheet anyway, so you should always have access to the current sprite's size and can compute the offsets you want from that.
Title: Re: Sprite animation with inconsistant sprite sizes
Post by: Jebbs on October 13, 2013, 08:07:47 am
Why not just add some blank pixels to the pallette until every sprite is the same square size?  That's probably a lot easier than complicating your rendering code to deal with variable-sized sprites.

I completely agree. Remaking your sprite sheet is going to be better/easier than an alternative, which would be hard coding the frames into your game, or maybe putting the frame data in a file and loading that at run-time.
Title: AW: Sprite animation with inconsistant sprite sizes
Post by: eXpl0it3r on October 13, 2013, 08:10:05 am
Edit: Double ninja-ed...

While it's possible to animate with different sizes, it will take you unessecary more programming and processing time, because you'll most likely have to create your own format that specifies how each frame has to be loaded.
It's on the other hand, far simpler to go and change the spritesheet once before runtime to use fixed sizes, thus allowing for an automated iteration over the texture.
Title: Re: Sprite animation with inconsistant sprite sizes
Post by: CodingNewbie on October 13, 2013, 02:20:40 pm
Sounds like a plan!  I wasn't sure how complex the logic would have to be.  Much appreciated gentlemen.
Title: Re: Sprite animation with inconsistant sprite sizes
Post by: Nexus on October 13, 2013, 02:28:56 pm
By the way, if you don't want to reinvent the wheel, take a look at my implementation in Thor.Animation (http://www.bromeon.ch/libraries/thor/v2.0/doc/group___animation.html) :)

// Define animation with two frames and their relative durations 1 and 2
thor::FrameAnimation animation;
animation.addFrame(1.f, sf::IntRect(...));
animation.addFrame(2.f, sf::IntRect(...));

// Either directly animate a sprite
sf::Sprite sprite;
animation(sprite, progress);

// Or use animator for convenience
thor::Animator<sf::Sprite, std::string> animator;
animator.addAnimation("walk", animation, sf::seconds(3.f));
animator.playAnimation("walk");
...
animator.update(frameTime);
animator.animate(sprite);

Having frames with different sizes is no problem, just specify it at thor::FrameAnimation.