sf::Texture texture; texture.loadFromFile("image.png"); sf::Sprite sprite(texture); sprite.setTextureRect(sf::IntRect(0, 0, 32, 32)); // display first 32x32 pixels of image window.draw(sprite);
sf::Texture texture; texture.loadFromFile("animation.png"); sf::Sprite sprite(texture); sprite.setTextureRect(sf::IntRect(0, 0, 32, 32)); // display first frame of animation while (window.isOpen()) { // update sprite's texture rect for next frame sprite.setTextureRect(sf::IntRect(frame * 32, 0, 32, 32)); // increment x position by 32 pixels each frame window.clear(); window.draw(sprite); window.display(); // increment frame counter frame++; }This code loads an image file containing an animation, creates a sprite from the texture, and sets its texture rect to display the first frame of the animation. The code then enters a loop that updates the sprite's texture rect for each frame of the animation, and draws it to the window. The package library used in these examples is SFML.