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

Author Topic: How do I get the boundaries of an Sf::Text object?  (Read 2000 times)

0 Members and 1 Guest are viewing this topic.

Justin H

  • Newbie
  • *
  • Posts: 2
    • View Profile
How do I get the boundaries of an Sf::Text object?
« on: December 10, 2012, 03:52:36 am »
Code: [Select]
void gui::addMenuButton(int x, int y, string name){
    //place text on top of the position of button (the sprite)
    sf::Text text(name, sf::Font::getDefaultFont(), 16);
    text.setColor(sf::Color(0,0,0));
    text.setPosition(x+(35*btns.size()), y);
    btns.push_back(buttons());

    //add button details
    btns[btns.size()-1].text = text;
    btns[btns.size()-1].children = 0;
    btns[btns.size()-1].x = x;
    btns[btns.size()-1].y = y;


    //now generate the sprite
    sf::Texture texture;
    texture.loadFromFile("img/transparent.jpg");

    sf::Sprite sprite;
    sprite.setTexture(texture);
    sprite.setPosition((float) x, (float) y);
    sprite.setScale(text.getGlobalBounds().width, text.getGlobalBounds().height);
    sprite.setColor(sf::Color(0,0,0));
    btns[btns.size()-1].sprite = sprite;
}

As you can see, I wish to create a sprite with a transparent texture with the same height & width as the text object. However, it seems when I call the function getGlobalBounds() on the text object to get its height & width, it's never accurate.

E.g:

without sprite:


with sprite:

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: How do I get the boundaries of an Sf::Text object?
« Reply #1 on: December 10, 2012, 06:22:07 am »
Unless you're using a 1x1 pixel texture, which seems unlikely, you need to adjust the scaling factors.

Justin H

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How do I get the boundaries of an Sf::Text object?
« Reply #2 on: December 10, 2012, 06:33:32 am »
Unless you're using a 1x1 pixel texture, which seems unlikely, you need to adjust the scaling factors.

I'm not too sure what you mean. Adjust the scaling factor of what?

I did try this:

Code: [Select]
sprite.setScale((text.getGlobalBounds().width/9), (text.getGlobalBounds().height/9));
I'm eyeballing it, but it seems accurate.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything