一起学wp7 XNA游戏开发(三. 一 SpriteFont)


在游戏开发中,一般首先碰到的是文字,对于文字又需要字体的支持,在XNA中,如果要使用文字,就需要先指定字体,对于字体XNA使用了SpriteFont和 SpriteFontTexture两种方式来指定字体。

  SpriteFont

  这个字体文件实际上是一个XML的配置文件,用来配置字体、字体大小、字体样式、字体编码范围。然后在编译时会按这个配置,将字体编译成.xnb二进制文件。

<?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>&#32;</Start>
                      <End>&#126;</End>
                 </CharacterRegion>
           </CharacterRegions>
      </Asset>
</XnaContent>

  XNA也支持中文,其配置方法是一样的。

<?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>&#19968;</Start>
                      <End>&#40869;</End>
                 </CharacterRegion>
           </CharacterRegions>
      </Asset>
</XnaContent>

  上面看到的图看起来很奇怪,为什么要输入这样的中文,这是因为中文是用unicode来表示的(这是unicode编码表中的前几个汉字),其编码区间是4E00(19968)—9FA5(40869),也就是说有2万多字,所以编译起来特别的慢,有时还会让vs2010无响应。由此看来,要开发 XNA中文版的游戏,机器一定要好,要不然连编译都过不去。

  第一个XNA程序 Hello World!

  一.       创建XNA工程,在Content工程下加入spritefont字体文件,并配置好所要用的字体。

  在Game1.cs文件中,加入代码:

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”);
        }

  示例下载:http://www.daisy123.com/?page_id=275 testXNAFont.zip


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3