Advertisement

Font Engine

Started by March 05, 2003 02:23 AM
8 comments, last by Strider81 21 years, 8 months ago
I''m using SDL, but i guess this question is just in general: okay, lets say you''re making an 2D rpg, or an adventure game etc. something that typically contains lots of dialogue, i.e. lots of text. I''ve never designed a font engine or anything like that before so i was just wondering how they work? Would it be terribly inefficient to just have a system that given a bmp with all the characters you need drawn out (via ms paint or photoshop etc.),just blits them to the screen in the same way you blit a character image or a background image to the screen?
Your bitmap-per-character idea is how I do it. It works very well.
Advertisement
I just finished writing a small Font Engine to display text in my game.
I have a 256*256 bmp file with all characters in it. Then my font engine creates a Display List containing every character. In this display list, every character is a quad where I apply the according texture of the character.
To draw a string I then just need to make a call to my display list and that draws the string.

It is basically what NeHe does in tutorial 17.

P.S.: I am using SDL OpenGL as well.
There are some existing font libraries for SDL you might want to look at, check out SFont/SoFont and SDL_ttf you can find links to them in the library section of the libSDL site. Of course you might want to roll your own which is perfectly worthwhile.

The easiest way to do it is as you have described:

1. Load a BMP containing all the characters you want to display, which could be your own sequence of numbers, lower case characters and upper case characters or an ASCII sequence.

2. Then go through the main surface and find the position of each character on your font surface, there are a few ways you can do this but you will most probably need a GetPixel type function for this.

3. When you want to render a string go through each letter in the string and blit the corresponding character.

The most complicated part is step 2, make sure you know exactly how your going to go about it. My first attempt at writing a font engine failed because the paint programme I was using anti-aliased the characters on the bitmap, it was a drama.

Hope that helps.
Mine works the same way, except that instead of loading a bitmap file with the font in it, you give it an HFONT and it creates a texture and uses gdi functions to draw the font to the bitbap, keeping track of the position of each character. It''s much easier than scanning an already-created bmp for character positions.
Using a bitmap is usually the way to go. If you code it properly, there isn''t too much of a problem with efficiency

--------------------------------------------
This is the polish virus. It is not an executable, but if you would kindly format your entire harddrive, we would be happy.
With love, AnonymousPosterChild
Advertisement
the way i was thinking was with a switch statement something like
switch(c)
case''a'':
case''b'':
etc...
and then draw the appropriate character for each case...but the case statement can get pretty long considering the amount of charcters. Is there any shortcut to this?? thanks
have an array of bitmaps or display lists.

(insert type here) ListOfLetters[256];

then to draw ''a'' you just draw List[''a''] and just make sure that everything is in the right place in the list. this is much easier than a switch statement.
If you are interested you can use function I wrote for my 3D GUI. It renders text into DXT3 texture. I did some testing and it beats both D3DXFONT and CD3DFont. You can get it at http://members.lycos.co.uk/ujanus. Unfortunately it could be down at the moment.
sounds cool thanks! i''ll check that out

This topic is closed to new replies.

Advertisement