Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Animated Sprite class  (Read 22407 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Animated Sprite class
« on: January 13, 2013, 04:03:26 pm »
Hello everybody!
I wrote a class that provides an easy interface to Animations and follows the design of sf::Spite. I find it very helpful, so I though I share it with everybody! Go check it out and tell me what you think! Any input is appreciated.
Here is the link to the Wiki page.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9994
    • View Profile
    • development blog
    • Email
Re: Animated Sprite class
« Reply #1 on: January 13, 2013, 04:20:33 pm »
Seems quite nice, though not as flexible as Thor's animation class, but for a simple animation it's straight forward. :)

Wouldn't it make more sense to call the function addFrame instead of pushBackFrame or if you want to go with push back, then why not simply push_back?

Could please include a proper license? Because
Quote
You can use it in any way you want. Just a little note that the class was written by me would be nice :)
doesn't legally hold up. It also doesn't exactly specify, if the mentioning is required or just wished.
If you don't specify any license it will automatically fall into the public domain as stated by the wiki rules. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #2 on: January 13, 2013, 05:35:22 pm »
Of course if you already use thor, than this will not be a replacement, because thor offers stuff like fading etc. But if you only need frame-based animation and don't want to include a big library than this class might be the way to go. Plus I designed my class to have a simple interface similar to sf::Sprite, so it's perfect for beginners. There where a bunch of different source codes on the old wiki on how to do animations (all using 1.6), but there is non on the new wiki using SFML 2.0 (with a clean interface).

I renamed pushBackFrame to addFrame, because it makes a lot more sense :D Thanks for the tip.

I'll think about what license to use. Probably zlib/png, but I'm not quiet sure yet.

I would love to see if somebody took a look at the code and checked if I got all the const's correct and if I don't do unnecessary copies. I tried, but I'm not sure if I got everything right. I know there are people here who love that stuff ;)

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #3 on: January 13, 2013, 06:09:54 pm »
Hmm I just noticed something weird...
If I rotate (both rotate() or setRotation()) the AnimatedSprite by 45 or -45 degrees the texture coordinates are not calculated correctly. The first and third frame (same one) are calculated correctly, but the second frame is offset one to the left and the fourth is offset on to the right. This only happens with 45 or multiples of it (405,...) and -45 or multiples of it.

Does anybody have an idea, why this could happen? The sf::Transform should not affect the texture coordinates, should it? It only affects the vertices, right?

edit: I just noticed that sf::Sprite has the same problem.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6268
  • Thor Developer
    • View Profile
    • Bromeon
Re: Animated Sprite class
« Reply #4 on: January 14, 2013, 08:34:49 pm »
The class looks primarily good, I like that you use RAII and don't inherit from sf::Sprite ;)
Still some advice, as requested:

Make your code const-correct. Top-level CV qualifiers for parameters like const std::size_t n are useless, as explained here.
std::size_t getSize();
// -->
std::size_t getSize() const;

sf::IntRect& getFrame(const std::size_t n);
// ->
sf::IntRect getFrame(std::size_t n) const; // or
const sf::IntRect& getFrame(std::size_t n) const;
And if the animation isn't modified in the animated sprite, you can make the pointers and references to it const, too.

