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

Author Topic: Question about Displaying Text  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Question about Displaying Text
« 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).

Ahlywog

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Question about Displaying Text
« Reply #1 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.

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Question about Displaying Text
« Reply #2 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Question about Displaying Text
« Reply #3 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).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Question about Displaying Text
« Reply #4 on: December 21, 2012, 07:09:39 pm »
Creating a static sf::Text will gain some performance?
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Question about Displaying Text
« Reply #5 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.
« Last Edit: December 21, 2012, 07:45:29 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Question about Displaying Text
« Reply #6 on: December 21, 2012, 09:16:18 pm »
Thanks for all the responses! Everyone was very helpful   :D

 

anything