在整个窗口中拉伸 sf::Sprite

Bra*_*ean 3 c++ sfml

我有一个 sf::Sprite,当将它绘制到窗口时,我希望它填满整个窗口。

sf::RenderWindow.draw 采用可选的 sf::RenderStates。这是我需要搞砸的吗?

Emi*_*ron 5

首先,教程中的基本 Sprite 用法

取自我自己关于在 SFML 2.0调整大小的答案,顺便说一下,这是搜索“sfml sprite fill the screen”时的第一个谷歌结果。

首先,这是一种将图像缩放到当前 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);
Run Code Online (Sandbox Code Playgroud)

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

那么,如果你想拉伸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);
Run Code Online (Sandbox Code Playgroud)

如果您正在寻找背景,最优雅的方法可能是定义一个 sf::VertexArray ,它将使用正确的纹理坐标渲染填充您的窗口的四边形。

这是从第二个谷歌结果中获取的:SFML2 中的 sprite::setSize 发生了什么?