Cocos2d-x VS2010 win32 输出中文字符的方法

来源:互联网 发布:java高级程序设计图片 编辑:程序博客网 时间:2024/06/05 13:24

方法一:

1、添加头文件:#include "iconv/iconv.h"

2、添加附加依赖项:项目->右键“属性”,如下图所示


点击编辑,添加libiconv.lib点击“确定”保存。


3、.h文件中添加两个方法

const char* GBKToUTF(std::string &gbkStr);int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);

4、.cpp文件

const char* HelloWorld::GBKToUTF(std::string &gbkStr){this->GBKToUTF8(gbkStr,"gbk","utf-8"); //后面两个参数就默认了,免得后面再传参麻烦return gbkStr.c_str();}
int GLobalData::GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)  {  iconv_t iconvH;  iconvH = iconv_open(formCode,toCode);  if(iconvH == 0)  {  return -1;  }#if CC_TARGET_PLATFORM==CC_PLATFORM_WIN32const 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; #else//安卓平台//自定义inbuf、inlen、outlen、outbufchar* inbuf = (char*)gbkStr.c_str();size_t inlen = gbkStr.length();size_t outlen = inlen*4; char* outbuf = (char*)malloc(outlen); char* pBuff = outbuf; memset(outbuf,0,outlen); if(!iconv(iconvH, &inbuf, &inlen, &outbuf, &outlen))   {  iconv_close(iconvH);  return -1;  } gbkStr = pBuff; #endif iconv_close(iconvH);  return 0;  } 


显示中文:

std::string china="中文!哈哈";    this->GBKToUTF(china);    CCMenuItem *chinaItem = CCMenuItemFont::create(china.c_str(),this,NULL);CCMenu *menu=CCMenu::create(chinaItem,NULL);menu->setPosition(ccp(20,240)); this->addChild(menu);
显示效果:

此方法是可以打包到手机上的,详细请参考:http://blog.csdn.net/smileyuping/article/details/9635365

安卓上使用iconv的方法http://blog.sina.com.cn/s/blog_a17b071c0101lm91.html

http://www.zaojiahua.com/iconv.html

方法2:使用.XML文件

项目的Resource文件夹下有个fonts文件夹,文件夹下有个strings.xml 文件,文件内容如下:

//利用CCDictionary来读取xml CCDictionary* strings = CCDictionary::createWithContentsOfFile("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(400, 140));  CCLabelBMFont *label2 = CCLabelBMFont::create(chinese, "fonts/arial-unicode-26.fnt");  addChild(label2);  label2->setPosition(ccp(400, 240));  CCLabelBMFont *label3 = CCLabelBMFont::create(japanese, "fonts/arial-unicode-26.fnt");  addChild(label3);  label3->setPosition(ccp(400, 340));  

效果:

方法3:C代码转化

inline void WStrToUTF8(std::string& dest, const std::wstring& src){dest.clear();for (size_t i = 0; i < src.size(); i++){wchar_t w = src[i];if (w <= 0x7f)dest.push_back((char)w);else if (w <= 0x7ff){dest.push_back(0xc0 | ((w >> 6)& 0x1f));dest.push_back(0x80| (w & 0x3f));}else if (w <= 0xffff){dest.push_back(0xe0 | ((w >> 12)& 0x0f));dest.push_back(0x80| ((w >> 6) & 0x3f));dest.push_back(0x80| (w & 0x3f));}else if (sizeof(wchar_t) > 2 && w <= 0x10ffff){dest.push_back(0xf0 | ((w >> 18)& 0x07)); // wchar_t 4-bytes situationdest.push_back(0x80| ((w >> 12) & 0x3f));dest.push_back(0x80| ((w >> 6) & 0x3f));dest.push_back(0x80| (w & 0x3f));}elsedest.push_back('?');}}//! simple warpperinline std::string WStrToUTF8(const std::wstring& str){std::string result;WStrToUTF8(result, str);return result;}

使用:

std::string xiao=WStrToUTF8(L"为什么你不是处男?~o..o~");CCLabelTTF* pLabel = CCLabelTTF::create(xiao.c_str(), "Arial", TITLE_FONT_SIZE);
效果:

弊端:此方法,我在用Cygwin打包的时候不能通过(我自己不怎么会,也许高手可以)。

总结:如果您想在界面上显示汉字,可以用.XML文件,也可以用字体,这两种方法都可以在VS环境下,显示中文,也可用Cygwin打包通过。

原创粉丝点击