C++ SFML 2.4.2在绘制sf::Text对象之前获取localBounds

C++ SFML 2.4.2在绘制sf::Text对象之前获取localBounds,c++,text,sfml,text-alignment,C++,Text,Sfml,Text Alignment,我正在做一个项目,需要用SFML完成一些按钮。我们使用的是2.4.2版。除了文本对齐之外,我已经成功地完成了所有的事情,我很困惑到底是什么问题 为了设置文本的对齐方式,我知道必须正确设置其原点和位置。因为人们可能会更改按钮的文本,所以我决定将对齐功能放入setString函数中 出于调试目的,我添加了两个控制台输出来显示一些数据。下面是函数的外观: void rgf::Button::setString(const sf::String & str) { text.setStri

我正在做一个项目,需要用SFML完成一些按钮。我们使用的是2.4.2版。除了文本对齐之外,我已经成功地完成了所有的事情,我很困惑到底是什么问题

为了设置文本的对齐方式,我知道必须正确设置其原点和位置。因为人们可能会更改按钮的文本,所以我决定将对齐功能放入setString函数中

出于调试目的,我添加了两个控制台输出来显示一些数据。下面是函数的外观:

void rgf::Button::setString(const sf::String & str)
{
    text.setString(str);

    std::cout << "Original Origin: " << text.getOrigin().x << ", " << text.getOrigin().y << std::endl;
    std::cout << "Original Position: " << text.getPosition().x << ", " << text.getPosition().y << std::endl;
    std::cout << "Original LocalBounds: " << text.getLocalBounds().width << ", " << text.getLocalBounds().height << std::endl;

    auto textRect = text.getLocalBounds();
    auto btnRect = body.getLocalBounds();
    text.setOrigin(textRect.left + textRect.width / 2, textRect.height + textRect.height / 2);
    text.setPosition(btnRect.left + btnRect.width / 2, btnRect.top + btnRect.height / 2);

    std::cout << "New Origin: " << text.getOrigin().x << ", " << text.getOrigin().y << std::endl;
    std::cout << "New Position: " << text.getPosition().x << ", " << text.getPosition().y << std::endl;
    std::cout << "New LocalBounds: " << text.getLocalBounds().width << ", " << text.getLocalBounds().height << std::endl;
}
void rgf::Button::setString(const sf::String&str)
{
text.setString(str);

std::coutsetFont()函数是在setString()函数之后调用的,这意味着localBounds仍然是0,因为没有字体来指示宽度或高度。

这两个函数都是
sf::Text::getLocalBounds()
sf::Text::draw()
调用同一个内部成员来更新几何体,因此不会有任何差异。在更新文本之前,您是否分配了
sf::Font