SFML community forums
Help => Graphics => Topic started by: Justin H on December 10, 2012, 03:52:36 am
-
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:
(http://puu.sh/1zet5)
with sprite:
(http://puu.sh/1zesb)
-
Unless you're using a 1x1 pixel texture, which seems unlikely, you need to adjust the scaling factors.
-
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:
sprite.setScale((text.getGlobalBounds().width/9), (text.getGlobalBounds().height/9));
I'm eyeballing it, but it seems accurate.
-
See http://en.sfml-dev.org/forums/index.php?topic=7174