Geeks With Blogs

News

Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer


Archives
Chris G. Williams Beware: I mix tech and personal interests here.

If you want to display some text on screen in your XNA game, the easiest way is to use a SpriteFont. The cool thing about SpriteFonts is that you can use any TrueType font and XNA will render it into an *XNB file and allow you to use it in your game. You can distribute the game without worrying about whether people have the font installed on their machines.

(edit: XNB files can be opened using the XNB Viewer, downloadable here, although it doesn't seem to work on Vista.)

So, anyway, to do this... just follow these simple instructions. I assume you already know how to make an XNA Windows Game.

Right click on your project name and "add new item." Name your SpriteFont whatever you want, or just leave the default name of SpriteFont1.

Once you do that, it's only a few lines of code to get your message out to the world.

First you need to open the SpriteFont file (it's just xml) and tell it what font to use:

<Fontname>UltraViolent BB</Fontname>

In this example, I'm using a font from the BlamBot site, which my buddy George hooked me up with. These guys have some really great fonts (some free, some for sale) that are available for independant comics and non-profit projects. (That applies to the free ones, you can do whatever you want if you pay for them.)

HINT: If you add new fonts to your OS (in Vista, and probably in XP) make sure you close and restart XNA GSE so it will recognize your font, otherwise your project won't build. The same is true if you don't spell the font's name properly.

We'll go ahead and bump the size up too, to make it totally readable.

<Size>36</Size>

Next, open your game.cs file and add a SpriteFont, like so.

SpriteFont UVfont;
SpriteBatch spritebatch;

We're adding a SpriteBatch too, since we'll be using that in our Draw() routine. You also need to add the following lines in your LoadGraphicsContent method:

spritebatch = new SpriteBatch(this.graphics.GraphicsDevice);
UVfont = content.Load<
SpriteFont>("SpriteFont1");

Next we go down to the Draw() routine and add these three lines:

spritebatch.Begin();
spritebatch.DrawString(UVfont, "Hello World", Vector2.Zero, Color.White);
spritebatch.End();
 

Finally, hit F5 and you should be greeted by our Cornflower Blue screen, with the words Hello World in large white letters, as below:

Edit: MonoSpaced fonts are not supported with SpriteFonts (as of the 1.0 Refresh.) If you need a monospaced font, then you'll have to create your own XNB file or find another way.

Update: Shawn Hargreaves (XNA Team) says MonoSpaced fonts will be supported in XNA 2.0.

Posted on Saturday, November 3, 2007 8:57 PM Game Development , General Interest | Back to top


Comments on this post: Fun with Fonts in XNA

# re: Fun with Fonts in XNA
Requesting Gravatar...
Thanks for the tip, works great!
Left by Chris Steinberg on May 20, 2008 8:51 PM

# re: Fun with Fonts in XNA
Requesting Gravatar...
Thank for sharing this.
Left by Truyenle on Mar 31, 2011 11:50 AM

# re: Fun with Fonts in XNA
Requesting Gravatar...
VEry useful great idea..
Left by bharathi on Dec 21, 2011 4:47 AM

# re: Fun with Fonts in XNA
Requesting Gravatar...
I was reading your article and wondered if you had considered creating an ebook on this subject. Your writing would sell it fast. You have a lot of writing talent.
madden mobile hack
visit website
Left by dewsw on Mar 28, 2017 10:15 AM

Your comment:
 (will show your gravatar)


Copyright © Chris G. Williams | Powered by: GeeksWithBlogs.net