In the game development, the general first encounter is the text, for the text and need font support, in XNA, if you want to use text, you need to specify the font, for the font XNA use Spritefont and spritefonttexture two ways to specify the font.
Spritefont
This font file is actually an XML configuration file to configure the font, font size, font style, and font encoding range. This configuration is then compiled to compile the font into a. xnb binary file.
<?xml version=”1.0″ encoding=”utf-8″?>
<XnaContent xmlns:Graphics=”Microsoft.Xna.Framework.Content.Pipeline.Graphics”>
<Asset Type=”Graphics:FontDescription”>
<FontName>Courier New</FontName>
<Size>32</Size>
<Spacing>2</Spacing>
<Style>Bold</Style>
<CharacterRegions>
<CharacterRegion>
<Start> </Start>
<End>~</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
XNA also supports Chinese, and its configuration method is the same.
<?xml version=”1.0″ encoding=”utf-8″?>
<XnaContent xmlns:Graphics=”Microsoft.Xna.Framework.Content.Pipeline.Graphics”>
<Asset Type=”Graphics:FontDescription”>
<FontName>宋体</FontName>
<Size>32</Size>
<Spacing>2</Spacing>
<Style>Bold</Style>
<CharacterRegions>
<CharacterRegion>
<Start>一</Start>
<End>龥</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
The figure seen above looks strange, why input such Chinese, this is because the Chinese is expressed in Unicode (this is the first few characters in the Unicode Code table), its coding interval is 4E00 (19968) -9fa5 (40869), that is to say there are more than 20,000 words, So it's particularly slow to compile, and sometimes it makes vs2010 unresponsive. From this view, to develop XNA Chinese version of the game, the machine must be better, or even the compiler can not pass.
First XNA Program Hello world!
A Create the XNA project, add the Spritefont font file under Content Engineering, and configure the fonts you want to use.
In the Game1.cs file, add the code:
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
SpriteBatch.Begin();
SpriteBatch.DrawString(Font, “Hello World!”, new Vector2(100, 200), Color.Red);
SpriteBatch.End();
}
protected override void LoadContent()
{
base.LoadContent();
ContentManager cm = this.Content;
Font = cm.Load<SpriteFont>(“gamefont”);
}
Sample Downloads: http://www.daisy123.com/?page_id=275 testxnafont.zip