1

I am trying to use BitmapFont from file 1.ttf (LibGdx application for android). However wherever I put this file, I obtain an error that it is not found. Here is my code:

BitmapFont font = new BitmapFont(Gdx.files.internal("1.ttf"), false);

Where should I put this font file? Or what should I change?

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
quarandoo
  • 369
  • 4
  • 9

3 Answers3

5

You can't create BitmapFont directly using .ttf file. You need to use Gdx freetype in your project.

Since gdx-freetype is an extension, it is not included in your LibGDX project by default so put gdx-freetype in your project. If your project using gradle for dependency management, add required artifacts in your root build.gradle file.

After injection of required dependency, keep your .ttf file inside assets folder of android module, that will share with other module using config.

Generate BitmapFont using FreeTypeFontGenerator in this way :

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("1.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12;
BitmapFont font12 = generator.generateFont(parameter); // font size 12 pixels
generator.dispose(); // don't forget to dispose to avoid memory leaks!
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • 1
    The important part that helped me was to put the font or font directory in the "assets" folder AND NOT the "res" folder! thx :) – Willi Mentzel Feb 21 '19 at 21:58
0

You should place the file in android/assets. To run the desktop version from Android Studio, you will need to modify the run configuration as described in this answer.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

You can use Hiero gdx tool: https://github.com/libgdx/libgdx/wiki/Hiero runnable jar is here: runnable-hiero.

Open this jar, setup font you want and save it as BitmapFont File. You should put it somewhere in {PROJECT_FOLDER}/android/assets folder. Now, when you have your .fnt font file, you can load BitmapFont as follow:

BitmapFont font = new BitmapFont(Gdx.files.internal("your_font_file.fnt"));

Remember to dispose it when you do not need it anymore or when app exit, ex:

public class App implements ApplicationListener {
    BitmapFont font;

    //...
    @Override
    public void dispose()
    {
        font.dispose();
    }
}
mk_
  • 179
  • 1
  • 4