0
\$\begingroup\$

I'm trying to start using AssetManager in my game. Previously I used to generate two BitmapFonts (bigger and smaller) from my .ttf file and it worked well. Here's how it looked in the code:

FreeTypeFontGenerator generatorRoboto = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Roboto-Regular.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter();
params.size = 80;
BitmapFont robotoFont = generatorRoboto.generateFont(params);
robotoFont.getData().setScale(0.5f);
robotoFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
params.size = 40;
BitmapFont smallerFont = generatorRoboto.generateFont(params);
smallerFont.getData().setScale(0.6f);
smallerFont.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, 
Texture.TextureFilter.Linear);

Then I add it to skin and use it.

skin.add("roboto-regular-font", robotoFont, BitmapFont.class);
skin.add("roboto-smaller-font", smallerFont, BitmapFont.class);

It works well, but now I'm trying to use it with AssetManager (according to documentation):

FreeTypeFontLoaderParameter robotoFontLP = new FreeTypeFontLoaderParameter();
robotoFontLP.fontFileName = "fonts/Roboto-Regular.ttf";
robotoFontLP.fontParameters.size = 80;
manager.load("fonts/Roboto-Regular.ttf", BitmapFont.class, robotoFontLP);
manager.finishLoading();

BitmapFont robotoFontBig = manager.get("fonts/Roboto-Regular.ttf", BitmapFont.class);
robotoFontBig.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
robotoFontBig.getData().setScale(0.5f);
BitmapFont robotoFontSmall = manager.get("fonts/Roboto-Regular.ttf", BitmapFont.class);
robotoFontSmall.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
robotoFontSmall.getData().setScale(0.15f);

skin.add("roboto-regular-font", robotoFontBig, BitmapFont.class);
skin.add("roboto-smaller-font", robotoFontSmall, BitmapFont.class);

But it doesn't work, LibGDX treat them as the same font. What am I doing wrong?

\$\endgroup\$
3
\$\begingroup\$

I believe this could be an issue with the AssetManager as it uses an objectMap which maps loaded objects to the string used to load them. In this case you're loading the font and the assetManager is storing it in it's objectMap under the key "fonts/Roboto-Regular.ttf", then when you load your font again with second set of parameters the previous font is overwritten.

I found this workaround from this page.

One solution would be to pass an id as the name, and also have parameters that get passed to the TTF loader that actually contains the filename.

This example shows how to use the parameter file to define font file name leaving the name in the load method to be used purely as a key. Adapted from the libgdx ttf test found here

    FreeTypeFontLoaderParameter robotoFontBigParam = new FreeTypeFontLoaderParameter();
    size1Params.fontFileName = "fonts/Roboto-Regular.ttf";
    size1Params.fontParameters.size = 40;
    manager.load("size40.ttf", BitmapFont.class, robotoFontBigParam );

    FreeTypeFontLoaderParameter robotoFontSmallParam= new FreeTypeFontLoaderParameter();
    size2Params.fontFileName = "fonts/Roboto-Regular.ttf";
    size2Params.fontParameters.size = 12;
    manager.load("size12.ttf", BitmapFont.class, robotoFontSmallParam);
\$\endgroup\$
  • \$\begingroup\$ I have no idea how to do that, could you please explain in code? \$\endgroup\$ – Divelix Sep 14 '17 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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