In game development, we generally encounter text first, which requires support for text. In XNA, if you want to use text, you need to specify the font first, for the font XNA, SpriteFont and SpriteFontTexture are used to specify the font.
SpriteFont
This font file is actually an XML configuration file used to configure the font, font size, font style, and font encoding range. Then, the font is compiled into a. xnb binary file based on this configuration.
<? 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> & amp; #32; </Start>
<End> & #126; </End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
XNA also supports Chinese characters, and the 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> & amp; #19968; </Start>
<End> & #40869; </End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
The figure shown above looks strange. Why do you need to enter such a Chinese character? This is because the Chinese character is represented by unicode (this is the first few Chinese characters in the unicode encoding table ), the encoding range is 4E00 (19968)-9FA5 (40869), that is to say, there are more than 20 thousand words, so compilation is particularly slow, sometimes it will make vs2010 no response. From this point of view, the development of XNA Chinese version of the game, the machine must be good, or even compilation can not go through.
The first XNA program Hello World!
1. Create an XNA project, add the spritefont font file to the Content project, and configure the font to be used.
Add the code to the Game1.cs file:
Protected override void Draw (GameTime gameTime)
{
Base. Draw (gameTime );
SpriteBatch. Begin ();
SpriteBatch. DrawString (Font, "Hello World !", New vector1 (100,200), Color. Red );
SpriteBatch. End ();
}
Protected override void LoadContent ()
{
Base. LoadContent ();
ContentManager cm = this. Content;
Font = cm. Load <SpriteFont> ("gamefont ");
}
Sample download: http://www.daisy123.com /? Page_id = 275 testXNAFont.zip