Loading TrueType font into LibGDX using AssetManager class

I am trying to load a TrueType font using the libGDX AssetManager class.

This is how I initialize my variable manager

:

manager = new AssetManager();

FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));

      

I created a method that looks for files in a specified directory and then with this code .. (it's in a function that takes a path as an argument)

FreeTypeFontLoaderParameter parameters = new FreeTypeFontLoaderParameter();
parameters.fontFileName = path;
parameters.fontParameters.size = 10;
manager.load(path, BitmapFont.class, parameters);

      

... adds a font to load the queue, but when I run my application it keeps the throwing exception:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: assets/fonts\Test.ttf.gen
at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:536)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:356)
at my.app(App.java:56)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: assets/fonts\Test.ttf.gen
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:127)
at com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.loadSync(FreetypeFontLoader.java:40)
at com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.loadSync(FreetypeFontLoader.java:20)
at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:139)
at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:89)
at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:477)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354)
... 3 more

      

Line 56 in App.java looks like this:

if(!this.resourceManager.getManager().update()) {

      

I can read from the exception that a resource named "Test.ttf.gen" cannot be loaded, but I have no idea where I can get it ... I mean I have a .ttf file, and I think that's enough.

I searched google but it looks like no one has come across a similar problem.

+3


source to share


1 answer


I only had this problem on windows machines. It turns out that the class I used to store resource paths was returning strings with platform-specific path delimiters. So on Windows it was returning a forward slash path string. Internally, the LibGDX AssetManager class uses backslashes, but still loads the string with a forward slash.

The problem is that the loaded object was stored in a dictionary with a path specified as a forward slash string key. When you try to get an asset with a backslash, it cannot find the key in the dictionary.



In short, make sure your paths always use slashes with LibGDX even on Windows.

Also I can write a bug report.

0


source







All Articles