Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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 2635 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: https://gist.github.com/Th0masR0ss/e9243290d1ea6459e03f

And here's how to use it:

Constructors:
There are two constructors,
FontUtil(java.awt.Font font, java.awt.Color color, boolean antiAliased)
and
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.

Methods:

There are three methods.

FontUtil.setAA(boolean antiAliasing)
turns on or off Anti Aliasing based on the parameter you give it (
true
is on,
false
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.
str
is the
String
to draw, and
x
and
y
is the position to draw at. For a full example, see the Example section below.

Example

There is a full example here.
Offline ClaasJG

Junior Member


Medals: 4



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

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

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
lowerCamelCasing
naming convention, where C# and other .Net languages use the
UpperCamelCasing
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

Junior Member


Medals: 4



« 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.

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

-ClaasJG

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.

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

Offline Th0masR0ss

Junior Newbie


Medals: 1
Exp: 2 years



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

@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.
Offline PandaMoniumHUN
« 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

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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!