Slick2D + Fonts = CRAZY THING

Hi guys! This is my first thread on this awesome forum. I have been reading a lot of threads, learning a lot of things thanks to all of you. Now I have another problem and I guess that nobody before posted it (i searched but find anything) (sorry for my bad english)

Im trying to use fonts on slick2D to simple draw “info strings” like player position, currentState and that kind of things.

The code:
import java.awt.Font;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.Graphics;

Font font = new Font(“Serif”, Font.BOLD, 15);
UnicodeFont ufont= new UnicodeFont(font, font.getSize(), font.isBold(), font.isItalic());
g.setFont(ufont);

g.drawString(cad, 15, 45); <----------doesn’t work (compile ok)
ufont.drawString(15, 45, cad); <----------doesn’t work either (compile ok)

Really dunno why.
Game runs fine. No warnings. No errors. No crashes. Just dont want to draw the Strings.

Any idea?

Thanks!!

P.D.: cad is already innitiated.

Welcome to JGO ;D.

You need to add glyphs to your font. This explains how to do so http://slick.cokeandcode.com/wiki/doku.php?id=unicode_font_support.

Also for future reference, when you post code, look for the little #Button that reads “Insert Code” which will add [.code][./code] (but without the periods in there) and then you simply add your code between the two brackets to make something that looks like this.


// I am not an expert but here is your code modified to work.
UnicodeFont ufont = null;
String cad = "test";
try {
	Font font = new Font("Serif", Font.BOLD, 15);
	ufont = new UnicodeFont(font, font.getSize(), font.isBold(), font.isItalic());
	ufont.addAsciiGlyphs();
	ufont.addGlyphs(400, 600);
	ufont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
	ufont.loadGlyphs();
} catch (SlickException e) {
	e.printStackTrace();
}
g.setFont(ufont);
g.drawString(cad, 150, 450);
ufont.drawString(200, 450, cad);

If this is with slick engine I do not know much about that but with the slick you use to render text in lwjgl I do not have to add glyphs.

Thing you may have to do is unbind any textures and then if you use colored text, set the color back to (1,1,1,1) as slick does not do this. Kinda retarded imho that it will not fix that but what ever.

Some fonts above code point 127 take a really long time to load on certain Macs, so only add what you need.

If you aren’t using the latest dev branch, then I’d recommend the following:

   ufont.addGlyphs(32, 127);

If you are using the latest dev branch, the above should be equivalent to:

   ufont.addAsciiGlyphs();

[quote]Thing you may have to do is unbind any textures and then if you use colored text, set the color back to (1,1,1,1) as slick does not do this. Kinda retarded imho that it will not fix that but what ever.
[/quote]
Slick binds textures and colors for you – if no color is specified, white (1,1,1,1) will be used. If you are running into a texture bind bug, then you’re either using an outdated version of Slick, or you’re not using Slick’s Texture.bind() utility.

In general its a “bad” practice to use unicode fonts, just use bitmap fonts / angelcode fonts

Thank you guys for the quick answers!

Vladiedoo, your code works!. The link also helped a lot (I understand a bit more now :P)

davedes, how can I know if Im using the latest dev branch? I speak spanish, really dont know what a dev branch is but…It’s the Slick version? Becouse i don’t know that either (could be: #Sat Jun 26 18:22:25 UTC 2010 build=274 ?) im kinda lost on that too. I dont know what version of Slick Im using (but that is for other topic, I guess).

This whole world of fonts, Unicode, Angel, TrueType…I feel that I just dont know enough. AngelCode is the best choice? If it is, then I’ll start watching it.

AngelCodeFont is the fastest and best choice for simple text rendering, but it’s also more limited. It expects you to supply the font definition and image, which needs to be generated with a tool like BMFont (Windows) or TWL’s Font Tool. It doesn’t play well with unicode glyphs, so if you need multiple language support, I’d stick with UnicodeFont.

The main downside to UnicodeFont is that it’s slightly slower to load; and on certain Macs including the codepoints 128-255 will increase loading to ~3.5 seconds for a single font (this seems to be a Mac and/or Java2D bug). Once it’s loaded, it will perform nearly as efficiently as AngelCodeFont.

Also, if you want, you can load glyphs one at a time; in UnicodeFontTest it loads glyphs on the fly in the update loop. Presumably in a more complex game you would have a loading screen with a progress bar.

TrueTypeFont is deprecated and no longer maintained.

You can find more about the development branch here:
http://slick.javaunlimited.net/viewtopic.php?f=3&t=5171&start=0

Liam is working on a website which hosts a more recent build, although I’m not sure if the build includes the UnicodeFont changes I mentioned. To be safe, I’d just call addGlyphs(32, 127).

Thanks dave! I guess I’ll use Unicode at the moment. Been looking at the BMFont soft and topics about angel, and looks like that it’s necesary one more step to make it work…I supose that I’ll priorize other things first and maybe later look at angelcode seriusly.

Thanks for the answers guys!