Fonts.  Using sprites?

I read the Space Invaders 101 tutorial on Java 2-D, and at the end when he was mentioning possible future tutes, he says something about “Sprite based fonts”. I don’t really know what this is, but I’m pretty sure I need it. The other day, I was trying to make some kind of in-game window system for displaying messages, but the spacing is so off when you try to use drawString(), so I couldn’t get the windows to fit the text perfectly. Are sprite based fonts the solution to this problem?

Sprite based fonts are when you use a sprite for each character your going to display. When you want to write something on the screen you use a series of sprites. So to write “TEST” you’d use four sprites, one for each letter.

This gives you better performance (normally), nicer looking fonts but uses up lots of graphics memory.

Kev

Yeah… I guess if I did that I would have to write some way to map characters from my strings to the appropriate sprite. That seems like some extra work in the beginnning, but it seems like it’d save me some time in the long run. Thanks for the reply. ;D

What’s the best way of doing this? If I recall, if you call evt.getKeyCode() this will return the same int value for ‘a’ as for ‘A’. I know you can also detect if the shift key is depressed with something like evt.isShiftPressed(). What is the most efficient method for constructing this mapping?

Regards,

Ribot.

I can’t see why you’d want to use sprite fonts, unless it is to design your own font. Why don’t your messages display correctly? Remember, the x and y values you use with drawString aren’t used for the top left point of the drawn string (check out the API). If i remember correctly, the x and y is used to position the baseline.

The message displays normally with drawString(), moving to sprite based fonts is a performance thing. Blitting sprites to the screen is considerably faster than tracing round a font glyph for each letter.

If you move to OpenGL this becomes more important because while you can use dynamic textures converted from buffered images into which you’ve used drawString() its far more efficent to push all your sprite fonts off onto texture memory at initialisation onto the graphics card and just use changes to your texture coordinates to actually write the text to the screen.

There are of course positives and negatives to both methods.

As to a good algorithm I’ve found an array of character code indexed images works quite nicely in Java 2D. If you’re looking for a solution for OpenGL it might be worth checking out NeHe Lesson 17 (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=17)

Kev

Many thanks for the reminder kev, I had totally forgotten about nehe’s bitmap texture tutorial.

http://members.rogers.com/mark.bernard/NeHeGLLessons.zip has a Java version of that tutorial. It also has the tutorial where the texture font is dynamically created at initialization.(#13 I think)

Thanks, I’ve got my basic font sprites working now! :smiley:

The outcome is a little bit ugly (unless you use a fixed width font), so I’m off to program in variable width fonts.

I tried using the lwjgl tutorial 13 for getting fonts,
and it seems like I can’t draw any other objects after
after initializing the fonts…

I’m not exactly sure what the dynamically font creation
code is doing. I was wondering if someone can point out, what else needs to be done after the buildFont()
function has been called. In order for polygons to be
drawn.

Thanks in advance.

Ok…
I give up,
and decided to use sprites…
Nehe lesson 17 way…