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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Justin H

Pages: [1]
1
Graphics / 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:

Pages: [1]
anything