SFML community forums

Help => Graphics => Topic started by: massive_potato on December 21, 2012, 03:57:52 am

Title: Question about Displaying Text
Post by: massive_potato on December 21, 2012, 03:57:52 am
void displayText(const std::string & text, sf::RenderWindow & window, const sf::Vector2f & position = sf::Vector2f(0, 0), const short & size = 30, const sf::Color & color = sf::Color::White)
        {
                sf::Text t(text, sf::Font::getDefaultFont(), size);
                t.setPosition(position);
                t.setColor(color);
                window.draw(t);
        }

Is there a faster way to replicate the functionality of this function, or is this the best way to output text? Whenever I call this function it drops my framerate by about 300 fps. How can I improve this or what alternatives are available? I've gone through the documentation and have not found anything (although I certainly could have missed something).
Title: Re: Question about Displaying Text
Post by: Ahlywog on December 21, 2012, 07:12:11 am
I am incredibly new to SFML.  I've only been looking at it for the last few hours so take whatever I say with a grain of salt:

From what I can gather the only function call you NEED to make every frame is window.draw(t);.  Try moving everything else outside your loop and see what that does.
Title: Re: Question about Displaying Text
Post by: cire on December 21, 2012, 07:59:16 am
The obvious optimization would be to remove the construction of the sf::Text object out of the function call.  Presumably the sf::Text object wouldn't need to be updated every frame, only drawn every frame, which would make pretty much everything in the function unnecessary on a per-frame basis.
Title: Re: Question about Displaying Text
Post by: Nexus on December 21, 2012, 12:17:46 pm
or is this the best way to output text?
No, "instant drawing" is an oldschool procedural approach. SFML provides objects like sf::Text which may outlive single function calls, so you have to change only the necessary things. Object-oriented programming gives you much more flexibility.

Pass simple types like short by value, and avoid the SFML default font (it has been removed in the newest version).
Title: Re: Question about Displaying Text
Post by: mateandmetal on December 21, 2012, 07:09:39 pm
Creating a static sf::Text will gain some performance?
Title: Re: Question about Displaying Text
Post by: Nexus on December 21, 2012, 07:43:40 pm
Creating a static sf::Text will gain some performance?
Maybe, but that's a workaround for a problem which doesn't exist when using idiomatic OOP. With static variables, you introduce new issues like destruction at global exit or race conditions in multithreading. Use static variables if you want to have one variable per function, not as an optimization.

Anyway, "drop by 300 FPS" isn't a problem per se, of course it takes some time to draw text. Also note that FPS are not a linear scale. Without knowing how many FPS there were before, this statement is meaningless.

We should only investigate this as soon as it becomes a real problem, i.e. the application is too slow.
Title: Re: Question about Displaying Text
Post by: massive_potato on December 21, 2012, 09:16:18 pm
Thanks for all the responses! Everyone was very helpful   :D