#includeint main() { sf::RenderWindow window(sf::VideoMode(200, 200), "Get Local Bounds Example"); sf::Texture texture; if (!texture.loadFromFile("sprite.png")) return EXIT_FAILURE; sf::Sprite sprite(texture); sprite.setPosition(window.getSize().x/2, window.getSize().y/2); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } //get the local bounds of the sprite sf::FloatRect bounds = sprite.getLocalBounds(); //create a rectangle shape with the sprite's boundaries sf::RectangleShape outline(sf::Vector2f(bounds.width, bounds.height)); outline.setPosition(sprite.getPosition().x - bounds.width/2, sprite.getPosition().y - bounds.height/2); outline.setFillColor(sf::Color::Transparent); outline.setOutlineThickness(2.f); outline.setOutlineColor(sf::Color::Red); window.clear(sf::Color::White); window.draw(sprite); window.draw(outline); window.display(); } return EXIT_SUCCESS; }
#includeThis example loads a sprite image and scales it down to fit within a 100x100 rectangle. getLocalBounds() is used to obtain the original boundaries of the sprite, which are used to calculate the scale factor needed to fit the sprite within the desired rectangle. Package Library: SFML (Simple and Fast Multimedia Library)int main() { sf::RenderWindow window(sf::VideoMode(200, 200), "Resize Sprite Example"); sf::Texture texture; if (!texture.loadFromFile("sprite.png")) return EXIT_FAILURE; sf::Sprite sprite(texture); //resize the sprite to fit within its local bounds sf::FloatRect bounds = sprite.getLocalBounds(); float scaleFactor = std::min(100.f / bounds.width, 100.f / bounds.height); sprite.setScale(scaleFactor, scaleFactor); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(sf::Color::White); window.draw(sprite); window.display(); } return EXIT_SUCCESS; }