Jump to content
  • Advertisement
Sign in to follow this  
Sly

BMFont and how to interpret the .fnt file

This topic is 4981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I figured this is the AngelCode forum, so it probably covers the tools as well as AngelScript. I have written a loader and renderer for the fonts produced by BMFont. All is working well in that the fonts are displaying on the screen. What I cannot work out is how to use the xoffset and yoffset fields from the .fnt file. If I add or subtract the yoffset from the y coordinate used to display the character, the characters are not displayed in line (they seem to be at random heights). What is the meaning of the offset values and how do I use them? I am currently looking at 'guppy' with the g, p and y characters sitting high so their descenders are in line with the bottom of the u. Is there some sample code or explanation somewhere that I could look at? There is also a bug in BMFont in how the xadvance field is written to the .fnt file in regards to the antialias level. The xadvance field is incorrectly multiplied by the antialias level. If I have the antialias set to 2, then I have to divide the xadvance value by 2 to get the text spaced correctly. Likewise for 3 and 4 (have to divide by 3 and 4 to get the correct value). Otherwise I'm getting some nice results from BMFont. Thanks for releasing it.

Share this post


Link to post
Share on other sites
Advertisement
Yes, this is the right place for your question.

I've never gotten around to writing any tutorial or sample for using the font file. I should probably do that as it would make the tool more valuable.

Here's a short description of each attribute in the .fnt file.

lineHeight: is how much to move the cursor when going to the next line.

base: this is the offset from the top of line, to where the base of each character is.

scaleW and scaleH: This is the size of the texture.

pages: gives how many textures that are used for the font.

id: is the character number in the ASCII table.

x, y, width, and height: give the position and size of the character image in the texture.

xoffset and yoffset: hold the offset with which to offset the cursor position when drawing the character image. Note, these shouldn't actually change the cursor position.

xadvance: is how much the cursor position should be moved after each character.

page: gives the texture where the character image is found.

Here's some pseudo code for rendering a character:


// Compute the source rect
Rect src;
src.left = x;
src.top = y;
src.right = x + width;
src.bottom = y + height;

// Compute the destination rect
Rect dst;
dst.left = cursor.x + xoffset;
dst.top = cursor.y + yoffset;
dst.right = dst.left + width;
dst.bottom = dst.top + height;

// Draw the image from the right texture
DrawRect(page, src, dst);

// Update the position
cursos.x += xadvance;


I hope that explains it.

I'll look into the bug. Thanks for letting me know.

Regards,
Andreas

[edit] fixed the pseudo code as Sly pointed out [/edit]

[Edited by - WitchLord on November 25, 2004 8:00:15 AM]

Share this post


Link to post
Share on other sites
Thanks. I'll give that a try when I get home tonight, or here at work if I manage to get FreePascal to compile my Delphi game project.

Share this post


Link to post
Share on other sites
For the easily confused, Andreas' example should have read


// Compute the destination rect
Rect dst;
dst.left = cursor.x + xoffset;
dst.top = cursor.y + yoffset;
dst.right = dst.left + width;
dst.bottom = dst.top + height;


Thanks for that, Andreas. It all works sweet now. :)

[Edited by - Sly on November 25, 2004 3:52:29 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!