Ok so this actually turned out harder than I thought it would be. This is what I came up with so far:
#include <SFML/Graphics.hpp>
#include "AnimatedSprite.hpp"
int main()
{
sf::Vector2i screenDimensions(800,600);
sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Animations!");
// load texture
sf::Texture texture;
texture.loadFromFile("Player.png");
// push frames
Animation walkingAnimationDown;
walkingAnimationDown.setSpriteSheet(texture);
walkingAnimationDown.addFrame(sf::IntRect(0, 0, 32, 32));
walkingAnimationDown.addFrame(sf::IntRect(32, 0, 32, 32));
walkingAnimationDown.addFrame(sf::IntRect(64, 0, 32, 32));
walkingAnimationDown.addFrame(sf::IntRect(32, 0, 32, 32));
Animation walkingAnimationLeft;
walkingAnimationLeft.setSpriteSheet(texture);
walkingAnimationLeft.addFrame(sf::IntRect(0, 32, 32, 32));
walkingAnimationLeft.addFrame(sf::IntRect(32, 32, 32, 32));
walkingAnimationLeft.addFrame(sf::IntRect(64, 32, 32, 32));
walkingAnimationLeft.addFrame(sf::IntRect(32, 32, 32, 32));
Animation walkingAnimationRight;
walkingAnimationRight.setSpriteSheet(texture);
walkingAnimationRight.addFrame(sf::IntRect(0, 64, 32, 32));
walkingAnimationRight.addFrame(sf::IntRect(32, 64, 32, 32));
walkingAnimationRight.addFrame(sf::IntRect(64, 64, 32, 32));
walkingAnimationRight.addFrame(sf::IntRect(32, 64, 32, 32));
Animation walkingAnimationUp;
walkingAnimationUp.setSpriteSheet(texture);
walkingAnimationUp.addFrame(sf::IntRect(0, 96, 32, 32));
walkingAnimationUp.addFrame(sf::IntRect(32, 96, 32, 32));
walkingAnimationUp.addFrame(sf::IntRect(64, 96, 32, 32));
walkingAnimationUp.addFrame(sf::IntRect(32, 96, 32, 32));
// set up AnimatesSprite
AnimatedSprite animatedSprite(sf::seconds(0.2));
animatedSprite.setAnimation(walkingAnimationDown);
animatedSprite.pause();
animatedSprite.setPosition(0,0);
sf::Clock frameClock;
float speed = 0.01f;
bool bNoKeyWasPressed = true;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if(animatedSprite.getAnimation() != &walkingAnimationUp)
{
animatedSprite.setAnimation(walkingAnimationUp);
}
animatedSprite.play();
animatedSprite.move(0, -speed);
bNoKeyWasPressed = false;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if(animatedSprite.getAnimation() != &walkingAnimationDown)
{
animatedSprite.setAnimation(walkingAnimationDown);
}
animatedSprite.play();
animatedSprite.move(0, speed);
bNoKeyWasPressed = false;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if(animatedSprite.getAnimation() != &walkingAnimationRight)
{
animatedSprite.setAnimation(walkingAnimationRight);
}
animatedSprite.play();
animatedSprite.move(speed, 0);
bNoKeyWasPressed = false;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if(animatedSprite.getAnimation() != &walkingAnimationLeft)
{
animatedSprite.setAnimation(walkingAnimationLeft);
}
animatedSprite.play();
animatedSprite.move(-speed, 0);
bNoKeyWasPressed = false;
}
if(bNoKeyWasPressed)
{
animatedSprite.pause();
}
bNoKeyWasPressed = true;
// update AnimatedSprite
animatedSprite.update(frameClock.restart());
window.clear();
window.draw(animatedSprite);
window.display();
}
return 0;
}
Also AnimatedSprite has to be extended by the addition of this method
const Animation* AnimatedSprite::getAnimation() const
{
return m_animation;
}
And you have to delete the line
m_currentTime = sf::Time::Zero; in
AnimatedSprite::play() (I don't know why I put it there in the first place...)
It doesn't work for diagonal movement, but I don't have time to think of something clean and elegant right now. Also I'm not gonna be around a computer for the next 2 weeks, so I though I share this, so that somebody else can maybe improve it.