FontUtil

This is my little library that allows you to draw fonts in LWJGL. It’s very small (only 114 LOC).

Here’s a screenshot of it drawing using the Sansation font:

http://puu.sh/9w4Ti/b16583ac37.png

Here’s the code itself: https://gist.github.com/Th0masR0ss/e9243290d1ea6459e03f

And here’s how to use it:

Constructors:
There are two constructors, [icode]FontUtil(java.awt.Font font, java.awt.Color color, boolean antiAliased)[/icode] and [icode]FontUtil(java.awt.Font font)[/icode].

[icode]FontUtil(java.awt.Font font, java.awt.Color color, boolean antiAliased)[/icode] will create a new FontUtil with the settings for the font you specified, the color you specified, and also if you want Anti Aliasing or not. You can change these settings later, see the Methods section.

[icode]FontUtil(java.awt.Font font)[/icode] will create a new FontUtil with the default settings: white color with anti aliasing on. You can change these settings later, see the Methods section.

Methods:

There are three methods.

[icode]FontUtil.setAA(boolean antiAliasing)[/icode] turns on or off Anti Aliasing based on the parameter you give it ([icode]true[/icode] is on, [icode]false[/icode] is off).

[icode]FontUtil.setColor(java.awt.Color color)[/icode] sets the color of the text to draw.

[icode]DrawString(String str, int x, int y)[/icode] actually draws the text. See the Drawing Text section.

Drawing Text

You draw text with the [icode]drawString(String str, int x, int y)[/icode] method. It accepts three arguments, which are pretty self-explanatory. [icode]str[/icode] is the [icode]String[/icode] to draw, and [icode]x[/icode] and [icode]y[/icode] is the position to draw at. For a full example, see the Example section below.

Example

There is a full example here.

Nice and handy, but i think it would be better to call glDeleteTextures somewhere.
-ClaasJG

You seem to be coming from .Net side right? Method names in Java follows [icode]lowerCamelCasing[/icode] naming convention, where C# and other .Net languages use the [icode]UpperCamelCasing[/icode] naming conventions. It’s not necessary to follow, but that makes it more Java-ish.

I’ve had a quick gander at your code and noticed some small things.

  • Your code is using Immediate mode rendering. I’m not accusing you of using it, but I don’t prefer it.
  • You are creating a texture for every string to be drawn. How much performance can this give with changing text like fps counter?
  • You do not destroy the textures one they are used. This can cause a lot of performance go waste.

I’m curious to know the FPS it offers on your computer. Anyway thanks for sharing though.

ha, i am correcting the code style and writting a small ‘catch’ in the backround. Based on Th0masR0ss version. I will post it as Edit for this post in some minutes.

[edit] https://gist.github.com/ClaasJG/f582bda7cee2048013d6

-ClaasJG

Here’s a great tutorial I’ve found on how to render text using OpenGL 3.2. He uses QT to load the fonts, but it can be simple to change to Java2D’s Font class.

http://jonmacey.blogspot.in/2011/10/text-rendering-using-opengl-32.html

@SHC

Thanks for your pointers (this is one of my first OpenGL projects). The FPS on my computer (an iMac 2009 with 8gb RAM) hovers around 100-120 FPS (using the example that i gave plus one more call to DrawString). I have updated the methods to use camelCase instead of PascalCase. I have also updated it to destroy the textures after they have been used. I have also updated the post.

Thanks.

If you need help porting your code to OpenGL 3.1+ for rendering with VBOs and shaders take a look at my engine’s font renderer: [Link]
Rendering with VBOs instead of immediate mode can speed up your rendering significantly (it’s about 50x faster to render with VBOs than with immediate mode, but that depends on quite a lot of things).
It’s only capable of rendering text from BitMap fonts (with font data generated by BMFont), but it’s pretty sophisticated seeing that it utilizes kerning and everything. :wink:

I think a lot of people misunderstand something.

The way that font rendering works is this:

  • You generate a font: character bitmaps and stuff like that
  • You render your desired text into a texture using your font
  • You render your texture acquired from rendering a font wherever you want

Using java2d to render fonts to buffered image, load that buffered image into opengl is the easiest way of achieving fonts in LWJGL.