8
\$\begingroup\$

Won't it slow my game if I use FreeType lib and convert 2 fonts from .ttf instead of using preloaded BitmapFont .png? Or may be FreeType so fast, that I can use fonts both ways? All I need is to change color and size of my text. P.S. I use 3 fonts in my project

| improve this question | | | | |
\$\endgroup\$
  • \$\begingroup\$ Voting to close, as answers will be entirely opinion based. What's "too slow" will always be subjective to the application. Decoding a freetype font 60 times a second will probably be too slow, while loading the font once at startup will probably make no difference, but anything in between will always depend on the application, the font and everything else. Loading the png may even take more time than loading the font. \$\endgroup\$ – Riot May 6 '17 at 22:03
  • \$\begingroup\$ @Riot, so to answer "will it slow my application", your saying "yes". I don't see any requirement for "too" slow, here. VTLO. \$\endgroup\$ – Gnemlock May 7 '17 at 0:17
  • \$\begingroup\$ I'm not saying "it will slow your application", though. Loading a font takes time, but that can be done once, costing you a one-off 5ms at the start and that's it - or it can cost you a ton of time if you keep loading new fonts, it depends entirely on how you're implementing it. Loading a png costs you time, too. Anything your application does will "slow it" from a logical point of view. There can be no general answers here. \$\endgroup\$ – Riot May 7 '17 at 12:13
13
\$\begingroup\$

If you use a font rendering engine like FreeType, then the most common mistake is to re-render strings every frame. This is pointless, because when you render a string, it will usually be on the screen unchanged for at least a few seconds.

When you want to display a text, render that text to a texture once and then draw that texture instead of re-rendering the text. When you do this, the cost of text-rendering will only apply the first time you need to render a given string with given settings. On any subsequent frame, the cost of displaying that string is the same as for drawing any other sprite of that size.

A solution which I found quite handy in various projects was to have a TextRenderingCache class to manage cached font renderings. It usually has a hash table with string plus render settings (font, color, size, etc.) as key and a texture with the rendering as value. When a given rendering is requested from the TextRenderingCache class, it checks if it already has that rendering, and if not it creates one. Dropping renderings from the hash-map which were not used for several seconds to avoid memory leakage is optional (some games need this, some do not).

By the way: FreeType already comes with a rendering cache system. But it is only used when you use it explicitly.

| improve this answer | | | | |
\$\endgroup\$
4
\$\begingroup\$

Yes, Freetype is reasonable. If you use .png files, you have to make one for EVERY size and EVERY color, and that may occupy 10 times the space a .ttf file takes up. Goodbye, free space on android and iOS devices. Moreover, you just have to get 1 .ttf file, and that's it. Hope I helped you!

| improve this answer | | | | |
\$\endgroup\$
  • 2
    \$\begingroup\$ You can use SDF and shaders - one bitmap for each font for all sizes and color. Yet again, performance of Freetype probably should be the last thing one should consider optimizing. \$\endgroup\$ – wondra May 6 '17 at 11:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.