Libgdx FreeTypeFontGenerator with AssetManager

I would like to use an asset manager in conjunction with FreeTypeFontGenerator.

I do not want to download fnt files because they are displayed differently on different screens. So what I'm doing right now is creating my fonts on the fly on every actor or screen. Now I believe that it is best to create fonts once, when the game starts, and upload them to the asset manager. But AssetManager seems to need a file name with the BitmapFont.class parameter. What I want to do is generate 5 different bitmaps and upload these BitmapFonts to the resource management agent, so I have all my resources in one place and you can reuse them. I could just create these BitmapFonts, save them to a list and provide a list to each player or screen, just like I do with an asset manager that I manage my textures and audio. But it would be more elegant to have everything in one place, an asset manager.

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

+6
source share
1 answer

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

You will have to implement either SynchronousAssetLoader or AsynchronousAssetLoader . Those would get the file to a free type font. With this, you can use the generator to generate the desired BitmapFont . Since you want to use the asset manager, you must overwrite the default bootloader for bitmap fonts, for example:

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

Through AssetLoaderParameters you can provide additional information to your loader, for example, font size.

The following code has not been verified, but may 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 downloaders for the freetype fonts themselves!

+10
source

Source: https://habr.com/ru/post/970785/


All Articles