AnimatedSprite's constructor should be explicit, and you should initialize everything in its initializer list. Also, it's not exactly clear in which cases you cast int to float... Basically, there exists an implicit conversion, but to make it expressive in the code, you can (but don't have to) use static_cast.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #5 on: January 16, 2013, 10:07:01 am »
First of all thank you very much for the post! I knew I would miss something :D I made the functions you named const correct  (include the animation in animated sprite, since it's not modified) and initialized everything in the initializer list. I also added the setFrame() function, that allows you to jump to a certain frame in the animation, which solves a bug, I noticed (when you first set the animation, the frame isn't set right away... anyway that's fixed).
I know there is an implicit conversion for variables. I took those parts from sf::Sprite's class. I guess it's more noticeable that a conversion is taking place this way.
I don't quiet understand why AnimatedSprite's constructor should be explicit though. Could you explain this to me?
Thanks again for the helpful post :)

edit: i also added a proper license (zlib/libpng)
« Last Edit: January 16, 2013, 10:16:48 am by Foaly »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6268
  • Thor Developer
    • View Profile
    • Bromeon
Re: Animated Sprite class
« Reply #6 on: January 16, 2013, 10:17:24 am »
The constructor is currently declared as follows. It can be called with 0, 1, 2, or 3 arguments.
AnimatedSprite(sf::Time frameTime = sf::seconds(0.2), bool paused = false, bool looped = true);

Every constructor that can be invoked with 1 argument provides an implicit conversion of the argument to the object. In your example, the following code works:
sf::Time time = sf::seconds(5);
AnimatedSprite sprite = time;

This is certainly not intended, and a potential source of errors. By using the explicit keyword, you make the conversion explicit. The above code won't compile anymore, instead you have to use the constructor syntax:
sf::Time time = sf::seconds(5);
AnimatedSprite sprite(time);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #7 on: January 16, 2013, 10:23:38 am »
Totally makes sense! I'll add it. Thanks for the explanation

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Animated Sprite class
« Reply #8 on: January 22, 2013, 09:22:43 pm »
Very good work. It is great and extremely easy to use and is enough for most cases.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #9 on: January 22, 2013, 09:37:34 pm »
Well thank you very much! I'm glad to hear that my class is useful to somebody!
I'd love to hear about every project that uses my class.
And of course if you find something to improve let me know! (because the real bugs usually show up, when the code is used extensively) :)

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #10 on: August 31, 2013, 05:44:03 pm »
Hello everybody!
Like I said, the real bugs only show up, if the class is in use for some time.
I had some time today and looked at the code again and noticed a few errors.
I updated the code on the wiki to fix these errors. Here is a list of what changed:
  • Fixed compiler warnings due to implicit casting
  • Implemented method I declared, but forgot to implement
  • Added a method to get the current animation
  • Fixed an error in the play() and stop() method
  • Fixed a calculation error in the update() method when reseting the time (not taking the remainder into account)
I recommend everybody that uses the class to update their code, especially because of the last two changes.
If somebody finds another error or has some other improvement, feel free to post a message!
Have fun animating! :)

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Animated Sprite class
« Reply #11 on: September 11, 2013, 07:04:18 pm »
Hi,

Only left a link to download it inside a ZIP with the version, like: AnimatedSpriteClass-012.zip.

Thanks.

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Animated Sprite class
« Reply #12 on: December 01, 2013, 12:37:23 pm »
using this for some experimenting, seems simple and easy enough to grab a hold of in a few minutes. What would be nice though, is to be able to set the frame to one fixed frame and then be able to for example only animate frames 2-6 and return pack to frame 1 when done, or something like that.
« Last Edit: December 01, 2013, 01:18:16 pm by makerimages »
Makerimages-It`s in the pixel

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Animated Sprite class
« Reply #13 on: December 01, 2013, 07:33:04 pm »
I don't really understand what you mean.
The AnimatedSprite class always uses fram 0 as the "fixed/default frame", that it returns to after the animation is done and not looped. If you only want to play a subset of an Animation, simply split it up in two Animations. If you meant neither of those thing, please explain what you mean more clearly.

DemonRax

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Animated Sprite class
« Reply #14 on: December 27, 2013, 08:54:37 pm »
Hey Foaly.

Thanks for the class! Helped me a lot as a beginner :)

There's a mystype if AnimatedSprite.cpp here, should be with newFrame,:
void AnimatedSprite::setFrame(std::size_t newFrame, bool resetTime)
{
...
sf::IntRect rect = m_animation->getFrame(newFrame);
...
}

I also attached modified version with backwards loop added and the formentioned piece fixed, if you're interested. And I also changed it so it stops at the last frame, because I thought this is correct way. Don't know for sure though.