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打包通过。
- Cocos2d-x VS2010 win32 输出中文字符的方法
- cocos2d-x 输出中文的方法
- Cocos2d-x 输出中文和版权符号的方法
- cocos2d-x 显示中文字符
- Cocos2d-x 中文显示的方法
- Cocos2d-x win32显示中文乱码解决方法
- cocos2d-x win32显示中文乱码解决方法
- cocos2d-x win32 log 中文乱码解决办法
- Win32 下的Cocos2d-x
- vs2010 中cocos2d-x 中文 乱码问题
- cocos2d-x+win32+vs2010开发中的调试打印问题
- Cocos2d—X游戏开发之VS2010 控制台输出中文,模拟器中文乱码问题解决(十八)上篇
- Cocos2d—X游戏开发之VS2010 控制台输出中文,模拟器中文乱码问题解决(十八)下篇
- cocos2d-x 添加中文方法
- cocos2d-x 添加中文方法
- cocos2d-x:读取指定目录下的文件名+解决中文乱码(win32下有效)
- 解决Cocos2d-x中文乱码的三种方法
- cocos2d-x的win32编译环境
- 『转』使用 Adobe Scout 执行准确分析
- 决定把之前的文章写一遍,哈哈
- Flash AIR开发的多媒体互动签名板程序
- CentOS 6.4 + 曙光DS200 IPSan组建FTP服务器
- git clone warning: remote HEAD refers to nonexistent ref, unable to checkout
- Cocos2d-x VS2010 win32 输出中文字符的方法
- CString.Format的详细用法(转)
- iOS UDID使用方法
- Struts 2 结果和结果类型
- python pymssql freetds,用python访问sqlserver
- Jquery Uploadify多文件上传带进度条且传递自己的参数
- oracle 11g数据库”enq: TX - row lock contention“等待事件的处理
- 近几年的开发经验:数据库设计中三大范式应用场景解析
- php批量上传图片提示Warning: Invalid argument supplied for foreach()