Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] What's wrong with this code?  (Read 6323 times)

0 Members and 1 Guest are viewing this topic.

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
[SOLVED] What's wrong with this code?
« 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:



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;
}
« Last Edit: May 16, 2018, 11:18:45 pm by rogeriodec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: What's wrong with this code?
« Reply #1 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;
« Last Edit: May 14, 2018, 09:31:06 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: What's wrong with this code?
« Reply #2 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: What's wrong with this code?
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: What's wrong with this code?
« Reply #4 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.
Laurent Gomila - SFML developer

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: What's wrong with this code?
« Reply #5 on: May 14, 2018, 07:34:04 pm »
Also don't use using namespace sf;
Why?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: What's wrong with this code?
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: What's wrong with this code?
« Reply #7 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 ...




rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: What's wrong with this code?
« Reply #8 on: May 16, 2018, 02:49:37 am »
No answer? Should I think it's a bug? How to get around this?  ???

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: What's wrong with this code?
« Reply #9 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.
SFML / OS X developer

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: What's wrong with this code?
« Reply #10 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()));

 

anything