Resizing in SFML 2.0

北城余情 提交于 2019-12-04 03:43:41

问题


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?


回答1:


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);



回答2:


is there a way to resize a loaded Texture in SFML 2.0?

Not an sf::Texture directly. But you can use an sf::Sprite: you load load your texture, you pass it to an sf::Sprite and you play with sf::Sprite::setScale or sf::Sprite::scale.



来源:https://stackoverflow.com/questions/21363557/resizing-in-sfml-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!