Search code examples
c++visual-studio-2013sfml

C++ Sfml Text output


I have recently started using SFML in C++ and I have a problem when it comes to drawing text. I managed to run the default program which draws a green circle to the window so I'm assuming there's no hardware problem. I'm using SFML 2.1 and Visual studio 2013.

Here's my code:

#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")  
#endif // SFML_STATIC

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


int main()
{

sf::RenderWindow mywindow(sf::VideoMode(800, 600), "SFML window");

sf::Text text;

text.setString(std::string("Hello World!"));

text.setCharacterSize(24);

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

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

    mywindow.clear();

    mywindow.draw(text);

    mywindow.display();

    std::cin.get(); //To prevent the window automatically closing.

}

Thank you for any help in advance!


Solution

  • Before you draw the text you need to provide the font by making an sf::Font object, using its loadFromFile() function and then passing it as an argument to the sf::Text's setFont() function.

    sf::Font font;
    font.loadFromFile("file_path");
    text.setFont(font);