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

Author Topic: Displaying text in center of screen  (Read 22993 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying text in center of screen
« Reply #15 on: July 23, 2014, 11:15:03 pm »
Quote
    text.setOrigin(text.getLocalBounds().left/2.0f,text.getLocalBounds().top/2.0f);
    text.setString("test"); //set version
Don't expect to get a non-empty rectangle if you call getLocalBounds() before setting the content of the text.
Laurent Gomila - SFML developer

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #16 on: July 23, 2014, 11:37:57 pm »
Quote
    text.setOrigin(text.getLocalBounds().left/2.0f,text.getLocalBounds().top/2.0f);
    text.setString("test"); //set version
Don't expect to get a non-empty rectangle if you call getLocalBounds() before setting the content of the text.
Alright thanks, gotchya.

But that still doesn't solve why the text isn't in the middle.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Displaying text in center of screen
« Reply #17 on: July 23, 2014, 11:41:22 pm »
He was trying to help you figure out that you need to put the setOrigin() line after the setString() line.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #18 on: July 23, 2014, 11:55:20 pm »
I just changed all of that and put it behind, but it still isn't in the center.

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Displaying text in center of screen
« Reply #19 on: July 24, 2014, 12:21:22 am »
On the screenshot it looks like the width and height values are reversed. Check their definition if the W_WIDTH is really larger than W_HEIGHT.

If you don't want to show code, just write minimal example. It would take like 5 minutes.
« Last Edit: July 24, 2014, 12:23:10 am by krzat »
SFML.Utils - useful extensions for SFML.Net

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #20 on: July 24, 2014, 12:33:57 am »
/
On the screenshot it looks like the width and height values are reversed. Check their definition if the W_WIDTH is really larger than W_HEIGHT.

If you don't want to show code, just write minimal example. It would take like 5 minutes.
You were indeed right.
I switched the values and It's like this now

It looks almost centered, but slightly too far right.
« Last Edit: July 24, 2014, 12:35:33 am by BeautiCode »

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #21 on: July 24, 2014, 12:40:59 am »
Nevermind I fixed it. Thanks to all of you.
I appreciate your constant efforts to help, you all are very nice people.
Thanks.
Btw heres the part of the code for future reference
    text.setString("test");
    text.setPosition(W_WIDTH/2.0f,W_HEIGHT/2.0f); //Window width divided by 2, same with height.
    text.setOrigin(text.getLocalBounds().width/2.0f,text.getLocalBounds().height/2.0f);
 

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Displaying text in center of screen
« Reply #22 on: July 24, 2014, 01:57:01 am »
What did you change to fix the image you posted?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #23 on: July 24, 2014, 02:06:07 am »
I switch the W_WIDTH and W_HEIGHT value as they were mixed up.
And I accesed .width and .height from getLocalBounds() instead of left and top
EDIT: And put the setString() call before all of those

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Displaying text in center of screen
« Reply #24 on: July 24, 2014, 02:10:28 am »
Whoa. I didn't even notice that Laurent had put left and top. Well spotted, BeautiCode  :D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying text in center of screen
« Reply #25 on: July 24, 2014, 07:38:35 am »
Quote
I didn't even notice that Laurent had put left and top.
I didn't put them. I just didn't notice them :P
Laurent Gomila - SFML developer

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Displaying text in center of screen
« Reply #26 on: July 24, 2014, 01:50:21 pm »
In order to put your text right in the middle you need to switch the lines of setPosition and setOrigin. Now your text is positioned based on the top left corner of the bounding rectangle, and then you set the origin to the middle of it.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Displaying text in center of screen
« Reply #27 on: July 24, 2014, 02:20:00 pm »
that's why people asks for a minimal example. i asked for the values of W_HEIGHT, W_WIDTH, and the window size. the answer was:
Quote
The window height is the value of W_HEIGHT and the window width is the value of W_WIDTH.

which wasn't true after all. some things are too hard to guess. but i'm glad you could solve your problem.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Displaying text in center of screen
« Reply #28 on: July 24, 2014, 08:07:25 pm »
Quote
Laurent had put left and top
I didn't
Wow. I didn't notice that it was in a quote. I think I must have been tired  :-[
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*