#includeint main() { sf::RenderWindow window(sf::VideoMode(800, 600), "My Window"); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(sf::Color::White); // Draw graphics here... window.display(); } return 0; }
#includeThis code loads a sprite image from a file called `sprite.png`, creates a `Sprite` object using this texture, and sets its position at the center of the window. It also defines a speed variable for the sprite movement. The while loop is similar to the first example, except it uses `sprite.move()` to change the sprite's position and checks if the sprite has gone beyond the screen's width, in which case it resets the x position to the left. The sprite is drawn on the screen using `window.draw()`. Package/Library: SFML (Simple and Fast Multimedia Library).int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "My Window"); sf::Texture texture; if (!texture.loadFromFile("sprite.png")) return -1; sf::Sprite sprite(texture); sprite.setPosition(sf::Vector2f(400, 300)); float speed = 0.1f; while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } sprite.move(sf::Vector2f(speed, 0)); if (sprite.getPosition().x > 800) sprite.setPosition(sf::Vector2f(0 - sprite.getGlobalBounds().width, 300)); window.clear(sf::Color::White); window.draw(sprite); window.display(); } return 0; }