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

Author Topic: problems rendering text (SFML 2.0)  (Read 1997 times)

0 Members and 2 Guests are viewing this topic.

jtjin

  • Newbie
  • *
  • Posts: 3
    • View Profile
problems rendering text (SFML 2.0)
« on: August 26, 2012, 07:35:25 am »
So I built SFML 2.0 straight from the GitHub repo, and I'm trying to run this

sf::RenderWindow window(sf::VideoMode(300,200), "SFML Test");
sf::Text text;
text.setString("Hello");

while (window.isOpen())
{
        sf::Event event;
        while(window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        window.close();
        }
        window.clear();
        window.draw(text);
        window.display();
}

First of all, I think the sample code in the tutorial (under 2.0 node) as well as the example in the documentation for sf::Text needs to be updated as there is no constructor for sf::Text that takes in a string directly. I tried sf::Text text("Hello"); and got a compiler error :(

Anyway, running the above code I just get a window with no text displayed. Is there something I'm missing? Do I need to specify extra parameter to the Text object (font, color, etc?) before it will show up in the window? I got the impression from the docs that it comes with a default font, so I didn't think I needed to.

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: problems rendering text (SFML 2.0)
« Reply #1 on: August 26, 2012, 08:21:09 am »
I tried sf::Text text("Hello"); and got a compiler error
You're right, that tutorial needs to be updated.

The reason for the lack of that constructor and why you don't see anything is because SFML no longer ships a default font. You will have to load a Font first and assign it to the text.
JSFML - The Java binding to SFML.

jtjin

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: problems rendering text (SFML 2.0)
« Reply #2 on: August 26, 2012, 08:43:48 am »
This worked, thanks so much!

sf::Font font;
font.loadFromFile("Resources\\segoe.ttf");
       
sf::Text text;
text.setString("Hello");
text.setFont(font);
 

I had actually tried loading in a font before, but forgot to escape the slash in my path, and so I thought text rendering just wasn't working for me.

 

anything