rendering text?

I’m trying to render some text in LWJGL, just 2D text for like info like coins and health etc. I tried using slick-util, but it didnt work out too well, so I tried out slick2d but: http://www.slick2d.org/

:frowning: says the hosting account was suspended? any other temporary way to solve this?

But the problem with slick-util was that i could see it work for a second or 2, then after that it all messed up. I think I have to enable/disable something?

Do you want to use a bitmap font or truetype?
Because making your own bitmap font is rather easy, you just texture some quads with the appropriate letters, each with the desired offset from each other. And you can find plenty of bitmap fonts here.

Screenies?

You could download it from https://bitbucket.org/kevglass/slick although I don’t know if they have any .jar builds there, so you’d have to build it yourself. You could just copy the source of Slick2d into your project’s source folder if you’re feeling slack though.

As for rendering text without using a library, if you want bitmap text you should do something like this:


/**
 * Class for printing lines of text using a bitmap font.
 * Do whatever you want with it.
 * @author DeadlyFugu
 */
class TextPrinter {
	Texture tex;
	int cw, ch;
	int charOffset;

	/**
	 * Creates a new TextPrinter
	 * @param path Path to a bitmap font image
	 * @param cw Width of characters in that image
	 * @param ch Height of characters in that image
	 * @param first first char in tileset.
	 *              If the bitmap has a space (ASCII 0x20) in
	 *              the top right corner, this should be ' '
	 */
	public TextPrinter(File path, int cw, int ch, char first) {
		this.tex = loadTexture(path);
		this.cw=cw;
		this.ch=ch;
	}

	/**
	 * Print a line of text using this TextPrinter
	 * @param x X location to draw text from
	 * @param y Y location to draw text from
	 * @param str The text to draw
	 */
	public void drawString(int x, int y, String str) {
		tex.bind();
		char[] chars = str.toCharArray();
		for (int i=0;i<chars.length;i++) {
			int c = chars[i];
			drawTexturedQuad(x+i*cw,y,cw,ch,c*cw,c*ch,cw,ch);
		}
		tex.unbind();
	}

	/**
	 * Renders a textured quad
	 */
	private void drawTexturedQuad(int x, int y, int w, int h,
	                              int tx, int ty, int tw, int th) {
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(tx,ty);
		GL11.glVertex2f(x,y);
		GL11.glTexCoord2f(tx,ty+th);
		GL11.glVertex2f(x,y+h);
		GL11.glTexCoord2f(tx+tw,ty+th);
		GL11.glVertex2f(x+w,y+h);
		GL11.glTexCoord2f(tx+tw,ty);
		GL11.glVertex2f(x+w,y);
		GL11.glEnd();
	}
}

Here’s a spritebatcher based renderer text, along with some other stuff.

rel.phatcode.net/junk.php?id=140

Sorry links get borked when using a phone to type. Just copy-paste to address bar.

slick-util.jar exist in the jar folder of the Latest stable release of lwjgl, and in case you are still facing problem with rendering text, check out this post i faced a similar problem and fixed it with the help of the nice JGO members ;D

good luck

Mind that loading the glyphs when using the Slick_util will take some time, maybe even a couple of seconds. If you consider this as a big problem, use bitmaps. :wink:

If you want to use Slick2D to render the default Java fonts, some code in this file is helpful for doing so. Lines 1182-1285 are the ones that create Slick2D fonts to render from default Java fonts that you can make with Java2D. 3 variables on lines 150-152 are also helpful to know. I apologize for it being so hard to get to and the file being so massive, when I wrote it I didn’t really understand OOP.

Just to clear up some of the confusion… :slight_smile:

  1. Slick2D is “dead” and no longer maintained. You can still pull the source from the bitbucket; if you do, make sure to pull from development branch (master is outdated).

  2. SlickUtil (a subset of Slick2D) is also no longer maintained; the version on LWJGL repo is very outdated and rather buggy.

  3. Regarding Simn’s comment; this is a bug with older versions of SlickUtil. If you use UnicodeFont, make sure to specify glyphs like so:

   UnicodeFont unicodeFont = new UnicodeFont(new Font("Verdana", Font.PLAIN, 16));
   unicodeFont.getEffects().add(new ColorEffect());
   unicodeFont.addGlyphs(32, 127); // <-- must specify glyphs like this
   unicodeFont.loadGlyphs();

See the bug here.

  1. One good way to render fonts in OpenGL is to use an API like freetype-gl. This allows you to load TTF fonts on the fly into a texture atlas, see here for example. An easy way to utilize this in Java is to use LibGDX, which includes a FreeType extension.

  2. If for whatever reason you don’t want to use LibGDX, you could also use a more minimal library:
    https://github.com/mattdesl/lwjgl-basics

You would need to use a Bitmap Font creator, like BMFont or Matthias’ Font Tool:
http://twl.l33tlabs.org/themer/fonttool.jnlp‎

Then see the font example here:

You can also (if you’re bored or just curious) write your own font loader/render. It’s basically just a loading an image, chopping it up into regions that represent the different characters and rendering these regions in a certain order when drawString() is called. It’s a fun little project imo.