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

Author Topic: Proper method of doing animation  (Read 2611 times)

0 Members and 1 Guest are viewing this topic.

Mossar

  • Newbie
  • *
  • Posts: 15
    • View Profile
Proper method of doing animation
« on: March 04, 2013, 12:44:30 pm »
I'm new in using SFML and I've just done algorithm for movement of my character in my game. Everything works on my computer but I'm not sure is it a good way of doing animation with SFML. It looks like that

                sf::Time time= timer.getElapsedTime();
       
                if(time < t1) {  s = 0; sprajt.setTexture(leftleg); }
                if (time > t1 && time < t2){    if (s == 0){  sprajt.move(30,0); s = 1; } sprajt.setTexture(stand); }
                if (time > t2 && time < t3) {   s = 0; sprajt.setTexture(rightleg); }
                if (time > t3 && time < t4){    if ( s== 0){  sprajt.move(30,0); s = 1; } sprajt.setTexture(stand);  }
                if (time > t4) timer.restart();
        }
 

Firsteval it is changing texture to "left leg texture", after t1 it is moved by 30 pixels and texture is changed to "standing texture", etc.

I'm curious is there (in SFML) better way of doing it? For example with framerate?

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Proper method of doing animation
« Reply #1 on: March 04, 2013, 12:56:20 pm »
I recently wrote a class for animation. The source code is over at the wiki. You can either use the code as inspiration or use it directly (the license is the same as SFML's).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Proper method of doing animation
« Reply #2 on: March 04, 2013, 02:43:18 pm »
In case you use Thor, you could also take a look at the Animation module.

You can use it as follows (see here for a more complex example):
// Create animation, where each frame has specified duration and texture rect
thor::FrameAnimation animation;
animation.addFrame(sf::seconds(0.2f), sf::IntRect(...));
animation.addFrame(sf::seconds(0.1f), sf::IntRect(...));

// Create object that stores multiple animations and accesses them by string
thor::Animator<sf::Sprite, std::string> animator;
animator.addAnimation("anim", animation, sf::seconds(1.f));
animator.playAnimation("anim");

// Game loop: Update animator according to passed time, animate sprite
sf::Time frameTime = ...;
animator.update(frameTime);
animator.animate(sprite);

The animations are not limited to texture rects, also colorizing and fading animations are provided. And you can define your own animations easily.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: