2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – spectacularbob Jun 14 '16 at 19:01
  • \$\begingroup\$ Oh, for some reason I did not think of that. Thank you. \$\endgroup\$ – Meeesh Jun 15 '16 at 19:56
0
\$\begingroup\$

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

https://libgdx.badlogicgames.com/tools.html

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();

in your dispose method

\$\endgroup\$
  • \$\begingroup\$ can anybody explain me that how can you give the negative vote for the above answer? \$\endgroup\$ – Gokul Sreenivasan Jun 21 '16 at 14:37
  • \$\begingroup\$ 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. \$\endgroup\$ – Vaillancourt Jun 21 '16 at 15:05
  • \$\begingroup\$ 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 \$\endgroup\$ – Gokul Sreenivasan Jun 21 '16 at 15:14
  • \$\begingroup\$ 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). \$\endgroup\$ – Vaillancourt Jun 21 '16 at 15:28
0
\$\begingroup\$

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);

    ////////// SHADERS //////////
    public static final ShaderProgram DEFAULT_SHADER = SpriteBatch.createDefaultShader();
}

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

assetManager.get(Assets.LOGO); //Or whatever asset you want.
\$\endgroup\$
0
\$\begingroup\$

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.

You can read more about this subject on the Lib GDX wiki: https://github.com/libgdx/libgdx/wiki/Memory-management

\$\endgroup\$
  • \$\begingroup\$ Why was my answer down voted? Everything I said was accurate and exactly as it is specified in the official Lib GDX wiki. Quote "Caution: don't make your AssetManager or any other resources (like Texture, etc.) static, unless you properly manage them.". Reference: github.com/libgdx/libgdx/wiki/Managing-your-assets \$\endgroup\$ – BananyaDev May 23 '17 at 19:27
  • \$\begingroup\$ I'm guessing your answer was down-voted because you are trying to solve the issue of context loss of static resources by using another static resource. Reading up on the reason why static resources are bad on Android is needed here. \$\endgroup\$ – Charanor Jan 8 '18 at 21:08

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.