Blogus Maximus

Rubbing people the wrong way since 1970...

  Home  |   Contact  |   Syndication    |   Login
  1276 Posts | 10 Stories | 1767 Comments | 1341 Trackbacks

News






Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Men who feel strong in the justice of their cause, or confident in their powers, do not waste breath in childish boasts of their own superiority and querulous depreciation of their antagonists.
--James Russell Lowell, 1861



follow chrisgwilliams at http://twitter.com
My Blog Juice

Twitter












Archives

Post Categories

Image Galleries

Blogs

Code Camps

CTown Geeks

GDI+

Geeky Webcomics

High Geek

Magenic Blogs

Microsoft Blogs

My Articles

My Sites

My Sponsors

PodCasts

UG

XNA

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 03, 2007 8:57 PM

Feedback

# re: Fun with Fonts in XNA 5/20/2008 8:51 PM Chris Steinberg
Thanks for the tip, works great!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: