这可能是一个简单的问题,但我很难找到一个直接的答案:在SFML 2.0中有没有一种方法可以调整加载的纹理的大小?例如,如果我有一个3264x2448的png文件,并且我想将其缩小以适应900x1200的渲染窗口而不剪裁它,该怎么做?
这可能是一个简单的问题,但我很难找到一个直接的答案:在SFML 2.0中有没有一种方法可以调整加载的纹理的大小?例如,如果我有一个3264x2448的png文件,并且我想将其缩小以适应900x1200的渲染窗口而不剪裁它,该怎么做?
// 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);
是否有一种方法可以在SFML 2.0中调整加载的纹理大小?
不能直接使用sf::Texture
,但您可以使用sf::Sprite
:加载纹理,将其传递给sf::Sprite
,然后使用sf::Sprite::setScale
或sf::Sprite::scale
进行操作。