2
votes

I'm using gdx-freetype library in android to generate BitmapFont from TrueType font which is in assets/fonts/arial.ttf This is the way I use to show some text on screen

generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/arial.ttf"));
font30 = generator.generateFont(60 , "ConectigTsrv" , false);
generator.dispose();    
Label l = new Label("Connecting to server...", new LabelStyle(font30 , Color.BLUE));
l.setX(400 - l.getWidth()/2f);
l.setY(480 - l.getHeight() - 10);
stage.addActor(l);

text will be printed to screen very well

enter image description here

But after a while or when I push Home button and then come back to application all characters turn to black rectangles

enter image description here

Any idea about whats going on?

2
I would do a debug to see exactly what happens when you press any of the buttonsPontus Backlund
I suppose it's bitmap's faultsupport_ms

2 Answers

4
votes

The problem is that the textures created by FreeTypeFontGenerator were not managed until this recent commit. The fact that textures are unmanaged means that they have to be reloaded following a loss of the OpenGL context, which occurs in scenarios such as the one you described.

If you upgrade libgdx to the latest nightlies then the problem will probably go away.

For more information, the problem with unmanaged textures is described very well in this article.

1
votes

In case anyone is still having this problem. Using LibGDX 1.9.2, I had this problem as well: Run the game, navigate 'back' to Android home screen, go back to the game and fonts would be black rectangles.

Turned out I was loading all textures in a static way, which only loads them once at game start and never again:

//THIS IS WRONG
public class Styles {
    public static final BitmapFont HEADER_FONT;
    public static final FreeTypeFontGenerator _freeTypeFontGenerator = ...

    static {
        FreeTypeFontGenerator.FreeTypeFontParameter params = ...
        HEADER_FONT = freeTypeFontGenerator.generateFont(params);
    }
}

This causes trouble when the game is reloaded in memory. As far as I understand, the final fields now refer to non-existent texture data. To fix that, I got rid of the final properties and load them in the create() function, recreating all assets each time the game is reloaded in memory:

public void onCreate() {
    Styles.loadAssets();
}

And in Styles:

//STATIC RESOURCES CAN CAUSE TROUBLE, KEEP IT IN MIND
public class Styles {
    public static BitmapFont HEADER_FONT;
    public static FreeTypeFontGenerator FONT_GENERATOR = ...

    public static void loadAssets() {
        FreeTypeFontGenerator.FreeTypeFontParameter params = ...
        HEADER_FONT = FONT_GENERATOR.generateFont(params);
    }
}

I prefer my read-only assets to be static to be memory friendly. However, the use of static resources may still create problems I'm not aware of, as per the manual.