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

Author Topic: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear  (Read 5924 times)

0 Members and 1 Guest are viewing this topic.

garthos

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Hello again,

I have replicated my problem below.
I have tried using both getLocalBounds and getGlobalBounds to no avail.
As you can see the stray pixel appears above the G in Game Over. (Screen shot attached.)
If I remove setOrigin line the stray pixel is removed.

I understand getGlobalBounds is using a rectangle and my text is well text.
If this is the problem can you suggest what method I should be using to find the centre of my Text?

Many thanks in advance.

int main()
{
        //Game::Start();

     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
     sf::Font font;
     if (!font.loadFromFile("images/Arial.ttf"))
         return EXIT_FAILURE;

    sf::Text Text ( "Game Over", font, 60);

    //Text.setOrigin( Text.getLocalBounds().width /2 , Text.getLocalBounds().height /2 );
        Text.setOrigin( (Text.getGlobalBounds().width /2) , (Text.getGlobalBounds().height /2 ));
        Text.setPosition(400,300);

     while (window.isOpen())
     {
        window.draw(Text);
        window.display();
     }
    return 0;
}
 



[attachment deleted by admin]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear
« Reply #1 on: November 29, 2012, 08:06:47 am »
First, you must use getLocalBounds because the origin is in local coordinates.

Then, try to round coordinates (here: the origin) when you have rendering artifacts.
Laurent Gomila - SFML developer

garthos

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Thank you. That works a treat.

Text.setOrigin( floor(Text.getLocalBounds().width /2) , floor(Text.getLocalBounds().height /2 ));

But I do have to ask why? just for my own sanity.

My problem was caused by using a decimal place in setOrigin(155,23.5);
setOrigin accepts two float parameters, and I understood the float type caters for decimal places.
What am I missing here? I am confused.

Lastly, what do you mean by "artifacts". Does that mean sf::Text only or does this apply to sprites and shapes as well ?



 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear
« Reply #3 on: November 29, 2012, 09:25:11 pm »
Quote
But I do have to ask why? just for my own sanity.
Because of rasterization issues. The drawing space uses floating point coordinates, but at the and of the rendering chain there are pixels, which have integer coordinates. If you don't round coordinates yourself, the rasterizer may produce unwanted results when a texel is between two pixels, especially when linear filtering (smoothing) is enabled.

Quote
Lastly, what do you mean by "artifacts". Does that mean sf::Text only or does this apply to sprites and shapes as well ?
It applies to everything.
Laurent Gomila - SFML developer

garthos

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear
« Reply #4 on: November 29, 2012, 10:31:16 pm »
Thanks again, sanity back in check ;)

Interesting, I'm guessing rasterization issues can exist in other (rendering) methods and not specifically to setOrigin. Obviously I need to take greater care when using floats.

Without having a complete grasp of which methods these are, whats the rule of thumb here ?
Do I need to identify these methods in SFML one by one or is there a better approach ?

edit.
Actually, I am the one creating the decimal point by dividing height by 2. SFML rendering floats are whole numbers? So I need not to worry whether my floats are a decimal unless I have specifically modified it. Would that be correct?

« Last Edit: November 29, 2012, 10:49:53 pm by garthos »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear
« Reply #5 on: November 29, 2012, 10:37:18 pm »
What's important is the final position of the entity. So translation, rotation, scale and origin can all produce artifacts. Views too.
Laurent Gomila - SFML developer

garthos

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear
« Reply #6 on: November 30, 2012, 10:36:31 am »
Can you confirm whether move should also use whole numbers?
For example I am currently doing this. If I understood this correctly I shouldn't tell it to move 2.568 pixels.
I should either round it to 2 or 3.
 
 
 float moveAmount = _velocity  * elapsedTime.asSeconds();
   GetSprite().move(moveAmount, 0);


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text with setOrigin and getLocalBounds causing a stray pixel to appear
« Reply #7 on: November 30, 2012, 12:22:00 pm »
Quote
Can you confirm whether move should also use whole numbers?
It is really much more complicated than this, but you the short answer "yes" is probably enough.
Laurent Gomila - SFML developer