SFML 绘图居中文本 [英] SFML Drawing centered text
本文介绍了SFML 绘图居中文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 SFML 中的一些文本居中.但正如您在下图中看到的那样,它并没有真正起作用.
I'm trying to center some text in SFML. It doesen't really work though as you can see in the picture below.
也许我的数学有问题?
void TextRenderer::renderCentered(sf::RenderWindow& window, std::string string, sf::Vector2f position, int size, sf::Color color) {
sf::Text text;
text.setFont(TextRenderer::kavoon);
text.setString(string);
float width = text.getLocalBounds().width;
text.setPosition(position.x - width / 2, position.y);
text.setCharacterSize(size);
text.setColor(color);
window.draw(text);
}
在渲染方法中:
TextRenderer::renderCentered(*window, pickuptext.str(), sf::Vector2f(player->sprite.getPosition().x, player->sprite.getPosition().y - 48), 28, sf::Color(255, 145, 61));
TextRenderer::renderCentered(*window, pickuptextdesc.str(), sf::Vector2f(player->sprite.getPosition().x, player->sprite.getPosition().y - 16), 18, sf::Color(255, 145, 61));
推荐答案
在边框内水平居中文本的公式是:
The formula for centering text horizontally within a bounding box is:
text_start = bounding_box_width / 2 - text_width / 2;
看起来您可能没有提供边界框水平中心点.
Looks like you may not be supplying the bounding box horizontal center point.
这篇关于SFML 绘图居中文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文