gpt4 book ai didi

c++ - 在整个窗口中拉伸(stretch) sf::Sprite

转载 作者:行者123 更新时间:2023-12-01 11:33:18 24 4
gpt4 key购买 nike

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

sf::RenderWindow.draw 采用可选的 sf::RenderStates。这是我需要解决的问题吗?

最佳答案

首先,basic Sprite usage来自教程。

摘 self 自己在 Resizing in SFML 2.0 上的回答顺便说一下,这是搜索“sfml sprite fill the screen”时的第一个谷歌结果。

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,它将使用正确的纹理坐标渲染一个填充窗口的四边形。

这取自第二个谷歌结果:What happened to sprite::setSize in SFML2?

关于c++ - 在整个窗口中拉伸(stretch) sf::Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30203708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com