How can one draw text?

I’m really sad. I’m trying to use LWJGL to draw some text on the screen, nothing fancy, I just need to be able to read some info. I find that I’ll need Slick. I get slick. I use this ( http://www.lwjgl.org/wiki/index.php?title=Slick-Util_Library_-Part_3-_TrueType_Fonts_for_LWJGL ) to try and draw something with TrueTypeFont. Doesn’t work, also deprecated. I find that I need to use “UnicodeFont”. I use that, but I get null pointer errors which no one online seems to get (specifically this:)

Exception in thread “Thread-0” java.lang.IllegalStateException: The UnicodeFont must have at least one effect before any glyphs can be loaded.
at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:340)
at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:325)
Any API’s I find are dead links.

I have a very simple question. How do I draw text on my screen?

Try this:


Font awtFont = new Font("Default", Font.PLAIN, 14);
font = new UnicodeFont(awtFont);
font.getEffects().add(new ColorEffect(java.awt.Color.white));
font.addAsciiGlyphs();
try { font.loadGlyphs(); }
catch (SlickException e) { 
    e.printStackTrace(); 
}

I’m guessing you don’t have a “font.getEffects().add(…)”.

No dice

Exception in thread “Thread-0” java.lang.NoSuchMethodError: org.newdawn.slick.opengl.renderer.SGL.glTexSubImage2D(IIIIIIIILjava/nio/ByteBuffer;)V
at org.newdawn.slick.font.GlyphPage.renderGlyph(GlyphPage.java:212)
at org.newdawn.slick.font.GlyphPage.loadGlyphs(GlyphPage.java:166)
at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:376)
at org.newdawn.slick.UnicodeFont.loadGlyphs(UnicodeFont.java:325)

It doesn’t like “try { font.loadGlyphs(); }”

This looks really weird: ???
org.newdawn.slick.opengl.renderer.SGL.glTexSubImage2D(IIIIIIIILjava/nio/ByteBuffer;)V

Are you using the latest version of slick?

EDIT:
also: https://bitbucket.org/kevglass/slick/issue/29/unicodefontloadglyphs-throws

I used the one at ninjacave, that’s the only one i could find
http://slick.cokeandcode.com/

Where can I even get the latest version of Slick?

See my edit above and check your build path, what does it have in it? e.g. for one of my projects which uses font rendering I have lwjgl_util.jar, lwjgl.jar, and slick.jar.

You probably have the latest version.

Here is a picture what my buildpath looks like

From what this said it’s probably an incompatability between you slick.jar and slick_util.jar versions. The latest slick download doesn’t even include slick_util so try removing it, maybe the slick_util stuff was combined with slick in the latest version.

That was a good idea. I no longer have any errors. However, I still cannot see my text.
I load everything like this:

		Font awtFont = new Font("Times New Roman", Font.PLAIN, 14);
		font = new UnicodeFont(awtFont);
		font.getEffects().add(new ColorEffect(java.awt.Color.white));
		font.addAsciiGlyphs();
		try { font.loadGlyphs(); }
		catch (SlickException e) { 
		    e.printStackTrace(); 
		}

And draw it like this:


			font.drawString(0.2f, 0.2f, "efer");

Is there anything else I need to include to get it to draw properly?

Thank you

Do you have other textures drawing before the text? If so, call

TextureImpl.unbind();

Or it could be

TextureImpl.bindNone();

I can’t remember which one :frowning:

How are your matrices set up? e.g. I’m doing:


        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, w, h, 0, 1, -1);// w is the screen width and h is the height
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        font.drawString(10, 10, "Text");//draws text 10 pixels down and to the right of the top left corner

Maybe your text is getting drawn off-screen? I don’t really know tbh.

That works! The only problem is that is removes everything else drawn from the screen. How can I use different GL settings at different times to overlay them?

Oh yeah, you need to set the matrices back to what they were again afterwards. I think this should work:


        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glOrtho(0, w, h, 0, 1, -1);// w is the screen width and h is the height
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();
        glEnable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        font.drawString(10, 10, "Text");//draws text 10 pixels down and to the right of the top left corner
        // do rest of text drawing here + any other overlay stuff
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
        glEnable(GL_DEPTH_TEST);

Also make sure any overlay type stuff is rendered last because if depth test is disabled it draws in the order of the commands.

It works! Thank you guys so much!

Just one thing, is there a way to make the text look smoother?
I tried to render Courier, but it looks like this

(I have only copied the code found in this thread)

Add glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); somewhere before rendering the text. I thought it got set to that by default.

Excellent, thank you! This was a very confusing block of code.

You can do this the “Notch” way. The almighty Notch’s way. Check out his Ludum Dare entries source code.

My API here also has some basic bitmap text support:

Of course if you want something more robust and cross-platform, you should be using LibGDX. :slight_smile: