Does anyone know how I can use a TTF font in libGDX? I have looked around and have seen things about StbTrueTypeFont but it doesn't seem to be in the latest release.
EDIT: I found the StbTrueType font stuff, the jar file is located in the extensions directory. I've added it to my project. Now I just need to figure out how to use it. Any examples?
Yes you will definitely need to add the gdx-stb-truetype
jars to your project as you stated in your edit. Here is how you will use it, pretty straighforward...
First you need to declare your BitmapFont
and the characters you will use...
BitmapFont font;
public static final String FONT_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789][_!$%#@|\\/?-+=()*&.;,{}\"´`'<>";
Then you need to create the font...
font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("font.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font.setColor(1f, 0f, 0f, 1f);
You can play with the arguments you pass to createBitmapFont()
and you will see what they do.
Then to render the font you would do it as you normally do...
batch.begin();
font.draw(font, "This is some text", 10, 10);
batch.end();
Use the gdx-freetype extension:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
BitmapFont font15 = generator.generateData(15);
BitmapFont font22 = generator.generateData(22);
generator.dispose();
"To use gdx-freetype, grab the latest nightlies, link gdx-freetype.jar
and gdx-freetype-natives.jar
to your desktop project, link gdx-freetype.jar
to your Android project, and copy the armeabi/libgdx-freetype.so
and armeabi-v7a/libgdx-freetype.so
files to your Android project’s libs/
folder, just like with the libgdx.so
files."
Source: http://www.badlogicgames.com/wordpress/?p=2300
Did researched a lot and found this working method:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12; // font size
BitmapFont font12 = generator.generateFont(parameter);
generator.dispose(); // avoid memory leaks, important
Use this if you failed tried answers above,
libGDX Freetype Wiki for reference.
This is working on an S4 clone phone:
Pinewood is a downloaded font, in assets folder. See the folder structure below.
import android.util.Log;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
public class Game implements ApplicationListener {
private SpriteBatch mSpriteBatch;
private Texture textureBackground;
private BitmapFont mBitmapFont;
public void create() {
Gdx.graphics.setContinuousRendering(false);
Gdx.graphics.requestRendering();
mSpriteBatch = new SpriteBatch();
Texture.setEnforcePotImages(false);
textureBackground = new Texture(Gdx.files.internal("background.jpg"));
FileHandle fontFile = Gdx.files.internal("Pinewood.ttf");
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
mBitmapFont = generator.generateFont(150);
generator.dispose();
mBitmapFont.setColor(0.9f, 0.5f, 0.5f, 1);
Log.e("Game", "mBitmapFont.getScaleX() : "+mBitmapFont.getScaleX() + ", mBitmapFont.getScaleY() "+mBitmapFont.getScaleY());
}
public void render() {
Log.e("Game", "render()");
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
mSpriteBatch.begin();
// Drawing goes here!
mSpriteBatch.draw(textureBackground, 0, 0);
mBitmapFont.draw(mSpriteBatch, "FPS:"+Gdx.graphics.getFramesPerSecond(), 110, 260);
mSpriteBatch.end();
}
public void resize(int width, int height) {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}