Search code examples
javafontslibgdxtruetype

Exception by loading TTF font into AssetManager


Follwing this code example: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/extensions/FreeTypeFontLoaderTest.java

I'm trying to load one single ttf file into the AssetManager with different parameters and different filenames.

public class LoadingScreen implements Screen {

    private final GdxAsternets game;

    private float progress;

    public LoadingScreen(final GdxAsternets game){
        this.game = game;

        game.bitmapFont = new BitmapFont(Gdx.files.internal("font/myFont.fnt"));

        queueAssets();
    }

    @Override
    public void show() {
        progress = 0f;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        update(delta);
    }

    private void update(float delta){
        progress = MathUtils.lerp(progress, game.assetManager.getProgress(), 0.1f);
        if(game.assetManager.update() && progress >= game.assetManager.getProgress()- 0.1f){
            game.setScreen(game.menuScreen);
        }
    }

    private void queueAssets(){
        //Fontloader/Filehandler
        FileHandleResolver resolver = new InternalFileHandleResolver();
        game.assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
        game.assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));

        //load timer font
        FreetypeFontLoader.FreeTypeFontLoaderParameter smallFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 40;
        game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);

        //load game msg font
        FreetypeFontLoader.FreeTypeFontLoaderParameter bigFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 85;
        game.assetManager.load("bigFont.ttf", BitmapFont.class, bigFontParams);
    }
    /**Overring methods...*/
}

Look up at the method queueAssets(). Quoting the link above me:"The names of the fonts are arbitrary and are not pointing to a file disk". I've named the generated fonts "bigFont.ttf" and "smallFont.ttf" but it fails to load the assets. The problem here does not lie on the wrong file path of the ttf font. I've made use of

game.assetManager.load("font/Montserrat-SemiBold.ttf", BitmapFont.class, smallFontParams);

instead of

game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);

before and it seems to work fine doing so which is contradictory to the statement above.

An exception is thrown at the update(float delta) method whereas game.assetManager.update() is being called.

Exception:

GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: null (Internal)
at com.bitbench.asternets.Screen.LoadingScreen.update(LoadingScreen.java:84)
at com.bitbench.asternets.Screen.LoadingScreen.render(LoadingScreen.java:67)

Solution

  • It took me days to figure out the problem but here is the answer for people who also face the same problem:

        FreetypeFontLoader.FreeTypeFontLoaderParameter smallFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 40;
        game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);
    
        FreetypeFontLoader.FreeTypeFontLoaderParameter bigFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 85;
        game.assetManager.load("bigFont.ttf", BitmapFont.class, bigFontParams);
    

    Using the same FreeTypeFontLoaderParameter and changing it's parameter to create a font will lead you to an exception. So use different FreeTypeFontLoaderParameter in order to create fonts with different parameters! The correct code should be:

        FreetypeFontLoader.FreeTypeFontLoaderParameter smallFontParams = new 
        FreetypeFontLoader.FreeTypeFontLoaderParameter();
        smallFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        smallFontParams.fontParameters.size = 40;
        game.assetManager.load("smallFont.ttf", BitmapFont.class, smallFontParams);
    
        FreetypeFontLoader.FreeTypeFontLoaderParameter bigFontParams = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
        bigFontParams.fontFileName = "font/Montserrat-SemiBold.ttf";
        bigFontParams.fontParameters.size = 85;
        game.assetManager.load("bigFont.ttf", BitmapFont.class, bigFontParams);