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

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

0 Members and 2 Guests are viewing this topic.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Displaying text in center of screen
« on: July 22, 2014, 08:22:21 am »
To try and display text in the center of the screen I do
text.setPosition(screen_width/2.0f,screen_height/2.0f);
 

But it displays the text somewhere close to the bottom left of the screen. Anyone know why? I looked on google,  I didn't understand much because mostly people were using getLocalBounds() and setOrigin(), what's the difference between that and setPosition();?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Displaying text in center of screen
« Reply #1 on: July 22, 2014, 08:34:46 am »
setPosition will set the top-left corner of the text to the given position (by default). That's why people use getLocalBounds() + setOrigin(), to set the text's origin to its center instead of its top-left corner.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Displaying text in center of screen
« Reply #2 on: July 22, 2014, 08:52:17 am »
An alternative, if you don't want to change the origin, is to get the width of the screen, subtract the width of the texts bounding box, divide the result by 2 and you are left with the x coordinate for a call to setPosition with the default origin. Similar calculation to get the y coordinate.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #3 on: July 22, 2014, 10:17:54 pm »
So how exactly would I use getLocalBounds() and setOrigin? Example please?


BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #5 on: July 23, 2014, 12:46:19 am »
I'd really appreciate quick thorough examples tbh, instead of just commenting with a link to the tutorials page on the official website, or just giving a quick and less elaborative answer, please.
I do slightly understand but it'd be better with an example.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Displaying text in center of screen
« Reply #6 on: July 23, 2014, 03:11:54 am »
Something like this for the X coordinate. And if this isn't enough information....  ::)

(X position) = (screen width) / 2 - (text width) / 2;
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Displaying text in center of screen
« Reply #7 on: July 23, 2014, 03:44:02 am »
(X position) = (screen width) / 2 - (text width) / 2;
I always prefer:
[X position] = ( [screen width] - [text width] ) / 2;  ;D

BeautiCode, I'm not sure what you're not understanding from the tutorials; the links to them went to the parts that explained setOrigin() and getLocalBounds(). As said above, set the origin to half of the size, which you can get from the local bounds (feel free to click on the getLocalBounds' type if you need to know more about that type), and then just set the position to the centre of the window.

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 #8 on: July 23, 2014, 04:08:53 am »
When I use setOrigin() my text doesn't get displayed at all, but when I use setPosition() it does.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Displaying text in center of screen
« Reply #9 on: July 23, 2014, 04:17:22 am »
You need to use both  :P
setOrigin moves the origin's location. setPosition moves the sprites location based on its origin.

set the origin to half of the size...and then just set the position to the centre of the window.
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 #10 on: July 23, 2014, 08:43:04 am »

    text.setPosition(W_WIDTH/2.0f,W_HEIGHT/2.0f);
    text.setOrigin(text.getLocalBounds().width/2.0f,text.getLocalBounds().height/2.0f);
 
What am I doing wrong?

Doodlemeat

  • Guest
Re: Displaying text in center of screen
« Reply #11 on: July 23, 2014, 10:47:34 am »
You mind posting the whole code? Try to use globalBounds instead of local.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Displaying text in center of screen
« Reply #12 on: July 23, 2014, 10:57:14 am »
Quote
You mind posting the whole code?
A complete and minimal one, if possible.

Quote
Try to use globalBounds instead of local.
Nop, never do that. The origin must be given in local coordinates.
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Displaying text in center of screen
« Reply #13 on: July 23, 2014, 12:03:44 pm »
    text.setPosition(W_WIDTH/2.0f,W_HEIGHT/2.0f);
    text.setOrigin(text.getLocalBounds().width/2.0f,text.getLocalBounds().height/2.0f);
 
What am I doing wrong?

what are the values of W_WIDTH and W_HEIGHT, and what is the window size?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Displaying text in center of screen
« Reply #14 on: July 23, 2014, 11:08:12 pm »
The window height is the value of W_HEIGHT and the window width is the value of W_WIDTH.

    sf::Text text;
    text.setFont(default_font);
    text.setStyle(sf::Text::Bold | sf::Text::Underlined);
    text.setColor(sf::Color::White);
    text.setCharacterSize(textsize);
    text.setPosition(W_WIDTH/2.0f,W_HEIGHT/2.0f);
    text.setOrigin(text.getLocalBounds().left/2.0f,text.getLocalBounds().top/2.0f);
    text.setString("test"); //set version
    win->draw(text);
    win->display();
 

All the other code is in different classes and stuff and this one is not an open source project, so I didn't want to show much. But I think this is enough.