• 一起学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

  • 相关阅读:
    [置顶] 内外网同时访问,我的拿来主义
    Nginx防攻击工具教程一 ngx_http_limit_conn_module
    晒晒我的厨艺修炼成果
    在 javascript 中,为什么 [1,2] + [3,4] 不等于 [1,2,3,4]?
    无法解析的外部符号__imp__AlphaBlend@44的解决
    Win32 API实现CDC类的FillSolidRect接口
    pugixml库学习之添加节点
    cleanup failed because the file not under version control问题的解决
    JavaScript 的 typeof 的用途
    支持在Win7和XP系统上创建环境变量的批处理文件
  • 原文地址:https://www.cnblogs.com/randylee/p/1851966.html
Copyright © 2020-2023  润新知