Learn wp7 XNA game development together (III. 1 SpriteFont)

Source: Internet
Author: User
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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.