Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (612)
Games in Android Showcase (172)
games submitted by our members
Games in WIP (659)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  FontUtil  (Read 3699 times)
0 Members and 1 Guest are viewing this topic.
Offline Th0masR0ss

Junior Newbie

Medals: 1
Exp: 2 years

« Posted 2014-06-16 14:53:49 »

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:

Here's the code itself:

And here's how to use it:

There are two constructors,
FontUtil(java.awt.Font font, java.awt.Color color, boolean antiAliased)
FontUtil(java.awt.Font font)

FontUtil(java.awt.Font font, java.awt.Color color, boolean antiAliased)
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.

FontUtil(java.awt.Font font)
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.


There are three methods.

FontUtil.setAA(boolean antiAliasing)
turns on or off Anti Aliasing based on the parameter you give it (
is on,
is off).

FontUtil.setColor(java.awt.Color color)
sets the color of the text to draw.

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

Drawing Text

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


There is a full example here.
Offline ClaasJG

Senior Devvie

Medals: 19

« Reply #1 - Posted 2014-06-16 15:13:41 »

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

My english has to be tweaked. Please show me my mistakes.
Offline SHC
« Reply #2 - Posted 2014-06-16 15:16:27 »

You seem to be coming from .Net side right? Method names in Java follows
naming convention, where C# and other .Net languages use the
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ClaasJG

Senior Devvie

Medals: 19

« Reply #3 - Posted 2014-06-16 15:18:17 »

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.



My english has to be tweaked. Please show me my mistakes.
Offline SHC
« Reply #4 - Posted 2014-06-16 15:24:45 »

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.

Offline Th0masR0ss

Junior Newbie

Medals: 1
Exp: 2 years

« Reply #5 - Posted 2014-06-16 16:34:55 »


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.

Offline PandaMoniumHUN

JGO Coder

Medals: 32
Exp: 3 years

White-bearded OGL wizard

« Reply #6 - Posted 2014-06-16 16:44:37 »

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

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline trollwarrior1
« Reply #7 - Posted 2014-06-20 19:47:06 »

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.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Andrew_3ds (20 views)
2015-09-01 19:08:10

afikri (15 views)
2015-08-31 09:30:22

afikri (23 views)
2015-08-31 09:30:07

afikri (11 views)
2015-08-31 09:27:24

afikri (15 views)
2015-08-31 09:26:40

Roquen (20 views)
2015-08-29 11:30:54

GamerC4 (33 views)
2015-08-22 20:38:50

GamerC4 (29 views)
2015-08-22 20:37:18

GamerC4 (34 views)
2015-08-22 20:37:01

Kefwar (43 views)
2015-08-22 18:07:24
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!