Search code examples
javaopenglunicodefontsdrawstring

How do I use a custom font with Slick's UnicodeFont class?


I am using this code, which was snipped and modified from the Slick Wiki Unicode tutorial:

UnicodeFont menuFont = new UnicodeFont("/fonts/mailrays.ttf", 8, false, false);

menuFont.addAsciiGlyphs();
menuFont.addGlyphs(400, 600);
menuFont.getEffects().add(new ColorEffect());
menuFont.loadGlyphs();

menuFont.drawString(25.0f, -80.0f, "Snake", new Color(1.0f, 1.0f, 1.0f));

But when the string is drawn on the screen it uses a low-res console font, what I assume is the default. What could be preventing the string from using the font I passed into the constructor (mailrays.tff)?


Solution

  • I know this is almost two months old, but in case you are still looking for the answer...

    If Slick can't locate your font, you'd get a RuntimeException when you try to instantiate the font. To verify this, try specifying the name of a font you know doesn't exist on your machine ("crap.ttf", for example)... You'll most likely see something like:

    Exception in thread "main" java.lang.RuntimeException: Resource not found: crap.ttf at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69) at org.newdawn.slick.UnicodeFont.createFont(UnicodeFont.java:61)

    Given that, the issue here is probably the size of the font you are creating. Trying to create an 8 point font will cause mayhem for many TTF fonts, and the result might look low-res as you described. To verify this, try creating the font in a larger size, perhaps 12 point or so. This will obviously be bigger than what you want, but at least it will verify that you are loading the proper font and that the issue is just scaling. If that is the case, you'll just have to find a font that scales better in the 8 point range.

    Hope this helps!