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

Author Topic: getLocalBounds and getGlobalBounds  (Read 4181 times)

0 Members and 1 Guest are viewing this topic.

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
getLocalBounds and getGlobalBounds
« on: August 09, 2015, 03:55:42 pm »
So I'm trying to make a button but I can't resize and center the text because both getLocalBounds and getGlobalBounds are returning 0. Here is the code:

       
void Button::setString(sf::String string)
        {
                m_string = string;
                m_text.setString(m_string);
                std::cout << m_text.getGlobalBounds().width; //This returns  0
    }
 

m_text is a private sf::Text variable.
The text is drawn on the screen, the setChar size method works as well. The only thing that isn't right here is that getGlobalBounds().width/height and getLocalBounds().width/height always return 0.
« Last Edit: August 09, 2015, 07:47:58 pm by ScArL3T »

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: getLocalBounds and getGlobalBounds
« Reply #1 on: August 09, 2015, 04:15:31 pm »
Are you sure font is set and string is not empty?
Back to C++ gamedev with SFML in May 2023

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: getLocalBounds and getGlobalBounds
« Reply #2 on: August 09, 2015, 04:19:18 pm »
Here is an image with the result:
« Last Edit: August 10, 2015, 08:48:13 am by ScArL3T »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: getLocalBounds and getGlobalBounds
« Reply #3 on: August 09, 2015, 07:42:05 pm »
Are you checking the bounds' width before setting the font?
(hint: you are)
« Last Edit: August 09, 2015, 07:44:58 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: getLocalBounds and getGlobalBounds
« Reply #4 on: August 09, 2015, 07:44:07 pm »
I think I found the problem. If I load the font inside the button constructor like this it works:
m_font.loadFromFile("data/font3.ttf");
m_text.setFont(m_font);

But I can't find a good solution to use the gui.setGlobalFont(font) properly. If you have any ideas please let me know. Thank you!

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: getLocalBounds and getGlobalBounds
« Reply #5 on: August 09, 2015, 07:46:57 pm »
As I wrote in my previous post, you are checking the bounds before setting the font. You can access the bounds of the text after it has a font otherwise how does it know how large it is?
Try accessing its bounds after adding the "button" to "gui".
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: getLocalBounds and getGlobalBounds
« Reply #6 on: August 09, 2015, 08:11:32 pm »
Ok I fixed it. Thank you!   ;D

 

anything