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?
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!
- FreeTypeFontGeneratorLoader (it's the same like the code above)
- FreeTypeFontLoader
- FreeTypeFontLoaderTest