Help! SFML - writing text to screen

Hi all;

I'm trying to output text to a window using sfml, but I don't know where to start. Using version 1.6 of sfml. Can anyone help?

Thank you
Run through this tutorial. It's a good place to start.
http://sfml-dev.org/tutorials/1.6/graphics-fonts.php
Hi Stewbond;

I've tried working through the tutorial, but I can't seem to get this to work. Are there are libraries I have to "#include"?

Thank you
Yes, there are libraries that you need to include and link.

You specifically said SFML 1.6 (which is not the latest version anymore). You can download the package here:
http://sfml-dev.org/download/sfml/1.6/

Installing it can be a little tricky if you haven't done this before. Fortunately there are tutorials for getting started which explain it way better than i could here:
http://sfml-dev.org/tutorials/1.6/

Once you've downloaded the sources, compiled them, linked to the appropriate libraries. Then you'll need to:
#include <SFML/Graphics.hpp>
I'm able to compile the code (after some tweaking) that i copied and pasted from that tutorial into a cpp file, but I still don't see any text in the window. The window appears and then disappears.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//#include "Test.h"
#include "stdafx.h"
#include <SFML/Graphics.hpp>

void main()
{

sf::Font MyFont;
if (!MyFont.LoadFromFile("plane.ttf", 50))
{
    // Error...
}

sf::String Text;
Text.SetText("Hello");
Text.SetFont(MyFont);
Text.SetSize(50);
Text.SetColor(sf::Color(128, 128, 0));
Text.SetRotation(90.f);
Text.SetScale(2.f, 2.f);
Text.Move(100.f, 200.f);

sf::RenderWindow Window;
Window.Draw(Text);
}
That's because Window.Draw() will return rather soon. You need to draw in a while loop:

This code comes from:
http://sfml-dev.org/tutorials/1.6/graphics-window.php

Any references to text are what I've added.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    sf::String Text;
    Text.SetText("Hello");
    Text.SetFont(MyFont);
    Text.SetSize(50);
    Text.SetColor(sf::Color(128, 128, 0));
    Text.SetRotation(90.f);
    Text.SetScale(2.f, 2.f);
    Text.Move(100.f, 200.f);
    
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

        App.Draw(Text);
        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


Last edited on
Thanks for your help Stewbond, I appreciate your feedback. Will look to add some sort of a while loop to incorporate this into a a pong game i've been working on.
Why the heck do you use 1.6? it is obselete just use 2.1 like anyone else!
Also if you need help for sfml do not hesitate to check the official site/forums, they are very, VERY useful
Topic archived. No new replies allowed.