【问题标题】:Issues to display Text from sfml从 sfml 显示文本的问题
【发布时间】:2014-06-05 22:21:46
【问题描述】:

感谢 sfml,我想在我的 Windows 中显示文本。但是我不是来做的,文本只是没有出现在窗口中,这真的很奇怪,因为我可以毫无困难地显示线条! 你能检查我的代码并告诉我我需要改变什么吗? :)

我的 main.cpp

int main()
{
    bool sortir = false;
    // Création de la pile de double
    Pile<double> pile_double;

    // Création de la pile de string
    Pile<string> pile_string;

    // Création du map
    map<string, function<void()>> _map;

    int largeur_fenetre = 300;
    int hauteur_fenetre = 400;

    // Création de la fenetre
    sf::RenderWindow window(sf::VideoMode(largeur_fenetre, hauteur_fenetre), "Interpreteur NPI");

    //copie largeur / longueur
    push(pile_double, (double) largeur_fenetre);
    push(pile_double, (double) hauteur_fenetre);

    // on fait tourner le programme tant que la fenêtre n'a pas été fermée
    while (window.isOpen())
    {
        // on traite tous les évènements de la fenêtre qui ont été générés depuis la dernière itération de la boucle
        sf::Event event;

        while (window.pollEvent(event))
        {
            cout << "passage 1er while" << endl;
            // fermeture de la fenêtre lorsque l'utilisateur le souhaite
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // effacement de la fenêtre en noir
        window.clear(sf::Color::Black);
        window.display();
        cout << "coucou" << endl;

        while (sortir == false)
        {
            cout << "Entrez la commande souhaité >";
            string carac;
            cin >> carac;
            // Recherche du string dans le map
            for (map<string, function<void()>>::iterator it = _map.begin(); it != _map.end(); ++it) // Debut recherche de la relation
            {
                // S'il est trouvé on demarre sa fonction associé
                if (carac == it->first)
                {
                    (it->second)();
                    pile_double.display();
                    pile_string.display();
                    window.display();
                }
            } // Fin de la recherche de la relation string -> fonction | Fin du For

            if (carac == "exit")
            {
                sortir = true;
            }
        }
    }
    return 0;
}

函数.cpp

void drawstr(Pile<double>& nomDeLaPile, sf::RenderWindow& window, Pile<string>& nomDeLaPile2)
{
    sf::Text text;

    sf::Font font;

    text.setFont(font);

    text.setString("Hello world");

    text.setCharacterSize(24);

    text.setColor(sf::Color::Red);

    text.setStyle(sf::Text::Bold | sf::Text::Underlined);

    window.draw(text);
}

对不起,我的英语很糟糕,我希望你能理解!

【问题讨论】:

    标签: c++ sfml


    【解决方案1】:

    你这样做:

    sf::Font font;
    
    text.setFont(font);
    

    但是你没有加载任何字体,你“设置”了一个空字体......

    之前尝试过类似的方法:

    if (!font.loadFromFile("arial.ttf"))
    {
        // error
    }
    

    但你不应该在绘图功能中这样做。 启动应用程序时必须加载一次字体。

    【讨论】:

      【解决方案2】:

      来自 SFML 教程:

      请注意,SFML 不会自动加载您的系统字体,即 font.loadFromFile("Courier New") 不起作用。首先是因为 SFML 需要文件名,而不是字体名,其次是因为 SFML 不需要 可以神奇地访问系统的字体文件夹。所以,如果你想 加载字体,你的应用程序需要有字体文件, 像其他资源(图像、声音等)一样。

      SFML 不提供默认字体,或与此相关的字体。

      当您使用以下方法创建 Font 对象时:

      sf::Font font;
      

      它是一个空字体。您需要做的是从网站下载字体,例如http://www.dafont.com/ 或查找字体文件在您的计算机上的位置。

      然后你应该加载字体(来自同一个 SFML 教程):

      sf::Font font;
      if (!font.loadFromFile("arial.ttf"))
      {
          // error...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多