vs默认源文件的字符集是多字节字符集,既本地化语言字符集,如果你用的系统是中文系统,简体中文,默认字符集是GBK,源码是不包含非ASCLL码。
要让其在win32上正常显示,就需要将其转成UTF-8。下面就和大家讲解解决这个问题方法。
解决方法一:函数转换编码
由于为了以后开发方便,我一个单独的类将其写成了....
Tools.h
-
#ifndef _TOOLS_H_ -
#define _TOOLS_H_ -
#include "cocos2d.h" -
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) -
#include "iconv\iconv.h" -
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode); -
#endif -
#endif
Tools.cpp
-
#include "tools.h" -
#include "iconv\iconv.h" -
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) -
//字符转换,使cocos2d-x在win32平台支持中文显示 -
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode) -
{ -
iconv_t iconvH; -
iconvH = iconv_open(formCode,toCode); -
if(iconvH == 0) -
{ -
return -1; -
} -
const char* strChar = gbkStr.c_str(); -
const char** pin = &strChar; -
size_t strLength = gbkStr.length(); -
char* outbuf = (char*)malloc(strLength*4); -
char* pBuff = outbuf; -
memset(outbuf,0,strLength*4); -
size_t outLength = strLength*4; -
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength)) -
{ -
iconv_close(iconvH); -
return -1; -
} -
gbkStr = pBuff; -
iconv_close(iconvH); -
return 0; -
} -
/** -
**在封装一层,直接传入一个string,转换后还回对应的编码给你 -
*/ -
const char* GBKToUTF(std::string &gbkStr) -
{ -
GBKToUTF8(gbkStr,"gbk","utf-8"); //后面两个参数就默认了,免得后面再传参麻烦 -
return gbkStr.c_str(); -
} -
#endif
呵呵~ 现在就只要使用GBKToUTF(string &gbkstr),处理后返回对应的编码给你了..
我们再来看怎么使用的...
(注意:对工程右键-> 属性 -> 连接器 -> 输入 -> 附加依赖项 栏目->后面有个按钮,点击打开,换一行加入libiconv.lib,或者在最后空一格加上libiconv.lib也行)
-
std::string china="中文!哈哈"; -
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) -
GBKToUTF8(china,"gbk","utf-8"); -
#endif -
CCMenuItem *chinaItem = CCMenuItemFont::create(china.c_str(),this,NULL); -
chinaItem->setPosition(ccp(size.width/2,size.height/2)); -
this->addChild(chinaItem);
运行效果:
解决方法二:从外部文件读取UTF-8
推荐大家使用资源文件进行配置保存,如xml将其采用的UTF-8的编码方式保存,自然会让我想到,日文、韩文等待各种国家的语言都可以正常显示了,为了你的软件国际化...尽量采用这种方式吧!到时候根据手机系统的语言,然后动态的来读取你文件中的资源...
先看下我们的xml文件:
-
-
chinese1 -
美好的一天 -
japanese -
良い一日を -
spanish -
Buen día -
-
然后再来看如何使用:
-
//利用CCDictionary来读取xml -
CCDictionary *strings = CCDictionary::create("fonts/strings.xml"); -
//中文,日语,西班牙语:objectForKey根据key,获取对应的string -
const char *chinese = ((CCString*)strings->objectForKey("chinese1"))->m_sString.c_str(); -
const char *japanese = ((CCString*)strings->objectForKey("japanese"))->m_sString.c_str(); -
const char *spanish = ((CCString*)strings->objectForKey("spanish"))->m_sString.c_str(); -
CCLabelBMFont *label1 = CCLabelBMFont::create(spanish, "fonts/arial-unicode-26.fnt"); -
addChild(label1); -
label1->setPosition(ccp(s.width/2, s.height/4*3-20)); -
CCLabelBMFont *label2 = CCLabelBMFont::create(chinese, "fonts/arial-unicode-26.fnt"); -
addChild(label2); -
label2->setPosition(ccp(s.width/2, s.height/4*2)); -
CCLabelBMFont *label3 = CCLabelBMFont::create(japanese, "fonts/arial-unicode-26.fnt"); -
addChild(label3); -
label3->setPosition(ccp(s.width/2, s.height/4*1));
运行效果:
呵呵~ 显示出来了.... 在此感谢大家阅览我的博文,只是没有看到大家的留言啊!希望也能看到大家的脚印...呵呵! 如果讲述得有误,或者不对的地方,还望各位指出!
源码下载地址:http://download.csdn.net/detail/aa4790139/4694562
