【问题标题】:SFML - Not displaying TextSFML - 不显示文本
【发布时间】:2017-09-03 16:16:22
【问题描述】:

在玩 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();

【问题讨论】:

  • window.clear() 在哪里?还要确保不要在白色背景上绘画。
  • put font.loadFromFile("fonts/arial.tff");在一个条件下,为了检查是否加载了字体
  • window.clear 在那里,忘记包含它,背景是空的。我认为字体没有被加载,所以字体的路径可能不正确。
  • @pmaxim98 我如何建立这个条件?会是这样吗? if (!font.loadFromFile("font/arial.tff") { // 错误信息?}

标签: c++ text sfml


【解决方案1】:

-首先:为了能够使用你的字体,它必须存在于项目ma​​in文件夹中的fonts文件夹中,否则loadFromFile()函数应该返回一个 false,但请注意这是写在官方文档中的:

loadFromFile 函数有时会在没有明显原因的情况下失败

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

祝你好运。

【讨论】:

  • 我完全不知道为什么,但我从系统字体中复制并粘贴了 arial 字体。我现在已经下载了一种免版税字体,它可以工作了。谢谢大家的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 2020-11-22
  • 1970-01-01
相关资源
最近更新 更多