#includeint main() { sf::RenderWindow window(sf::VideoMode(200, 200), "Sprite Example"); // Create a texture from an image file sf::Texture texture; if (!texture.loadFromFile("sprite.png")) return EXIT_FAILURE; // Create a sprite from the texture sf::Sprite sprite(texture); // Main game loop while (window.isOpen()) { // Handle events sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } // Clear the window window.clear(); // Draw the sprite window.draw(sprite); // Display the window window.display(); } return EXIT_SUCCESS; }
#includeBoth examples use the SFML (Simple and Fast Multimedia Library) package library for window management and event handling. However, Cpp Sprite can be used with any other graphic package libraries that support C++.int main() { sf::RenderWindow window(sf::VideoMode(200, 200), "Sprite Example"); // Create a texture from a sprite sheet sf::Texture texture; if (!texture.loadFromFile("spritesheet.png")) return EXIT_FAILURE; // Calculate the size of each sprite in the sprite sheet sf::IntRect spriteRect(0, 0, 32, 32); // Create a sprite from the texture sf::Sprite sprite(texture, spriteRect); // Change the sprite's texture rectangle every frame to animate the sprite sf::Clock clock; while (window.isOpen()) { // Handle events sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } // Clear the window window.clear(); // Calculate the elapsed time since the last frame float deltaTime = clock.restart().asSeconds(); // Update the sprite's texture rectangle to animate the sprite spriteRect.left = (spriteRect.left + 32) % (texture.getSize().x - 32); sprite.setTextureRect(spriteRect); // Draw the sprite window.draw(sprite); // Display the window window.display(); } return EXIT_SUCCESS; }