0

I'm using FreeTypeFontGenerator to load my fonts. Currently I have 6 different BitmapFonts and I load them in the constructor of my Screen class and draw them in the render function. Here's a code snippet, myFont is a class I've written which internally uses FreeTypeFontGenerator and the loadFont function returns a BitmapFont object.

public Screen1 () {
    .....
    .....

    myFont = new MyFont();

    circleFonts = new BitmapFont[4];
    for (int i=0; i<4; i++) {
        circleFonts[i] = myFont.loadFont("fonts/roboto/Roboto-Light.ttf", 20);
    }
    textOrColorFont = myFont.loadFont("fonts/roboto/Roboto-Light.ttf",15);
    currentColorFont = myFont.loadFont("fonts/roboto/Roboto-Light.ttf", 15);
}

The problem is since I have to load these fonts every time I move to Screen1, the game gets stuck for a good 2-3 seconds because it has to load the fonts from the file. Is there a better way to do this?

Thanks.

akshayt23
  • 2,793
  • 3
  • 21
  • 29

2 Answers2

1

maybe you want to look at this:

Libgdx FreeTypeFontGenerator with AssetManager

not only for font, you can also use it for all their asset either bitmap font, music, pictures, or anything else that supports AssetManager.

AssetManager wiki -> https://github.com/libgdx/libgdx/wiki/Managing-your-assets

I hope you help.

Community
  • 1
  • 1
Angel Angel
  • 19,670
  • 29
  • 79
  • 105
  • Thanks that link is helpful. I understand I can use the FreeTypeFontLoader with the AssetManger but I couldnt get it to work. The github link for the example showing how to use it [FreeTypeFontLoaderTest](https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/FreeTypeFontLoaderTest.java) is broken. Can you please provide me with an example of how to work with this? – akshayt23 Apr 18 '15 at 11:01
  • @user2558050 This is the alternative, new link, I hope you help https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/extensions/FreeTypeFontLoaderTest.java – Angel Angel Apr 18 '15 at 13:52
0

Well I'm not familiar with this library in particular but I had a slimier problem a couple of years ago.

As the fonts are a Resource they should be loaded at the game/level start-up.

And not while the game is already running.

Reading from the disc is an IO operation and it is quite costly, so loading all of your resources before usage (fonts sprites and so on) and then using their reference is a smarter way of handling this.

It should also be noted that doing any kind logic in the constructor is bad practice and should be avoided at any cost, the initialization phase of class should be quick.

David Limkys
  • 4,907
  • 5
  • 26
  • 38