Getting text sizes in SFML

I was wondering how to get the dimensions of my text in SFML?

I tried to do it like this:

sf::Text text("Hello SFML", font, 50); // using text.getRect() // i also tried getScale() & getSize() // neither are correct text.setPosition( window.getSize().y/2 - text.getRect().y,50 ); 

Somebody knows?

Thanks:)

+7
source share
2 answers

Looking at the documentation, it seems that the getLocalBounds function might come in handy. The line will be:

 float width = text.getLocalBounds().width; 

I'm not sure if the sf::Text object will add any padding at the ends of the bounding box.

Alternatively, you can use findCharacterPos something like:

 float width = text.findCharacterPos(numChars - 1).x - text.findCharacterPos(0).x; 

where numChars is the number of characters in the string of your text object. However, since findCharacterPos will return global coordinates, it might be more convenient to use getLocalBounds , so you don’t have to worry about whether your text object has any transformations applied to it.

+13
source

You can use getGlobalBounds () to get the size / coordinates after the conversion (rotation, scale, moving ...). Otherwise, getLocalBounds ().

Doc: http://www.sfml-dev.org/documentation/2.3.1/classsf_1_1Text.php

0
source

All Articles