简体   繁体   中英

Issues to display Text from sfml

I would like to display text in my Windows thanks to sfml. But I don't arrived to do it, the texte just don't appear in the windows, that's really strange because I can display lines without difficulties ! Can you check my code and tell me what I need to change please ? :)

My 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;
}

fonction.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);
}

My apologies for very bad English, I hope you understand anyways!

You do:

sf::Font font;

text.setFont(font);

But you don't load any font, you "set" an empty font...

Try something like that before:

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

But you shouldn't do that in the drawing function. Font has to be loaded once when starting the application.

From the SFML tutorial:

Note that SFML won't load your system fonts automatically, ie font.loadFromFile("Courier New") won't work. First because SFML requires file names, not font names, and secondly because SFML doesn't have a magic access to your system's font folder. So, if you want to load a font, you need to have the font file with your application, like other resources (images, sounds, ...).

SFML does not provide a default font, or an font for that matter.

When you created a Font object using:

sf::Font font;

it is an empty font. What you need to do is download a font from a website, such as http://www.dafont.com/ or find where the font files are on your computer.

Then you should load the font (from the same SFML tutorial):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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