为什么纹理只出现在第一象限
Why the texture appears only in the first quadrant
这段代码使用 SFML 有什么问题?
在下面的代码中,我有 this image (1000x1000),我想使用 sf::RenderTexture 在 window (500x500) 中显示它。
但是,只有部分图像出现在第一象限:
#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;
}
据我了解,代码应该生成自动调整为 500x500 的原始图像 (1000x1000)。
谁能告诉你哪里出了问题?
事实上,您面临着两个截然不同的问题:
第一个:
As far as I can understand, the code should generate the original
image (1000x1000) automatically adjusted to 500x500.
这不是真的。 SFML 以纹理的真实大小处理 sprites。如果你的图像是 1000x1000,但你想将其表示为 500x500,你应该将纹理分配给精灵,就像你做的那样:
Sprite numeros(background);
然后 缩放 这个精灵以适应 500x500 window,这是:
numeros.setScale(0.5, 0.5);
进行此更改后,您应该查看整个图像,但是...
第二个:
你在扰乱 window 的视图。如果我们检查 SFML documentation,我们可以看到 sf::View
期望:
- A
sf::FloatRect
:这是坐标 (x,y) - 在本例中为左上角 - 和大小(宽度,高度)
或
- 两个
sf::Vector2f
:一个对应中心坐标,一个对应view的大小
假设你想使用第二个,你错过了第一个参数,中心坐标,但这并不是真正必要的。如果您只是 不应用视图 ,图像应该显示在整个 window.
因此您只需删除:
window.setView(camera);
我试过的代码:
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;
}
我的结果:
只是为了给@alseether 的出色响应添加另一个选项,我意识到整个问题都由错误的视图初始化组成。
这样您还可以将视图的大小设置为背景图像的大小 (1000,1000),最后将视图的中心设置为 windows的左上角。
由于视图大于 window 大小 (500,500),它将自动调整为这个新大小。
简而言之,要更改的部分是:
View camera;
camera.setSize(Vector2f(background.getSize().x, background.getSize().y));
camera.setCenter(Vector2f(window.getSize()));
这段代码使用 SFML 有什么问题?
在下面的代码中,我有 this image (1000x1000),我想使用 sf::RenderTexture 在 window (500x500) 中显示它。 但是,只有部分图像出现在第一象限:
#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;
}
据我了解,代码应该生成自动调整为 500x500 的原始图像 (1000x1000)。
谁能告诉你哪里出了问题?
事实上,您面临着两个截然不同的问题:
第一个:
As far as I can understand, the code should generate the original image (1000x1000) automatically adjusted to 500x500.
这不是真的。 SFML 以纹理的真实大小处理 sprites。如果你的图像是 1000x1000,但你想将其表示为 500x500,你应该将纹理分配给精灵,就像你做的那样:
Sprite numeros(background);
然后 缩放 这个精灵以适应 500x500 window,这是:
numeros.setScale(0.5, 0.5);
进行此更改后,您应该查看整个图像,但是...
第二个:
你在扰乱 window 的视图。如果我们检查 SFML documentation,我们可以看到 sf::View
期望:
- A
sf::FloatRect
:这是坐标 (x,y) - 在本例中为左上角 - 和大小(宽度,高度)
或
- 两个
sf::Vector2f
:一个对应中心坐标,一个对应view的大小
假设你想使用第二个,你错过了第一个参数,中心坐标,但这并不是真正必要的。如果您只是 不应用视图 ,图像应该显示在整个 window.
因此您只需删除:
window.setView(camera);
我试过的代码:
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;
}
我的结果:
只是为了给@alseether 的出色响应添加另一个选项,我意识到整个问题都由错误的视图初始化组成。
这样您还可以将视图的大小设置为背景图像的大小 (1000,1000),最后将视图的中心设置为 windows的左上角。
由于视图大于 window 大小 (500,500),它将自动调整为这个新大小。
简而言之,要更改的部分是:
View camera;
camera.setSize(Vector2f(background.getSize().x, background.getSize().y));
camera.setCenter(Vector2f(window.getSize()));