Freigeben über


Creating and using bitmap fonts in XNA

As mentioned in a previous post, there is a clear need for a bitmap font generator that provides proper Unicode support.

To address this need, I created BMFontGen.exe. This program can generate a bitmap font from any of the fonts currently installed on your system and it provides a variety of options for controlling which glyphs get placed in the font and how they appear. More details about the tool (including a download link) are given in this document, but for our purposes it suffices to know that BMFontGen takes a font name and a font size and produces an XML file (containing the font description) and a collection of PNG files (that contain the glyph bitmaps).

To use these fonts in an XNA game, you need some code that can load these files and let you draw a string of text on the screen. To fill this role, I created the BitmapFont class. This class handles the font loading and exposes a set of DrawString methods that do what you would expect them to do.

To demonstrate the class in action, I've created a simple demo app (attached to this post - see link at bottom) which I've cleverly named "BitmapFontDemo". Here is a screenshot from the app that shows off some of the capabilities of the font renderer.

The BitmapFont class is intended to provide basic text rendering services only, so don't expect it to handle linebreaks or format text in a rectangle or provide any other advanced text options. However, I'll be adding functionality to it as time goes on, and I also intend to release other game support classes that build upon the base functionality provided by BitmapFont.

This is the first release for this tool, so let me know if you find this useful or if you think there're features missing. I'm curious to see games that make use of these fonts (even if I can't read them ^_^).

BitmapFontDemo.zip

Comments

  • Anonymous
    August 30, 2006
    PingBack from http://blogs.msdn.com/garykac/archive/2006/08/29/726222.aspx

  • Anonymous
    August 30, 2006
    Thanks dude. You rule!

  • Anonymous
    September 02, 2006
    Works great, thanks for sharing.

  • Anonymous
    September 02, 2006
    Awesome class! This is gonna save me SO much time.. Thank you very much!

  • Anonymous
    September 03, 2006
    This is great. I only have 1 recommendation. Can you add a readonly property of FontFilename to the BitmapFont class? I've done so for my project, but I think I'll be checking in and updating the class with your new improvements. I'm using that prop to check to see if the font has already been loaded by looping through a List<BitmapFont>, etc.

  • Anonymous
    September 04, 2006
    Gary has several informative links in his XNA Framework Content Pipeline post.  He also gives us a couple...

  • Anonymous
    September 04, 2006
    You're a hero, thank you for this.

  • Anonymous
    September 04, 2006
    PingBack from http://mindforwar.com/2006/09/05/not-quite-later-today/

  • Anonymous
    September 05, 2006
    The biggest missing feature is that there aren't any "button graphics" included with XNA. I would think pretty much every game developed would want them. (I'm saying, a bitmap for A, B, X, Y, right shoulder, and so forth).

    In any event, you are correct in your premise about this issue: this kind of functionality needs to be rolled into XNA proper.

  • Anonymous
    September 08, 2006
    Great work cheers. David's comment is a good suggestion other than that, great job helped me a lot :D

    Pete

  • Anonymous
    September 08, 2006
    The ClearType&amp;nbsp;text rendering hint (option: -trh ct-grid) tells&amp;nbsp;BMFontGen&amp;nbsp;to apply&amp;nbsp;ClearType...

  • Anonymous
    September 11, 2006
    The comment has been removed

  • Anonymous
    September 12, 2006
    The comment has been removed

  • Anonymous
    September 12, 2006
    Still getting the surface error. I'm not sure what else needs disposing.

    Just to make sure i have:

    //handlers for device events
               device.DeviceLost += delegate { this.UnloadResources(); };
               device.DeviceReset += delegate { this.LoadResources(device); };

    Then:

    private void LoadResources(GraphicsDevice device)
           {
               //set anisotropic filter
               InitAnisotropy();
               debugFont.Reset(device);
           }

    private void UnloadResources()
           {
               debugFont.Dispose();          
           }

    Is this correct? [I don't see any problems with what you've posted.  Can you create a small example project that exhibits the problem and send it to me? I suspect something else is going on but I can't tell from the snippets. -GaryKac]

  • Anonymous
    September 19, 2006
    Hi,

    Thanks for sharing this! I really like the custom glyph option because it allows me to create my own custom 'Photoshopped' fonts instead of creating a separate bitmap for words like Game Over and Score :)

    I've been using it for a couple of days now and everything worked fine. I just tried to 'publish' my 2d shooter and I got an filenotfound exception. After doing some digging (I'm new to C# express)  I found that the .xml file is not copied to directory:
    shoo..tion_05bd4a2781ef8435_0001.0000_9c1bafe256fc38bb
    but is copied to:
    shoo...exe_05bd4a2781ef8435_0001.0000_none_4341aed75d4629b4

    When I copy the XML file from the exe to the tion directory it manually after installing the published version, it works fine. I have the xml and png file set to "Content" and "Copy always". The other images in the game, with the same settings, show up in both directories.

    I'm guessing I'm just doing something very wrong, but I noticed some comments about the filepath in your other topic, so I was hoping you could clear this up, or maybe even, if appropriate, change the class to avoid this.

    Thanks again! [I don't do anything magical with the files - I just expect them to be copied to the correct location (and to be given a path to that location). If they're not being copied, then there must be something screwy with the copy. However, it doesn't sound like you're doing anything wrong. One suggestion I have is to create a new project, add the art assets and then make sure that they're being copied over correctly. If they copy properly in the new project, then add all your source code and replace your old project with new one. Yes, this technically shouldn't do anything, but I needed to do this once to "fix" a truly unfathomable problem I was encountering a week or so ago. If you're still having problems with the "new project" version, then let me know. -GaryKac]

  • Anonymous
    September 21, 2006
    The comment has been removed

  • Anonymous
    September 21, 2006
    Oh and one more thing.  If you don't put the font files in a seperate directory, the class looks for the font file name in the "c:/: drive root instead of the root in the "bin/debug" folder. [You're right. It will find the XML file, but will look in the wrong directory for the PNG image files. I'll include a fix in this weekend's release. Thanks! -Gary]

  • Anonymous
    November 30, 2006
    PingBack from http://andyq.no-ip.com/blog/?p=18

  • Anonymous
    April 25, 2008
    《XNA高级编程:Xbox 360和Windows》第四章第四小节:更多辅助类