代码之家  ›  专栏  ›  技术社区  ›  Owen Nel

SFML-不显示文本

  •  1
  • Owen Nel  · 技术社区  · 8 年前

    在使用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();
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Bo Halim    8 年前

    -首先:为了能够使用你的字体,它必须出现在 字体 主要的 文件夹,否则函数 loadFromFile() 应返回false,但请注意,这已写入官方文档:

    -其次:正如@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;
    }
    

    推荐文章