2
\$\begingroup\$

I'm trying to load a font in libGDX. But I can't figure out want I do wrong.

Code:

white = new BitmapFont(Gdx.files.internal("fonts/white.font"), false);

Error:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: fonts/white.font
    at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:657)
    at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:469)
    at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:115)
    at scenes.GameMode.create(GameMode.java:61)
    at scenes.GameMode.<init>(GameMode.java:45)
    at com.circle.game.Circle.create(Circle.java:17)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1931)
    at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:480)
    ... 7 more
:desktop:run

Files

\$\endgroup\$
  • \$\begingroup\$ The error says "File not found: font/white.font". Your directory seems to be called "fonts", not "font". \$\endgroup\$ – Anko Oct 12 '15 at 18:47
  • \$\begingroup\$ Ok that was an Error but not the Error. I'm still getting the error :( com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: fonts/white.font \$\endgroup\$ – Kevin Oct 12 '15 at 18:57
  • \$\begingroup\$ That is a very different error. It sounds like the file is now found, but can't be loaded for another reason. Please edit the question accordingly. \$\endgroup\$ – Anko Oct 12 '15 at 19:16
  • \$\begingroup\$ Since you also have a white.png file, that makes me think you want to use the three-argument BitmapFont constructor that takes a font file (specifying glyph positions) as well as an image file (containing the actual glyph images). Could that be it? \$\endgroup\$ – Anko Oct 12 '15 at 19:37
0
\$\begingroup\$

With LibGDX, the user.dir property is used for the Gdx.files.internal(...) root directory. This property is, unless explicitly changed, set to the directory from which the app was launched.

With that being said, you should move your assets folder to your target platform's root dir (desktop for Desktop, android for Android, etc.).

Also, after the first step (Whatever it may be, you should use assets/fonts/white.font rather than fonts/white.font, as LibGDX does not automatically use the assets folder.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Everything is correct.. But Have you set the working directory of the desktop project? If not set working directory of desktop project to Android/assets Else, it is the error caused by the file itself which tool did you use to generate the font file? Use a tool called BMFont

|improve this answer|||||
\$\endgroup\$

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.