Slick2D Fonts don't work

2019-02-20 01:42发布

问题:

I have Slick2D library and making a game in Java. I have been wondering, just a short question, how do you set fonts on Graphics object to draw in a different font. I can't make it work. I think it has something to do with AngelCode but that requires some format that isn't '.tff' so I don't know.

So can you tell me how you do it?

回答1:

sorry, but this answer is anything but short

You can use angel code's BMF font tool, which makes bitmap font files with a glyph image that stores the character images, and a text file that describes the image file (windows only, Herio.jar is similar and is included with slick, but it only works sometimes, crashing on me repeatedly)

  1. Get BMF font here and install it with the included windows installer. Once you have done that, use the GUI to select a font, size, etc. (note that you have to make 1 font/glyh image pair per size and font) Then highlight the boxes that contain characters you want to use, noting that slick only supports up to 256 (I think) characters.

  2. you need to Change some export settings so that slick will recognize the files. Go to file -> export options and change the the "bit channels" (I think that's what its called) A -> glyph R,G, and B -> one. Set presets to custom, and the textures to .png files, and your configuring is done.

  3. Export your newly created font files [file -> save bitmap font as] (yes, it will make a glyph image (.png), and a text descriptor file with the extension .fnt) in your appropriate data directory.

  4. Code it in by using the AngelCodeFont constrcutor:

    fnt = new AngelCodeFont("LOCATION OF .fnt FILE", new Image("LOCATION OF .png GLYPH IMAGE");

then you can then either draw with it directly:

fnt.drawString(float x, float y, "TEXT TO DISPAY");

or you can

set the current graphics font as the AngelCodeFont and draw directly through the graphics class.

GraphicsObject.setFont(fnt);
GraphicsObject.drawString("THIS WLL DRAW IN THE ANGEL CODE FONT");

hope this helped :D

more info here, in the API documentation: http://slick.cokeandcode.com/javadoc/