Archive

Archive for the ‘XNA’ Category

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