2
votes

i have trouble having scale font in libgdx. I want use 1 file .fnt to 3 different font scale. But the result the font still have same scale whatever i scale them 1 by 1 .

this is my code, i'm using AssetManager.

// this is I load the assetsFont
// Settings.fileSilverBoldFont is font location.

// load font
manager.load(Settings.fileSilverBoldFont, BitmapFont.class);
BitmapFont defaultFont = manager.get(Settings.fileSilverBoldFont, BitmapFont.class);

// edited, old -> defaultSmall = defaultFont; (reference object, my fault)
BitmapFont defaultSmall= new BitmapFont(defaultFont.getData(), defaultFont.getRegion(), true);
BitmapFont defaultNormal= defaultFont.getCache().getFont();
BitmapFont defaultBig= new BitmapFont(defaultFont.getData(), defaultFont.getRegion(), false);;

// set font sizes
defaultSmall.setScale(0.75f / Settings.widthRatio, 0.75f / Settings.heightRatio);
defaultNormal.setScale(1.0f / Settings.widthRatio, 1.0f / Settings.heightRatio);
defaultBig.setScale(2.0f / Settings.widthRatio, 2.0f / Settings.heightRatio);

// enable linear texture filtering for smooth fonts
defaultSmall.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
defaultNormal.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
defaultBig.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);

Then i fill my font to my game controller initFont().

public void initFont(){
    distanceFont = new BitmapFontCache(Assets.assetFont.defaultBig).getFont();
    scoreFont = new BitmapFontCache(Assets.assetFont.defaultSmall).getFont();
}

At last i Render my font to screen in render(), I call game controller using variable controller.

public void render(float delta){
    controller.scoreFont.draw(hudBatch, controller.scoreText, 160/Settings.widthRatio, hudCam.viewportHeight - (40/ Settings.heightRatio));
    controller.distanceFont.draw(hudBatch, controller.distanceText, hudCam.viewportWidth/2, hudCam.viewportHeight - (40/ Settings.heightRatio));
}

I want to have scoreFont draw Small and distanceFont draw big font. but the result 2 of them still have same size. How can i have my scoreFont draw with small font and distanceFont draw with big font ?

found about reference object in java, now i'm change using new object of it, and it still same, I can't have different size of font.

Okey now i found, I must load using BitmapFont directly multiple times, this is solution what i do. I don't know how to use AssetManager properly to do that, maybe this is enough for this case.

// create three fonts using Libgdx's built-in 15px bitmap font
silverBoldSmall = new BitmapFont(Gdx.files.internal(Settings.fileSilverBoldFont));
silverBoldNormal = new BitmapFont(Gdx.files.internal(Settings.fileSilverBoldFont));
silverBoldBig = new BitmapFont(Gdx.files.internal(Settings.fileSilverBoldFont));

// set font sizes
silverBoldSmall.setScale(2f / Settings.widthRatio, 2f / Settings.heightRatio);
silverBoldNormal.setScale(3f / Settings.widthRatio, 3f / Settings.heightRatio);
silverBoldBig.setScale(4f / Settings.widthRatio, 4f / Settings.heightRatio);
2

2 Answers

1
votes

You've only got one font instance.

BitmapFont defaultSmall= defaultFont;
BitmapFont defaultNormal= defaultFont;
BitmapFont defaultBig= defaultFont;

Then you change the scale three times on the same font...

defaultSmall.setScale(0.75f / Settings.widthRatio, 0.75f / Settings.heightRatio);
defaultNormal.setScale(1.0f / Settings.widthRatio, 1.0f / Settings.heightRatio);
defaultBig.setScale(2.0f / Settings.widthRatio, 2.0f / Settings.heightRatio);

If pointing this out doesn't make sense then you read up on how references work in Java.

I must load using BitmapFont directly multiple times, maybe this is enough for this case.

// create three fonts using Libgdx's built-in 15px bitmap font
BitmapFont defaultSmall= new BitmapFont(Gdx.files.internal(Settings.fileSilverBoldFont));
BitmapFont defaultNormal= new BitmapFont(Gdx.files.internal(Settings.fileSilverBoldFont));
BitmapFont defaultBig= new BitmapFont(Gdx.files.internal(Settings.fileSilverBoldFont));
1
votes

I know this is old, but this my be relevant to people browsing still.

If you are grabbing your font from a skin (ex. font = skin.getFont("fontName")), a solution is to modify your json file and define multiple fonts which all point to the same font in your atlas.

In my case this looks like this

com.badlogic.gdx.graphics.g2d.BitmapFont: {
    HelveticaNeue-Light: {
        file: HelveticaNeue-Light.fnt
    }
    HelveticaNeue-Light2: {
        file: HelveticaNeue-Light.fnt
    }
}

And then when grabbing the fonts from your skin, you can access the multiple different ones which will create separate instances.