简体   繁体   English

sf::Text 不显示?

[英]sf::Text doesn't display?

I'm trying to display a sf::Text on my window and it doesn't work and i don't understand why !我试图在我的窗口上显示一个 sf::Text 但它不起作用,我不明白为什么!

Here's an example of the code: constructor下面是代码示例:构造函数

timerText.setFont(getFont());
timerText.setString("test");
timerText.setCharacterSize(70);
timerText.setColor(Color::Red);

getFont() : getFont() :

Font& Timer::getFont(){
Font font;
if(!font.loadFromFile("arial.ttf")){
    cout << "font not loaded" << endl;
}
return font;

} }

draw() :画() :

timerText.setPosition(Vector2f(center.x - 8, center.y - size.y/2 + 12));
window.draw(timerText);

All my other draws work perfectly and i haven't error when i'm loading the font.我的所有其他绘图都完美无缺,并且在加载字体时没有出错。 Sorry for my English and thank you for your help !对不起我的英语,感谢您的帮助!

Resolved by adding a private variable sf::Font in my class and by swaping setFont(getFont());通过在我的类中添加一个私有变量 sf::Font 并交换 setFont(getFont()); 来解决; by font.loadFromFile(fontFile);通过 font.loadFromFile(fontFile); setFont(font);设置字体(字体);

I still doesn't understand my error but now it works.我仍然不明白我的错误,但现在它起作用了。

at getFont() , you load a font into local variable in stack memory.getFont() ,您将字体加载到堆栈内存中的局部变量中。 When the function returns, it is deleted automatically..当函数返回时,它会自动删除..

You should move or allocate it other place..您应该将其移动或分配到其他地方..

Font font;
Font& Timer::getFont(){
    if(!font.loadFromFile("arial.ttf")){
        cout << "font not loaded" << endl;
    }
    return font;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM