Resizing in SFML 2.0

user3113547 picture user3113547 · Jan 26, 2014 · Viewed 10.8k times · Source

This may be a simple question but I'm having a hard time finding a straight answer to it: is there a way to resize a loaded Texture in SFML 2.0? For instance, if I have a 3264x2448 png and I want to scale it down to fit a 900x1200 rendered window without cropping, how would I do so?

Answer

Emile Bergeron picture Emile Bergeron · Feb 4, 2014

Is there a way to scale all rendered windows to fit whatever monitor of whatever system the application is running on?

First, here's a way to scale the image to the current RenderWindow size.

// assuming the given dimension
// change this dynamically with the myRenderWindow->getView().getSize()
sf::Vector2f targetSize(900.0f, 1200.0f); 

yourSprite.setScale(
    targetSize.x / yourSprite.getLocalBounds().width, 
    targetSize.y / yourSprite.getLocalBounds().height);

Be aware that this may stretch your image if the aspect ratio is not maintained. You might want to add code to adjust the decision for your case.

Then, if you want to stretch the RenderWindow to fill all the screen, may I suggest you use fullscreen mode?

Here's a snippet of how it's done:

// add the flag to the other ones
mStyleFlag = sf::Style::Default | sf::Style::Fullscreen;

// get the video mode (which tells you the size and BPP information of the current display
std::vector<sf::VideoMode> VModes = sf::VideoMode::getFullscreenModes();

// then create (or automatically recreate) the RenderWindow
mMainWindow.create(VModes.at(0), "My window title", mStyleFlag);