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

Author Topic: Text wrong display position  (Read 3244 times)

0 Members and 1 Guest are viewing this topic.

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Text wrong display position
« 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();
}

« Last Edit: January 24, 2018, 08:42:29 pm by Redee »

M74

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Text wrong display position
« Reply #1 on: January 24, 2018, 08:22:40 pm »
Hi,

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

Regards,
Mathias

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Text wrong display position
« Reply #2 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
« Last Edit: January 24, 2018, 09:22:19 pm by Redee »

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Text wrong display position
« Reply #3 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.
« Last Edit: February 04, 2018, 10:01:05 pm by Pinco »
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Text wrong display position
« Reply #4 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything