sf::Sprite is a class in SFML (Simple and Fast Multimedia Library) C++ library used to display 2D textures and images on the screen. The setPosition function in Sf::Sprite is used to set the position of the sprite in the window.
Code Examples:
Example 1:
sf::Window window(sf::VideoMode(800, 600), "Window"); sf::Texture texture; texture.loadFromFile("image.png"); sf::Sprite sprite; sprite.setTexture(texture); sprite.setPosition(100, 100); // sets the position of the sprite to (100,100) while (window.isOpen()) { //draw the sprite window.clear(); window.draw(sprite); window.display(); }
Example 2:
sf::RenderWindow window(sf::VideoMode(800, 600), "My Window"); sf::Texture texture; texture.loadFromFile("image.png"); sf::Sprite sprite; sprite.setTexture(texture); sprite.setPosition(sf::Vector2f(200.0f, 150.0f)); // sets the position of the sprite to (200,150) while (window.isOpen()) { //draw the sprite window.clear(); window.draw(sprite); window.display(); }
In both examples, we first create a sprite object and set its texture using the function setTexture(). We then use the setPosition() function to set the position of the sprite in the window. The first example uses two integers to set the position whereas the second example uses the sf::Vector2f method to define the position.
Package/Library: SFML (Simple and Fast Multimedia Library)
C++ (Cpp) Sprite::setPosition - 30 examples found. These are the top rated real world C++ (Cpp) examples of sf::Sprite::setPosition extracted from open source projects. You can rate examples to help us improve the quality of examples.