I was wondering how do animation using a sprite sheet. I have a sprite sheet and the size is height:400 width:601

The code I am using is:

#include <string>  
int main() {

sf::RenderWindow Window;
Window.create(sf::VideoMode(980, 760), "Game Engine");  
sf::Texture Ptexture;
sf::Sprite playerImage;
if (!Ptexture.loadFromFile("spritesheetIdle.png")) { 
    std::cout << "error file not found\n";
while (Window.isOpen()) {
    sf::Event Event;
    while (Window.pollEvent(Event))     {
            switch (Event.type)
            case sf::Event::Closed:



If you could then can you please try to explain me about sf:IntRect() too?

  • 1
    \$\begingroup\$ Why did you roll back my edit? \$\endgroup\$
    – Vaillancourt
    Jul 12 '16 at 16:23

First off, I would suggest using power of two dimensions for your texture, it's not required, but more efficient.

sf::Sprite has a function "void setTextureRect(const sf::IntRect& rectangle)" which you can use to specify a sub-rectangle of the texture that sprite will display.

sf::IntRect has a few options to be created, generally it wants the coordinates for the top left corner and the width and height - IntRect is a typedef for sf::Rect<int>.

So if the first frame of your animation starts at 0,0 and your sprite is 64px by 32px, you could write

playerImage.setTextureRect(sf::IntRect(0, 0, 64, 32));

Between your event pulling and rendering, you could have "update" logic which changes the texture based on whatever conditions you'd like.

*Edit: fixed code segments. Also - SFML has great documentation, check it out: http://www.sfml-dev.org/documentation/2.0/classsf_1_1Sprite.php http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .