SFML - 不显示文本

SFML - Not displaying Text

玩弄 SFML,我确实设置了字体,但它仍然不想显示文本。任何帮助,将不胜感激。

谢谢,

欧文

// Choose a font
Font font;
font.loadFromFile("fonts/arial.tff");

// Set our message font
scoreText.setFont(font);

scoreText.setString("Score = 0");
scoreText.setCharacterSize(100);

// Choose a color
scoreText.setFillColor(Color::White);

// Position the text
scoreText.setPosition(20, 20);

window.draw(scoreText);
window.display();

-首先:为了能够使用您的字体,它必须存在于项目的 main 文件夹中的 fonts 文件夹中, 否则函数 loadFromFile() 应该 returns 为 false, 但注意官方文档中是这样写的:

The loadFromFile function can sometimes fail with no obvious reason

-其次:正如@pmaxim98所述,在绘制任何东西之前需要调用clear()函数,并且颜色参数应该与文本填充颜色不同,以便您可以查看显示的文本。

-第三:尝试将字体文件放在项目的主文件夹中并尝试这个最小代码:

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

int main()
{

RenderWindow window(VideoMode(800,600),"TEXT");

/****************************************************/

//Declare a Font object
Font font;

//Load and check the availability of the font file
if(!font.loadFromFile("arial.ttf"))
{
    cout << "can't load font" << endl;
}

//Declare a Text object
Text text("Score = 0",font);

//Set character size
text.setCharacterSize(100);

//Set fill color
text.setFillColor(Color::White);

/****************************************************/


while(window.isOpen())
{
    Event event;
    while(window.pollEvent(event))
    {
         if(event.type == Event::Closed){window.close();}
    }

    //Clear the window
    window.clear();
    //Draw the text
    window.draw(text);
    //Display the text to the window
    window.display();
}

return 0;
}

祝你好运。