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

Author Topic: Displaying more-and-more (sf::)texts slows the program down rapidly.  (Read 1785 times)

0 Members and 1 Guest are viewing this topic.

val

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hello.

I'am trying to make a roguelike game, and i'll need quite a few sf::Texts to display messages, the interface, the dungeon, etc. When i tried to add more string to display the game dropped from ~400 fps to ~100 after 5-6 extra texts. Adding ten or so slowed the game down to 30-40 fps. I made a "small" example program in the vein of what iam doing in my main program. Here i start with 4000 or so fps and adding 20 sf::Texts to display slows it down to ~200. I tried appending text to a single string to display with one sf::Text, but the resoult was the same.  Is displaying text this costly? Or iam doing something wrong?

Test Code Below.
(click to show/hide)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Displaying more-and-more (sf::)texts slows the program down rapidly.
« Reply #1 on: October 28, 2014, 08:02:56 pm »
This isn't a very realistic test since you're constructing the sf::Text objects and setting their strings and fonts every single frame.  In particular, all the glyph rendering is being redone every single frame.  Normally you do that once and then draw the same sf::Text for several frames in a row (since the humans need time to actually read the text).

Your fps counter is also constructing sf::Text objects from scratch every single time, so the time it takes to print the number might be non-negligible.  Try printing directly to std::cout to cut down on that overhead.  Ideally, you would do no printing at all most frames and only output a rolling average fps maybe once every few seconds.

Also note that 4000 fps vs 200 fps is a bit meaningless, since super-high fps like 4000 usually indicate a program that does absolutely nothing, not a program with amazing performance.



P.S. It's generally a bad idea to use #pragmas for linking.  That sort of information should be part of your build system, not your source code.  Otherwise your source code ends up tightly coupled to your current build system and hard to port later on.
« Last Edit: October 28, 2014, 08:10:00 pm by Ixrec »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Displaying more-and-more (sf::)texts slows the program down rapidly.
« Reply #2 on: October 28, 2014, 09:20:54 pm »
sf::Text isn't necessarily the cheapest thing to draw. They consist of multiple textured quads, so there's that.

However, you would have to draw a truly ludicrous amount of sf::Texts for you to notice anything really game-breaking. For all "reasonable" intents and purposes, the performance is sufficient. This might even improve in the future.

Like Ixrec already said, the reason for the poor performance is not because sf::Text is really expensive to draw, but because your test throws it into an unrealistic worst-case scenario. In typical "well-designed" games, you would not have to recreate so many sf::Texts every frame. I even go so far as to say that your test performs so slowly not because of the drawing itself, but because of the amount of memory allocations and constructor/destructor calls, so basically "standard C++ stuff". If you tested any other library this way, you would probably get similar results.

I maintain SFGUI, so I can say I've had my share of benchmarking sf::Text in the past. To make sf::Text the bottleneck of your game, you would probably have to draw so much text on your screen, I would feel sorry for the player that has to read all of it ;).
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Polyfructol

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Displaying more-and-more (sf::)texts slows the program down rapidly.
« Reply #3 on: October 29, 2014, 02:48:44 am »
All that have been said is absolutely right, but I was curious and I've made the correction by myself. So there you are:

(click to show/hide)

val

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Displaying more-and-more (sf::)texts slows the program down rapidly.
« Reply #4 on: October 29, 2014, 08:18:50 am »
Thanks guys for the replies. Didn't know that creating objects on the fly would have *this* much impact on performance. Also thanks, Ixrec, on the #pragma-ing.