Home > C#, Windows Phone 7, XNA > Using SFont sprite fonts in XNA

Using SFont sprite fonts in XNA

I am currently busy porting a SDL based Perl game to C#, XNA and Windows Phone 7. During the development I was confronted with the problem that I had to convert a SFont sprite font to something that XNA can handle.

The XNA content processor by default transforms a windows font file to sprite font with the aid of an xml file that describes the export settings. The problem was that I didn’t have the font file. I only had a texture with the letters. Luckily I found out that there was also another way in XNA.  If each character is completely surrounded with magenta then XNA can also use a texture. The SFont font format on the other hand requires only that the pixels of the top row are magenta for separating the different characters. So I couldn’t do a straight import of the SFont format.

Being a geek I decided to write a small command line application to transfer the SFont fonts; instead of doing the transformation by hand in Photoshop.

After this we only need to specify the font texture as “Sprite Font Texture” and set the first character to  “!”.

Next we can load the sprite font the usual way:

protected override void LoadContent() 
{
	// ..
	spriteFont = Content.Load<SpriteFont>(“fontName”);
	// ..
}

And draw text with it using:

protected override void Draw(GameTime gameTime)
{
	// ..
	spriteBatch.DrawString(spriteFont, "your message", new Vector2D(x, y), Color.White);
	// ..
}

For people interested: my command line tool be downloaded at:
http://rapidshare.com/files/401795596/SFontToXnaFontConverter.rar.html

You can use it by running:
SFontToXnaFontConverter.exe [inputfile] [outputfile]

More information about SFont can be found at:
http://www.nostatic.org/sfont/

Advertisements
Categories: C#, Windows Phone 7, XNA
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: