将 sf::Sprite 延伸到整个 window
Stretch sf::Sprite across entire window
我有一个 sf::Sprite,在将它绘制到 window 时,我希望它填满整个 window。
sf::RenderWindow.draw 接受一个可选的 sf::RenderStates。那是我需要搞砸的吗?
首先,basic Sprite usage 来自教程。
摘自我自己在 Resizing in SFML 2.0 上的回答,顺便说一句,这是搜索 "sfml sprite fill the screen".
时的第一个 google 结果
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);
如果您正在寻找背景,最优雅的方法可能是定义一个 sf::VertexArray,它将使用正确的纹理坐标渲染一个填充您的 window 的四边形.
这是取自第二个 google 结果:What happened to sprite::setSize in SFML2?
我有一个 sf::Sprite,在将它绘制到 window 时,我希望它填满整个 window。
sf::RenderWindow.draw 接受一个可选的 sf::RenderStates。那是我需要搞砸的吗?
首先,basic Sprite usage 来自教程。
摘自我自己在 Resizing in SFML 2.0 上的回答,顺便说一句,这是搜索 "sfml sprite fill the screen".
时的第一个 google 结果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);
如果您正在寻找背景,最优雅的方法可能是定义一个 sf::VertexArray,它将使用正确的纹理坐标渲染一个填充您的 window 的四边形.
这是取自第二个 google 结果:What happened to sprite::setSize in SFML2?