SFML community forums

Help => Graphics => Topic started by: Luis on October 28, 2012, 03:27:18 pm

Title: Drawing Text [Solved]
Post by: Luis on October 28, 2012, 03:27:18 pm
I'm relatively new to SFML and I've been going through the 2.0 documentation for the past couple of days. That being said, I've been trying to unsuccessfully render text to my renderWindow, which I create as follows:

        window.create(sf::VideoMode(width, height, 32), title, sf::Style::Close);
        window.setFramerateLimit(60);
 

This works fine as a window is rendered.

Then, for debug purposes I attempt to draw text while the window is open with:
        window.clear(sf::Color::White);
        sf::Text text;
        text.setString("TTT");
        text.setCharacterSize(12);
        text.setColor(sf::Color::Black);
        text.setPosition(10, 10);
        window.draw(text);
        window.display();
 

The code compiles fine, however I just get a white window with no text. I have successfully linked to the graphics and window package for SFML 2.0.

Thanks in advance.
Title: Re: Drawing Text
Post by: FRex on October 28, 2012, 03:38:40 pm
You need sf::Font, the default font was removed.
Title: Re: Drawing Text
Post by: Luis on October 28, 2012, 04:09:04 pm
Thanks for that.

However, I now use the following code, yet it still only displays a white screen:
        window.clear(sf::Color::White);
        sf::Text text;
        text.setString("Hello");
        text.setFont(font);
        text.setCharacterSize(50.f);
        window.draw(text);
        window.display();
 

(I have also tried setting the position of the font to see if that was it; alas it wasn't)

After loading the font in the game's beginning:
font.loadFromFile("resources/arial.ttf");
 

The font face loads fine as no warnings are displayed in the game's console.
Title: Re: Drawing Text
Post by: eXpl0it3r on October 28, 2012, 04:11:44 pm
Please make use of the code=cpp tag and show complete example codes.

You need to call window.clear() every frame iteration.
Title: Re: Drawing Text
Post by: Luis on October 28, 2012, 04:17:21 pm
I do clear the window every frame as my last two code exerts show ;).
Title: Re: Drawing Text
Post by: FRex on October 28, 2012, 04:24:44 pm
Show us full main function that reproduces your problem.
Title: Re: Drawing Text
Post by: eXpl0it3r on October 28, 2012, 04:28:35 pm
I do clear the window every frame as my last two code exerts show ;).
Oh right, you should keep the clear(), draw() and display() calls next to each other and keep the 'logic' (i.e. setting a string, etc) separated from the drawing parts.

As Frex said, we can only help you if you provide a minimal but complete example that reproduces the problem.
Title: Re: Drawing Text
Post by: Luis on October 28, 2012, 10:39:23 pm
void Render::createWindow(unsigned int w, unsigned int h, string title) {
        //Create new rendering window
        width = w;
        height = h;

        window.create(sf::VideoMode(width, height, 32), title, sf::Style::Close);
        window.setFramerateLimit(60);
        font.loadFromFile("resources/arial.ttf");
}

void Render::render() {
        //Render
        window.clear(sf::Color::White);
        sf::Text text;
        text.setString("Hello");
        text.setFont(font);
        text.setCharacterSize(50.f);
        text.setPosition(10.f, 10.f);
        window.draw(text);
        window.display();
}
 

And my main game loop:
Render::createWindow(800, 600, "Caption");

        while(Render::window.isOpen()) {
                currentTime = clock->restart().asSeconds();
                fps = 1.f / currentTime;

                //Event polling
                sf::Event event;
                while (Render::window.pollEvent(event)) {
                        if(event.type == sf::Event::Closed) {
                                Render::window.close();
                        }
                }

                lastTime = currentTime;

                Render::render();
        }
 

Thanks again.
Title: Re: Drawing Text
Post by: Laurent on October 28, 2012, 10:49:23 pm
You should check whether font.loadFromFile succeeds or not (returns true or false).
Title: Re: Drawing Text
Post by: FRex on October 28, 2012, 10:52:36 pm
I can't see what could be wrong except that you clear with white and text is by default white.
Title: Re: Drawing Text
Post by: Laurent on October 28, 2012, 11:05:43 pm
Quote
I can't see what could be wrong except that you clear with white and text is by default white.
This is exactly what happens ;D
Title: Re: Drawing Text
Post by: FRex on October 28, 2012, 11:14:04 pm
Well this is awkward. ;D
Why did I see imaginary set color black in first snip? :o
Right.. snip with color has no font, snips with font have no color...
Title: Re: Drawing Text
Post by: Luis on October 29, 2012, 01:04:30 am
Somehow didn't catch that I omitted setting the color in the second snip..

Thanks, works now!