SFML community forums

Help => General => Topic started by: rogeriodec on May 14, 2018, 05:21:56 am

Title: [SOLVED] What's wrong with this code?
Post by: rogeriodec on May 14, 2018, 05:21:56 am
In the code below, I have the attached image (1000x1000) and I want to show it in a window (500x500) using RenderTexture.
However, only part of the image appears in the first quadrant:

(https://i.imgur.com/wJmKUrt.jpg)

What is wrong?

Code: [Select]
#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;
}
Title: Re: What's wrong with this code?
Post by: eXpl0it3r on May 14, 2018, 08:41:21 am
You're not calling clear on the render texture.
The view in its current form is not needed.
You should make sure that the texture rect for both sprites is correct.
Also don't use using namespace sf;
Title: Re: What's wrong with this code?
Post by: Flaze07 on May 14, 2018, 09:30:11 am
wait, isn't it better to cast the windowSize rather than creating an object ? I mean
camera.setSize( Vector2f{ window.getSize() } );
 
isn't this better ?
camera.setSize( static_cast< Vector2f >( window.getSize() );
 

although I don't know very well.... I hope someone would clear me on this
Title: Re: What's wrong with this code?
Post by: eXpl0it3r on May 14, 2018, 09:41:59 am
I'd claim the first one to be better design-wise, but I guess it really just depends on what you want to pick.

The sf::Vector2<T> can take a sf::Vector2<N> of a different type for the whole reason, so you can convert between different types of vectors.
Title: Re: What's wrong with this code?
Post by: Laurent on May 14, 2018, 10:25:02 am
Quote
wait, isn't it better to cast the windowSize rather than creating an object ?
Better for what? Design? Readability? Performances?

In the end it makes really no difference: static_cast works because the Vector2<T>(Vector2<U>) constructor exists, and it is what is called to perform the conversion in the end. So the static_cast is just an extra step, that is removed by the compiler anyway.
Title: Re: What's wrong with this code?
Post by: rogeriodec on May 14, 2018, 07:34:04 pm
Also don't use using namespace sf;
Why?
Title: Re: What's wrong with this code?
Post by: eXpl0it3r on May 14, 2018, 08:04:25 pm
Because the reason for namespaces is to not get name conflicts and it will make your code more readable, as everyone will immediately know that you're using the SFML Texture and not your own Texture class or similar.
Title: Re: What's wrong with this code?
Post by: rogeriodec on May 14, 2018, 08:07:21 pm
You're not calling clear on the render texture.

texture.clear() before texture.draw(numeros) in this case makes no difference.

Quote
The view in its current form is not needed.

I think I'll need more in my project, for example, zooming, moving, etc.

Quote
You should make sure that the texture rect for both sprites is correct.

How?

Anyway, I still do not understand why the image only appears in the first quadrant ...



Title: Re: What's wrong with this code?
Post by: rogeriodec on May 16, 2018, 02:49:37 am
No answer? Should I think it's a bug? How to get around this?  ???
Title: Re: What's wrong with this code?
Post by: Hiura on May 16, 2018, 05:19:26 pm
Set aside programming style, you can fix the bug in your program by changing two lines:

camera.setSize({ 1000, 1000 }); // and not the physical window size
texture.create(1000, 1000);

That's one way of doing things. You could also do without the render texture and simply draw the sprite. You can also do without the view and scale the sprite itself. Whatever makes more sense for your app.
Title: Re: What's wrong with this code?
Post by: rogeriodec on May 16, 2018, 11:18:21 pm
Thank you.
The problem was all at View initialization.
In your example it would be missing to move the center of the view to the upper right corner, but in fact its solution elucidated the problem.
To homologate the solution, i did  this snippet to fit any background size:

Code: [Select]
View camera;
camera.setSize(Vector2f(background.getSize().x, background.getSize().y));
camera.setCenter(Vector2f(window.getSize()));