Archives

How to Load TrueType Fonts in Libgdx

I was having some trouble with coloring Bitmap Fonts using the iOS backend of libgdx, so after a couple of days trying to fix it without solution, I decided to try using TrueType Fonts instead.

The thing was that the only way to use TTF's in libgdx is using the libgdx extension called "FreeType Fonts" included in the nightlies of libgdx, but I was having some trouble integrating it into my project, I finally managed to do it, so this post is a summary of the steps I found across the web that helped me getting it to work along with a few more tips from me that caused me some troubles during the integration.

Including FreeType In Your Game

First download the latest nightly build

Once downloaded, navigate to extensions/gdx-freetype and do the following:

  • extract gdx-freetype.jar and gdx-freetype-natives.jar to your core project's libs folder

  • link gdx-freetype.jar to your core, android and desktop project

  • link gdx-freetype-natives.jar to your desktop project

  • copy armeabi/libgdx-freetype.so to your android project's libs/armeabi folder

  • copy armeabi-v7a/libgdx-freetype.so to your android project's libs/armeabi-v7a folder

Steps To Make It Work With iOS Games

Navigate to your game-ios folder, then open your convert.properties file and add the freetype jar to the classpath and the freetype dll, the file should looks like this:

SRC = ../my-gdx-game/src/
CLASSPATH = ../my-gdx-game/libs/gdx.jar;../my-gdx-game/libs/gdx-freetype.jar
EXCLUDE   =
IN        = -r:libs/ios/gdx.dll -r:libs/ios/gdx-freetype.dll -recurse:target/*.class
OUT       = target/my-gdx-game.dll

Now you have to link your native file, the libgdx-freetype.a to your monotouch/xamarin project's libs/ folder. Then you have to add it to your monotouch/xamarin project in the solution explorer, just like you add any file to a solution.

Then open your game-ios.csproj and for each configuration (e.g. Release|IPhone, Debug|Simulator) you'll need to modify it to link to gdx-freetype in the same way it already links to libgdx.a and libikvm-natives.a

It should looks something like this:

-nosymbolstrip -nostrip -cxx -gcc_flags "-L${ProjectDir}/libs/ios -L${ProjectDir}/libs/ios/ikvm/bin -likvm-natives -lgdx -lgdx-freetype -force_load ${ProjectDir}/libs/ios/libgdx.a -force_load ${ProjectDir}/libs/ios/ikvm/bin/libikvm-natives.a -force_load ${ProjectDir}/libs/libgdx-freetype.a"

Finally, you have to open your project, right click the “References” entry in your project, select Edit References, then go to “.Net Assemblies” and find the gdx-freetype.dll. Click the add button so it appears on the right sidebar which shows you which assemblies are currently referenced.

How to use it

Now remember to include the TTF file inside your data or assets folders on both game-android and game-ios projects, and also remember to change the build action inside Monotouch/Xamarin of the TFF file to Content in order to use it inside of the game.

Here is a little example on how to use the extension:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/myfont.ttf"));
BitmapFont font12 = generator.generateFont(12); // font size 12 pixels
BitmapFont font25 = generator.generateFont(25); // font size 25 pixels
generator.dispose(); // don't forget to dispose to avoid memory leaks!

And that's it, now you can use TrueType Fonts in your libgdx game, and manage their size as much as you want!

Here are the links of the sites I used to assemble this post:

GdxFreeType

Gdx FreeType Supported on iOS

Installing a Jar in Maven local repository

You have to run the following command in the terminal:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

where:

path-to-file: is the path to the jar you want to install in the local repository

group-id: the group in the maven repository you want to use

artifact-id: the name that the jar will receive when you want to use it in a pom.xml

version: the version of the jar/library

packaging: the type of package it is, usually is a jar

Using Android Library Projects with Maven

Ok so I've decided to start with a new blog (again) and to make a good first post I'm going to explain how to use Android Library Projects integrated with Maven.

Your Android project has to have the android maven plugin in its pom.xml file in order to unpack the android library project that you are going to package as an apklib.

The first thing you have to make is go to the root directory of your Android Library Project, compress it all in a zip file, and rename to the extension apklib

The files included in this gist are the maven script to convert your new apklib file into a dependency in your maven repo and the second one is the structure to add the dependency on the android pom.xml file.

This is the script to generate the apklib in your local maven repository

mvn install:install-file \
  -DgroupId=org.abc \
  -DartifactId=mywidget \
  -Dpackaging=apklib \
  -Dversion=1.0.0 \
  -Dfile=mylib.apklib \
  -DgeneratePom=true

and this is how you add the dependency in your pom.xml

 <dependency>
    <groupId>org.abc</groupId>
    <artifactId>mywidget</artifactId>
    <version>1.0.0</version>    
    <type>mylib.apklib</type>
</dependency>