# With LibGDX how is it okay to use Fonts statically?

I am working on a engine/framework/game in LibGDX as a baseline for future Java games (really lightweight and stuff, just gamestates, entity framwork, physics, font, etc), but I am unsure of how I should handle font.

Should I create a Font Factory to generate fonts, manage them, and allow static access to fonts? Or should I just create fonts when I need them, like in my HUD/GUI/Player/Level/etc.

• Is there some reason that you can't use the AssetManager for this? Loading and retrieving a font is as simple as giving the Assetmanager the path to your font resource. – spectacularbob Jun 14 '16 at 19:01
• Oh, for some reason I did not think of that. Thank you. – Meeesh Jun 15 '16 at 19:56

Just do one thing you can change the method which your following now. you can use the "hiero" tool for the font creation . Just download "hiero" jar file from the following link

by using hiero you will be able to access almost all the fonts . after creating the fonts save it on your android project files "asset" folder and add the folowing code.

 private BitmapFont font;
font = new BitmapFont(Gdx.files.internal("myNewFontCreatedByHiero.fnt"));


this is the simple method to create the fonts . which you can dispose after the usage although it doesn't required any additional codes too . "hiero" is a simple jar file which helpfull to create the different fonts for the libgdx. Later you can dispose the font

     font.dispose();


• can anybody explain me that how can you give the negative vote for the above answer? – Gokul Sreenivasan Jun 21 '16 at 14:37
• Maybe they realized that you copy-pasted an answer that you wrote on another question and felt it was very lazy from you. If you feel that it's the same question, flag the question as duplicate and don't answer it. – Vaillancourt Jun 21 '16 at 15:05
• ya i copy pasted from my another answer . but both the questions answer is same . but i given more explanation too . eg. there is in;t any difference in the answer of "1+2" and the "2+1", both are the same question and which have the same answer but it's asked in the different way . instead of understanding that giving negative vote is not fair – Gokul Sreenivasan Jun 21 '16 at 15:14
• Don't let it get you down :) Keep on answering other questions (but don't copy paste :P) (I'm not the one who downvoted). – Vaillancourt Jun 21 '16 at 15:28

To answer your question; it should be absolutely fine to have static fonts. As long as you remember to dispose of them when you exit the game.

What I would recommend though is using the AssetManager (like spectacularbob mentioned in his comment). In reality you should probably use the AssetManager for all of your assets (music, textures, atlases, fonts etc.), because it simplifies the process of disposing of assets by only having to dispose of the AssetManager instead of every single asset, and it makes it easy to load assets on-demand from AssetDescriptors (look at this official tutorial).

To show how AssetDescriptors could be implemented in a static way, take a look at this snippet from one of my projects:

public static class Assets {
////////// Skins //////////
public static final AssetDescriptor<Skin> SKIN = new AssetDescriptor<Skin>("skins/default.json", Skin.class);

////////// Textures //////////
public static final AssetDescriptor<Texture> LOGO = new AssetDescriptor<>("graphics/meta/logo.png",
Texture.class);
public static final AssetDescriptor<Texture> TEST_TEXTURE_BLOCK = new AssetDescriptor<>(
"graphics/block/sample.png", Texture.class);

////////// Fonts //////////
// In this project I was using FreeTypeFont, but using normal BitmapFonts is fine as well!
public static FreeTypeFontGenerator FREETYPE_GENERATOR = new FreeTypeFontGenerator(
Gdx.files.internal("fonts/bricks.ttf"));

////////// Music //////////
public static final AssetDescriptor<Music> INTRO_MUSIC = new AssetDescriptor<Music>("audio/music/intro.mp3",
Music.class);

}


Then you can load them using AssetManager#get(AssetDescriptor<T> assetDescriptor):

assetManager.get(Assets.LOGO); //Or whatever asset you want.


No, native resources like fonts should not be static, especially on Android this can cause lots of problems. I would suggest using the singleton pattern to create a class that manages such resources and also properly dispose of them.