SFML community forums

Help => Graphics => Topic started by: Redee on January 24, 2018, 08:05:29 pm

Title: Text wrong display position
Post by: Redee on January 24, 2018, 08:05:29 pm
I create simple Text with default null position and why so much offset from Top and Left ??
SFML-2.4.2-vc14-x86
Here example of code and scr >>

win.create(VideoMode(600, 550), "111", Style::Default);
win.setVerticalSyncEnabled(true);

Font font;
font.loadFromFile("Data/arial.ttf");

Event ev;
while (win.isOpen())
{
  while (win.pollEvent(ev))
  {
    if (ev.type == Event::Closed)
      win.close();
  }

  Text txt;
  txt.setFont(font);
  txt.setCharacterSize(65);
  txt.setString("D11");
 
  win.clear(Color(80, 80, 80));
  // Why so much offset ??
  win.draw(txt);
  win.display();
}

(https://c1.staticflickr.com/5/4648/25007365617_63fd47fc5c_o.jpg)
Title: Re: Text wrong display position
Post by: M74 on January 24, 2018, 08:22:40 pm
Hi,

did you tried this with different fonts? I never had this before.

Regards,
Mathias
Title: Re: Text wrong display position
Post by: Redee on January 24, 2018, 08:26:28 pm
Its standart Arial font.
I can manually measure offset to need me character sizes from Top / Left - but its looks like horrible decision.

Please help me normal display Text at exact pixels in window.

Okey I found - its offset in Text::getLocalBounds().

And some Topic already >> https://en.sfml-dev.org/forums/index.php?topic=20284.0
Title: Re: Text wrong display position
Post by: Pinco on February 04, 2018, 09:51:36 pm
Its standart Arial font.
I can manually measure offset to need me character sizes from Top / Left - but its looks like horrible decision.

Please help me normal display Text at exact pixels in window.

Okey I found - its offset in Text::getLocalBounds().

And some Topic already >> https://en.sfml-dev.org/forums/index.php?topic=20284.0

You don't have to do it manually, you can use .getGlobalBounds().top and .left, that should do the trick. I had the same problem.

If you want to set the position of the text based on the first pixel in the top-left corner you should do this:
txt.setOrigin(txt.getGlobalBounds().left, txt.getGlobalBounds().top) to compensate for the offset

EDIT: Didn't see you solved it already, I'm gonna leave this here for anyone who needs the answer.
Title: Re: Text wrong display position
Post by: Hapax on February 05, 2018, 12:48:44 am
You don't have to do it manually, you can use .getGlobalBounds().top and .left, that should do the trick. I had the same problem.

If you want to set the position of the text based on the first pixel in the top-left corner you should do this:
txt.setOrigin(txt.getGlobalBounds().left, txt.getGlobalBounds().top) to compensate for the offset

EDIT: Didn't see you solved it already, I'm gonna leave this here for anyone who needs the answer.

Just to clarify, when settings the origin in most cases (including this one), you would use the local bounds (as already mentioned above by Redee) not the global bounds.