将SF :: Sprite伸展整个窗户

Stretch sf::Sprite across entire window

本文关键字:窗户 SF Sprite      更新时间:2023-10-16

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

sf :: renderwindow.draw采用可选的SF :: Renderstates。那是我需要弄乱的吗?

首先,教程中的基本sprite用法。

从我自己的回答SFML 2.0中提取的答案,顺便说一句,这是搜索" SFML Sprite填充屏幕"时的第一个Google结果。

首先,这是一种将图像扩展到当前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);

请注意,如果纵横比不是 保持。您可能需要添加代码以调整您的决定 案例。

然后,如果要拉伸渲染窗以填充所有屏幕, 我可以建议您使用全屏模式吗?

这是完成方式的片段:

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

如果您正在寻找背景,那么最优雅的方法可能就是仅定义sf :: vertexarray,这将使用正确的纹理坐标填充窗口,从而使四边形填充窗口。

这是从第二个Google结果中获取的:Sprite :: Setsize在SFML2中发生了什么?