I have an sf::RectangleShape inputFieldRect_ and an sf::Text inputFieldText_ and I want to center the text on the rectangle because the sf::Text's dimensions will vary because its string will have different lengths.
After setting inputFieldText_'s string with inputFieldText_.setString(s); (s is a an std::string that the user inputs) I am adjusting its position so that it's still in the center of inputFieldRect_.
This is my code for that but the text is too much to the left and a little too much to the bottom, so not perfectly centered:
sf::FloatRect rectBounds = inputFieldRect_.getGlobalBounds();
sf::FloatRect textBounds = inputFieldText_.getGlobalBounds();
inputFieldText_.setPosition(
rectBounds.left + (rectBounds.width / 2) - (textBounds.width / 2),
rectBounds.top + (rectBounds.height / 2) - (textBounds.height / 2)
);
Can someone please help me? I don't see the flaw.