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<SFML/Graphics.hpp>
#include<iostream>
#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";
}
playerImage.setTexture(Ptexture);
while (Window.isOpen()) {
    sf::Event Event;
    while (Window.pollEvent(Event))     {
            switch (Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;

            }     
                }
    Window.draw(playerImage);
    Window.display();
            }


        }   

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

share|improve this question
1  
Why did you roll back my edit? – Alexandre 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

share|improve this answer
    
thank you for your help – Gaming Royale Jul 12 '16 at 21:24
    
no problem, did this answer your question, or do you still need more help? – Andy M Jul 12 '16 at 21:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.