Learn WP7 XNA game development (three. a spritefont)

Source: Internet
Author: User
Tags add end version xmlns
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



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.