cocos2d-x解决中文乱码问题的几种办法

来源:互联网 发布:杨振宁 丘成桐 知乎 编辑:程序博客网 时间:2024/04/28 08:21

cocos2d-x解决中文乱码问题的几种办法

昨天改写cocos2d-x的例程,想在其基础上加上一个计分系统。

没有分数实在让人没有玩下去的动力!


我在主场景上加上了一个CCLabelTTF,用于显示分数。

但是意外的发现,当内容含有中文时,CCLabelTTF的显示内容为乱码。


无奈只好Google百度寻求答案,明白了这个问题的缘由。

因为cocos2d-x内部是以utf8处理文本的,而VS直接输入时文本编码为GBK,如果添加L标志,则为Unicode编码。


解决这个问题有三种办法:

  1. 将源代码文件保存为utf8编码,不过由于编译器的问题,这种方式会导致很多无法预测的问题

  2. 将字符串用utf8编码集中存到一文件中,然后用代码读取这些字符串来使用,这种办法还能很好的支持多语言版本

  3. 使用字符串时,先将其转换为utf8编码


我最终使用了第三种方法,第一种撇开不说,第二种实现起来比较麻烦,第三种则要方便很多。


一般在windows上,我们使用API MultiByteToWideChar来进行各种编码转换。

不过这东西只能在Windows上用,在cocos2d-x上用就有点不合时宜的感觉,毕竟安卓上可没这个API。

还好cocos2d-x考虑很周到,它自带了一个iconv库

只需要在项目附加依赖项里加入libiconv.lib,并且包含头文件iconv/iconv.h即可使用。

我通过这个库封装了几个编码转换的函数,代码如下


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Tool.h"
     
int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
    iconv_t cd;
    const char *temp = inbuf;
    const char **pin = &temp;
    char **pout = &outbuf;
    memset(outbuf,0,outlen);
    cd = iconv_open(to_charset,from_charset);
    if(cd==0) return -1;
    if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
    iconv_close(cd);
    return 0;
}
     
/*UTF8转为GB2312*/
std::string u2a(const char *inbuf)
{
    size_t inlen = strlen(inbuf);
    char * outbuf = new char[inlen * 2 + 2];
    std::string strRet;
    if(code_convert("utf-8""gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
    {
        strRet = outbuf;
    }
    delete [] outbuf;
    return strRet;
}
     
/*GB2312转为UTF8*/
std::string a2u(const char *inbuf)
{
    size_t inlen = strlen(inbuf);
    char * outbuf = new char[inlen * 2 + 2];
    std::string strRet;
    if(code_convert("gb2312""utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
    {
        strRet = outbuf;
    }
    delete [] outbuf;
    return strRet;
}


然后在每次要使用中文前,用a2u函数将文本转换为utf-8编码,使用例程如下:


1
2
3
4
//刷新分数显示
char buff[1024];
sprintf_s(buff, 1024, "得分:%d", _Score);
_pLabelScore->setString(a2u(buff).c_str());


至此,我们可以在cocos2d-x中正常显示中文了!


转载地址:http://hi.baidu.com/coofly/item/dcb911f363cba1763c198b7b


原创粉丝点击