# SFML: Optimizing sf::Text?

I've been noticing that using multiple sf::Text objects has a huge impact on performance.

The difference between using 1 or 4 sf::Text objects can result in almost a 40FPS drop (From 120 to 80).

ExampleText = new sf::Text();
ExampleText->setFont(*ResourceManager::GetInstance()->GetAceFont());
ExampleText->setColor(sf::Color::White);
ExampleText->setCharacterSize(36);


Am I not making them right? If somebody could even explain why this happens as the docs tell me sf::Text is really lightweight.

Any help would be appreciated.

• What do you mean with "using"? Rendering? Updating properties? Creating every frame? – Tyyppi_77 Aug 7 '16 at 20:33
• Nothing more than drawing. – GigaNova Aug 7 '16 at 20:40
• You should use a profiler and narrow down where the problem comes from. – Vaillancourt Aug 8 '16 at 1:34

I don't think your issue is necessarily with sf::Text -- I wrote up a small program to draw multiple sf::Texts and record the frames per second for 40 seconds, and got these results:

Drawing 1 sf::Text: Average 738.7 FPS
Drawing 4 sf::Texts: Average 735 FPS

That's a slight difference, but nowhere near your 40 FPS drop; you may want to double-check that nothing was changed other than the number of Text items drawn.

My program doesn't do anything special with the text items -- other than FPS counting, all it does is draw either 1 or all 4 of the sf::Text objects declared outside of the program loop (I could show the code if you want) -- which leads me to believe that the issue likely has something to do with how your program handles drawing & manipulating the Text objects.

I don't see anything particularly suspicious in the extract of code your post shows (assuming there's a reason you're creating the Text with new). It might help to be able to see where you actually draw them.

So, without knowledge of your code, there are a couple of potential causes that I can think of:

• For some reason, your text objects are being recreated each frame, or perhaps are resetting some of their data which needs to be drawn. The former could cause slight performance loss on its own, but both would require new vertex data to be sent to the GPU each frame, which can (potentially) be a performance loss more noticeable with more things to be drawn.

• Your draw code alternates between drawing various different textures (drawing text between drawing other objects?). Changing the active texture when drawing is not a cheap OpenGL operation. SFML uses OpenGL, and thus is partially vulnerable to this.

• Using multiple sf::Fonts alongside multiple sf::Texts. The sf::Font class is more computationally expensive than sf::Texts to use.

• Your program is simple enough that quadrupling the number of drawn texts is actually a pretty large increase in computational load.

Edit: How many frames per second do you get if you don't draw any of the sf::Text objects?

• Only thing I can think of is number 2, I do draw quite the number of sprites and text over them. I normally run at 5600 fps and I kid you not, it drops to about 900-1000 when drawing multiple sf::Text objects. – GigaNova Aug 8 '16 at 7:55
• @GigaNova Are you drawing like sprite-text-sprite-text-sprite-text or sprite-sprite-sprite-text-text-text? The latter is generally better, if possible, grouping together objects using the same texture, as to minimize the number of times you need to change the active texture. (Though that still does seem like a pretty hefty drop in FPS unless you are drawing a lot of different things.) – Brenn_ Aug 8 '16 at 14:10

The problem was that the x and/or y position wasn't rounded, and apparently that causes the text to blur.

Also, it is possible the font has fixed sizes where it's sharp, and might not render good at different sizes. Example of this was one font that was only sharp at 16px and 24px, so keep that in mind as well.

• What? What has blurriness have to do with performance issues? – Tyyppi_77 Nov 28 '16 at 9:48