c – 为什么纹理仅出现在第一象限中
作者:互联网
使用SFML的代码有什么问题?
在下面的代码中,我有this image(1000×1000),我想使用sf :: RenderTexture在一个窗口(500×500)中显示它.
但是,只有部分图像出现在第一个象限中:
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window({500, 500}, "SFML Views", Style::Close);
View camera;
camera.setSize(Vector2f(window.getSize()));
Texture background;
background.loadFromFile("numeros.png");
Sprite numeros (background);
RenderTexture texture;
texture.create(window.getSize().x, window.getSize().y);
Sprite content;
content.setTexture(texture.getTexture());
texture.draw(numeros);
texture.display();
while (window.isOpen())
{
for (Event event; window.pollEvent(event);)
if (event.type == Event::Closed)
window.close();
window.clear();
window.setView(camera);
window.draw(content);
window.display();
}
return EXIT_SUCCESS;
}
据我所知,代码应生成自动调整为500×500的原始图像(1000×1000).
谁能告诉你什么是错的?
解决方法:
事实上,你面临着两个截然不同的问题:
第一:
As far as I can understand, the code should generate the original
image (1000×1000) automatically adjusted to 500×500.
这不是真的. SFML使用纹理的实际大小处理精灵.如果您的图像是1000×1000,但是您希望将其表示为500×500,则应将纹理指定给精灵,如下所示:
精灵数字(背景);
然后缩放此精灵以适应500×500窗口,这是:
numeros.setScale(0.5,0.5);
通过此更改,您应该查看整个图像,但是……
第二个:
你弄乱了窗户的景色.如果我们检查SFML documentation,我们可以看到sf :: View需要:
>一个sf :: FloatRect:这是一个坐标(x,y) – 在这种情况下是左上角 – 和一个尺寸(宽度,高度)
要么
>两个sf :: Vector2f:一个对应于中心的坐标,另一个对应于视图的大小.
假设您想要使用第二个参数,那么您缺少第一个参数,即中心坐标,但这不是必需的.如果您只是不应用视图,则图像应显示在整个窗口中.
所以你只需要删除:
window.setView(照相机);
我试过的代码:
int main()
{
RenderWindow window({ 500, 500 }, "SFML Views", Style::Close);
View camera;
camera.setSize(Vector2f(window.getSize()));
Texture background;
background.loadFromFile("numeros.png");
Sprite numeros(background);
numeros.setScale(0.5, 0.5); // <-- Add this
RenderTexture texture;
texture.create(window.getSize().x, window.getSize().y);
Sprite content;
content.setTexture(texture.getTexture());
texture.draw(numeros);
texture.display();
while (window.isOpen())
{
for (Event event; window.pollEvent(event);)
if (event.type == Event::Closed)
window.close();
window.clear();
//window.setView(camera); <-- Remove this
window.draw(content);
window.display();
}
return EXIT_SUCCESS;
}
而我的结果是: