Libgdx FreeTypeFontGenerator with AssetManager
Asked Answered
U

1

7

i'd like to use the asset manager in combination with the FreeTypeFontGenerator.

I dont want to load fnt files cause they display differently on different screenresolutions. So what i do currently is generating my fonts on the fly in every actor or screen. Now i think its best to generate the fonts once when the game is started and load them into the asset manager. But the AssetManager seems to need a filename with the BitmapFont.class parameter. What i want to do is generate 5 different bitmapfonts and and load those BitmapFonts into the assetmanager, so i have all my ressources in one place, and can reuse them. I could just create those BitmapFonts, save them in a list, and give the list to every actor or screen, just like i do with the assetmanager i manage my textures and audio with. But it would be more elegant to have everything in one place, the asset manager.

So, is there a way to load BitmapFonts created with FreeTypeFontGenerator into the assetmanager?

Unclassified answered 12/6, 2014 at 23:37 Comment(0)
O
11

Here you can read about how to supply your own AssetLoader.

You would have to implement either a SynchronousAssetLoader or an AsynchronousAssetLoader. Those Would get the file to a free type font. With that you can use the generator to generate your desired BitmapFont. Since you want to use the asset manager, you have to overwrite the default loader for bitmap fonts like this:

manager.setLoader(BitmapFont.class, new MyFreeTypeFontLoader(new InternalFileHandleResolver()));

Via AssetLoaderParameters you could supply further information to your loader, like the font size.

The following code is untested, but might work:

public class FreeTypeFontLoader extends SynchronousAssetLoader<BitmapFont, FreeTypeFontLoader.FreeTypeFontParameters> {

    public FreeTypeFontLoader(FileHandleResolver resolver) {
        super(resolver);
    }

    @Override
    public BitmapFont load(AssetManager assetManager, String fileName, FileHandle file, FreeTypeFontParameters parameter) {
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(file);

        return generator.generateFont(parameter.fontParameters);
    }

    static public class FreeTypeFontParameters extends AssetLoaderParameters<BitmapFont> {

        public FreeTypeFontParameter fontParameters;
    }

    @Override
    public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, FreeTypeFontParameters parameter) {
        return null;
    }

}

UPDATE:

This is no longer necessary, the gdx-freetype extension now has loaders for freetype fonts itself!

Owens answered 13/6, 2014 at 8:8 Comment(6)
Thx for this. I currently dont get everything in the code, but the main reason i like to do this, is , so i can dispose() with a simple assetmanager.clear() in my pause() method. Now i'm not sure what the assetmanager disposes if i implement this loader. Just the generator(s) ? Just the bitmap fonts ? Or both ?Unclassified
It would dispose the BitmapFonts. You should probably add the FreeTypeFontGenerator to the get dependencies as well, so it would get disposed as well when the last bitmap font gets disposed. However, I think it's not possible to actually GET the BitmapFonts like this, since there is no way to specificy the parameters when GETting them. Only on load. It might be better to implement a FreeTypeFontGeneratorLoader to just manage the generator via the asset manager, but then you still need to dispose all BitmapFonts yourself.Owens
Maybe there is a combination of both possible, but I'll need to try that at home myself, since right now I'm at work.Owens
Ok, thanks. I guess ill just dispose the fonts in every actor and screen than. Just holding a FreeTypeFontGenerator in the assetmanager doesnt win me too much, cause i can just dispose it on fly after creating the bitmapfonts in the actors. My main goal would have been to save actual bitmap fonts with different sizes there, to reuse and dispose in one place.Unclassified
Can you please give a reference as too where you found out LibGDX has loaders?Mccue
@iLoveUnicorns Sure, I added links to the code. I added the generator loader myself as a PR. badlogic/mario added the one for the fonts themselves just recently.Owens

© 2022 - 2024 — McMap. All rights reserved.