在SFML 2.0中调整大小

时间:2014-01-26 12:51:11

标签: c++ sfml

这可能是一个简单的问题,但我很难找到一个直接的答案:有没有办法在SFML 2.0中调整加载的纹理的大小?例如,如果我有一个3264x2448 png并且我想缩小它以适应900x1200渲染窗口而不进行裁剪,我该怎么做?

2 个答案:

答案 0 :(得分:5)

  

有没有办法缩放所有渲染窗口以适应任何监视器   应用程序运行的任何系统?

首先,这是一种将图像缩放到当前RenderWindow大小的方法。

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

请注意,如果不保持纵横比,可能会拉伸图像。您可能希望添加代码来调整案例的决策。

然后,如果您想拉伸RenderWindow以填满所有屏幕,我建议您使用全屏模式吗?

以下是它如何完成的片段:

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

答案 1 :(得分:4)

  

有没有办法在SFML 2.0中调整加载的纹理?

不是sf::Texture直接。但您可以使用sf::Sprite:加载纹理,将其传递给sf::Sprite,然后使用sf::Sprite::setScalesf::Sprite::scale