Game Development Community

Sprite Fonts

by Cosmic Logic · in Torque X 2D · 07/29/2009 (11:07 pm) · 7 replies

I've been looking in to this for a bit now but all I'm finding is stuff that is either for 2.0 or doesn't have much of an explaination on how to use these.

Could anyone tell me how to work a set of custom sprite fonts?

#1
07/30/2009 (12:40 am)
For GUI elements? I have some docs on how to create custom SpriteFonts for text elements. You can find that guide here: static.garagegames.com/static/pg/blogs/michael-perry/An%20Introduction%20to%20th...
#2
07/30/2009 (2:06 pm)
Jason, you should update that PDF to give yourself credit for writing it. Excellent work!

Brian
#3
07/31/2009 (3:26 am)
Theres two ways you can throw in a sprite font:
1. This method was explained in Jasons guide. You can right click the content part of your project and go to Add->New Object. Select SpriteFont and name it what you want to reference it as. If you want to be uniform with the ones included do something like "Arial12", where 12 is the size. Open up your new .spritefont file and edit the values as you see fit. Make absolutely sure the Sprite Family is the official name for the font and that you have the font installed on your computer.
2. Create a custom png for yourself. There are a bunch of different utilities that convert ttf to bmp or png along with other effects, you can find em easily all over the internet. Add it to your project and change the "Content Processor" property to "Sprite Font Texture - XNA Framework"

Ok, now you have your sprite fonts building, how do you use it? Well there are a few ways.

Let's say I created a Sprite Font in the data\fonts folder called "Sansation15"(Sansation is a license free font I found...more on that later). To use my Sansation font with GUI elements, such as GUIText or GUIButton, heres how I do it:

GUIText TextHello = new GUIText();
GUITextStyle TextStyle = new GUITextStyle();
TextStyle.FontType = @"data\fonts\Sansation15"
TextStyle.SizeToText = true;
TextStyle.TextColor[CustomColor.ColorBase] = Color.White;

TextHello.Style = TextStyle;
TextHello.Text = "Hello World";
TextHello.Position = INSERTPOSHERE;
TextHello.Folder = INSERTFOLDERHERE;
Anywho, the big thing with this code is that FontType needs to have the path to your spritefont.

You may need to load up the sprite font yourself for various things(I use it to determine the size of a string in pixels, or you might want to use base XNA sprite drawing for some reason...), so heres how you do that:
SpriteFont customFont = Game.Instance.Content.Load<SpriteFont>(@"data\fonts\Sansation15");
Then you could do something like this if you are getting the size of a string to render:
Vector2 stringSize = customFont.MeasureString("Lewls, this is a string");

Anywho, I hope you get everything. Fun stuff playing with this.
#4
07/31/2009 (11:30 am)
That's awesome Christopher! Thanks!

Brian
#5
07/31/2009 (11:54 am)
Also helpful is the GUIMLText
use it the same way, just use a GUIMLTextStyle
/n breaks to the next line
#6
07/31/2009 (2:37 pm)
OH YEAH!!! I forgot to talk about the license stuff.

Anywho, most of the fonts out there are licensed where you cant use it more than just on the machine that installed it. In fact most of the ones that comes with windows are like that. Jason has a what microsoft offers in his doc and there are a few other places to find fonts you can legally use. I picked up Sansation off www.fontsquirrel.com/. They are free to use for commercial use.
#7
07/31/2009 (4:00 pm)
There's also the redistributable font pack on creators.xna.com.

Brian