今天上网搜了不少方法,不过也没实践多少,倒是找到一种适合自己的方法,通过调用函数来解决字符编码的问题,解决win32平台不能显示中文的问题。还有就是如何使用字体文件,代码如下。
1 | #ifndef _GBKTOUTF8_H_ |
2 | #define _GBKTOUTF8_H_ |
3 | #include "cocos2d.h" |
4 | //包含这个头文件,iconv库是一个字符集转换的开源的库,cocos2dx支持的编码是UTF-8, |
5 | //在win32下使用的编码是GB2312,所以要转化一下,才能显示中文 |
6 | #include "iconv\iconv.h" |
7 |
8 | //记住附加依赖项libiconv.lib或者使用以下的代码 |
9 | //#pragma comment(lib, "libiconv.lib") |
10 |
11 | //就是一个函数没有分装成类 |
12 | int GBKToUTF8(std::string & gbkStr, const char * toCode, const char * fromCode); |
13 |
14 | #endif |
1 | #include "GBKToUTF8.h" |
2 |
3 | //以下函数是从网上copy的,试了一下可以解决问题 |
4 | int GBKToUTF8(std::string & gbkStr, const char * fromCode, const char * toCode) |
5 | { |
6 | iconv_t iconvH; |
7 | //这个函数调用的作用是将formCode的编码转换成toCode的编码,我们一般调用的时候是GB2312 UTF-8 |
8 | //因为系统又的编码支持有的不支持所以就有可能返回0 |
9 | iconvH = iconv_open(toCode,fromCode); |
10 |
11 | //如果返回值为0代表不可以转化 |
12 | if (iconvH == 0) |
13 | { |
14 | return -1; |
15 | } |
16 |
17 | //将string类型转化为char *类型 |
18 | const char * strChar = gbkStr.c_str(); |
19 | //以下是基础不说了 |
20 | const char ** pin = &strChar; |
21 | size_t strLength = gbkStr.length(); |
22 | char * outbuf = ( char *) malloc (strLength*4); |
23 | char * pBuff = outbuf; |
24 |
25 | //这里GB2312和UTF-8的位数是1:4不明白,知道的人说一下 |
26 | memset (outbuf, 0, strLength*4); |
27 | size_t outLength = strLength*4; |
28 |
29 | //第二个参数表示转化前字符的地址,以后的参数分别是转化前字符的长度,转化后的存储地址,转化后的长度 |
30 | if (-1 == iconv(iconvH, pin, &strLength, &outbuf, &outLength)) |
31 | { |
32 | free (pBuff); |
33 | iconv_close(iconvH); |
34 | return -1; |
35 | } |
36 |
37 | gbkStr = pBuff; |
38 | iconv_close(iconvH); |
39 |
40 | return 0; |
41 | } |
1 | #include "HelloWorldScene.h" |
2 | //用python建立的工程以下的包含可能有问题,请到工程的属性设置中附加包含目录,把路径写进去,其实就是个. |
3 | #include "GBKToUTF8.h" |
4 |
5 | USING_NS_CC; |
6 |
7 | CCScene* HelloWorld::scene() |
8 | { |
9 | // 'scene' is an autorelease object |
10 | CCScene *scene = CCScene::create(); |
11 |
12 | // 'layer' is an autorelease object |
13 | HelloWorld *layer = HelloWorld::create(); |
14 |
15 | // add layer as a child to scene |
16 | scene->addChild(layer); |
17 |
18 | // return the scene |
19 | return scene; |
20 | } |
21 |
22 | // on "init" you need to initialize your instance |
23 | bool HelloWorld::init() |
24 | { |
25 | if ( !CCLayer::init() ) |
26 | { |
27 | return false ; |
28 | } |
29 |
30 | CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); |
31 | //对字体文件的测试 |
32 | CCLabelTTF * ttf1 = CCLabelTTF::create( "china" , "Marker Felt.ttf" ,24); |
33 | //第二个参数加上字体文件的路径和名称 |
34 | CCLabelTTF * ttf2 = CCLabelTTF::create( "0123456789" , "Paint Boy.ttf" ,24); |
35 | ttf1->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); |
36 | ttf2->setPosition(ccp(visibleSize.width/2,visibleSize.height/4)); |
37 | this ->addChild(ttf1); |
38 | this ->addChild(ttf2); |
39 |
40 | //测试中文 |
41 | std::string str = "中文版的HelloWorld" ; |
42 | GBKToUTF8(str, "GB2312" , "UTF-8" ); |
43 | //没写字体的话就使用平台默认的字体 |
44 | CCLabelTTF * ttf3 = CCLabelTTF::create(str.c_str(), "" ,24); |
45 | ttf3->setPosition(ccp(visibleSize.width/2,visibleSize.height*2/3)); |
46 | this ->addChild(ttf3); |
47 |
48 | return true ; |
49 | } |
这个项目移植android平台的时候会出现问题的,因为使用了第三方的库,我们需要采取一些措施才行,请参考我的文章cocos2dx使用了第三方库照样移植android平台-解决iconv库的移植问题。有问题欢迎留言。