Learning Cocos2d-x for XNA(4)——中文显示

上讲中谈到中文显示的问题,XNA显示中文可以从马宁的Windows Phone 7开发教程(4)——XNA显示中文字体

找到解决方法,当然,既然Cocos2d-x for xna是基于XNA的游戏引擎,同样适合。

这里就详细转述一下在Cocos2d-x for xna中的使用。

添加messages.txt

在主程序Project中添加messages.txt文件

打开message.txt,添加你需要的中文,保存。

添加“内容管道扩展库”FontProcessor

右击解决方案,添加——新建项目,找到内容管道扩展库。

打开默认创建的ContentProcessor1.cs文件

复制以下代码,替换掉原来的代码

FontProcessor
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using System.IO;
using System.ComponentModel;

namespace FontProcessor
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to apply custom processing to content data, converting an object of
    /// type TInput to TOutput. The input and output types may be the same if
    /// the processor wishes to alter data without changing its type.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    ///
    /// TODO: change the ContentProcessor attribute to specify the correct
    /// display name for this processor.
    /// </summary>
    [ContentProcessor(DisplayName = "FontProcessor.ContentProcessor1")]
    public class ContentProcessor1 : FontDescriptionProcessor
    {
        public override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)
        {
            string fullPath = Path.GetFullPath(MessageFile);

            context.AddDependency(fullPath);

            string letters = File.ReadAllText(fullPath, System.Text.Encoding.UTF8);

            foreach (char c in letters)
            {
                input.Characters.Add(c);
            }

            return base.Process(input, context);
        }

        [DefaultValue("messages.txt")]
        [DisplayName("Message File")]
        [Description("The characters in this file will be automatically added to the font.")]
        public string MessageFile
        {
            get { return messageFile; }
            set { messageFile = value; }
        }
        private string messageFile = @"..\WindowsPhoneGame1\messages.txt";
    }
}

接下来,修改代码,修正路径。

路径中修改为自己工程文件的名称(本例为LearningCocos2d-xForXNA),修改后保存。

修改FontProcessor的目标框架为.Net Framework 4。

由于cocos2d-x for xna是基于.Net Framework 4,必须确保其目标框架为.Net Framework 4,当然,你也可以在创建FontProcessor时,就指定为.Net Framework 4。

右击“FontProcessor”,点击“属性”,修改目标框架。

然后编译一下FontProcessor,让其生成dll。

右击“FontProcessor”,“生成”。

添加Sprite Font字体

在这之前,首先内容管道Content添加FontProcessor引用。

在内容管道Content(本例LearningCocos2d-xForXNAContent)下,右击“引用”,点击浏览,路径“FontProcessor/bin/x86/Dubug/”,添加FontProcessor.dll,确认引用。

正式添加Sprite Font字体

在内容管道(Content)文件夹“fonts”下,右击,添加——新建项,

找到“Sprite字体”新项。取名Yahei。

点中Yahei1.spritefont文件,在其属性页中的“内容输出器”中,修改选项为FontProcessor.ContentProcessor1。

显示中文

打开我们之前的StartPageLayer.cs文件,添加如下代码。

            //中文显示
            CCLabelTTF lbl_Chinese = CCLabelTTF.labelWithString("铁道部", "Yahei", 30);//可在Yahei.spritefont纹理中修改大小
            lbl_Chinese.position = new CCPoint(size.width / 2, size.height / 1.5f);//设置lbl_Chinese的显示位置
            this.addChild(lbl_Chinese);//将其添加到Layer下

修改后代码:

StartPageLayer
 1         /// <summary>
 2         /// 构造方法
 3         /// </summary>
 4         public StartPageLayer()
 5         {
 6             //创建一个Lable标签,显示文字StartPage
 7             //Arial为内容管道中的字体纹理
 8             //30为字体大小
 9             CCLabelTTF lbl_Start = CCLabelTTF.labelWithString("StartPage", "Arial", 30);
10             CCSize size = CCDirector.sharedDirector().getWinSize();//通过CCDirector(导演)获取窗口大小
11             lbl_Start.position = new CCPoint(size.width / 2, size.height / 2);//设置lbl_Start的显示位置
12             this.addChild(lbl_Start);//将其添加到Layer下
13 
14             CCSprite img_SGQ = CCSprite.spriteWithFile("img/imgSGQ");//读取内容管道(Content)中图片
15             img_SGQ.position = new CCPoint(size.width / 2, size.height / 3);//设置图片位置
16             this.addChild(img_SGQ);//将其添加到Layer下
17 
18             //中文显示
19             CCLabelTTF lbl_Chinese = CCLabelTTF.labelWithString("铁道部", "Yahei", 30);//可在Yahei.spritefont纹理中修改大小
20             lbl_Chinese.position = new CCPoint(size.width / 2, size.height / 1.5f);//设置lbl_Chinese的显示位置
21             this.addChild(lbl_Chinese);//将其添加到Layer下
22 
23         }

注意labelWithString()方法的参数。

编译运行,显示效果

还不错,麻烦是麻烦了点,但最终还是实现了。该方法同样适合xna的中文显示。当然,如果觉得麻烦,可以变通一下,用图片代替文件也是个不错的选择,ps出来的文字可能更有吸引力。

本例源码下载

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

posted @ 2013-01-21 10:50  Ghost Soar  阅读(1646)  评论(9编辑  收藏  举报