Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing Text [Solved]  (Read 2949 times)

0 Members and 1 Guest are viewing this topic.

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Drawing Text [Solved]
« 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.
« Last Edit: October 29, 2012, 01:04:46 am by Luis »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing Text
« Reply #1 on: October 28, 2012, 03:38:40 pm »
You need sf::Font, the default font was removed.
Back to C++ gamedev with SFML in May 2023

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Drawing Text
« Reply #2 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.
« Last Edit: October 28, 2012, 04:16:43 pm by Luis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Drawing Text
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Drawing Text
« Reply #4 on: October 28, 2012, 04:17:21 pm »
I do clear the window every frame as my last two code exerts show ;).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing Text
« Reply #5 on: October 28, 2012, 04:24:44 pm »
Show us full main function that reproduces your problem.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Drawing Text
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Drawing Text
« Reply #7 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing Text
« Reply #8 on: October 28, 2012, 10:49:23 pm »
You should check whether font.loadFromFile succeeds or not (returns true or false).
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing Text
« Reply #9 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.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing Text
« Reply #10 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
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing Text
« Reply #11 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...
Back to C++ gamedev with SFML in May 2023

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Drawing Text
« Reply #12 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!