cocos2d-x在win32开发中,不能直接显示中文,需要转字符。
cocos2d-x已经自带了一个对应的库iconv。如果要使用它,我们要在做以下配置
1.右键项目->属性->附加包含目录->编辑。然后新增一个路径,我的如下
D:\cocos2d-x\cocos2d-2.0-x-2.0.4\cocos2d-2.0-x-2.0.4\cocos2dx\platform\third_party\win32\iconv
2.右键项目->属性->连接器->输入->附加依赖库
增加libiconv.lib
这样就配置完了,不保证我记忆的很准确,如果有错误,可参考别人的调试下。
然后转化代码如下
#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* fromCode ,const char* toCode);
void* GBKToUTF8(std::string &gbkStr);
#endif
#endif
#include "tools.h"
#include "iconv\iconv.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int GBKToUTF8(std::string &gbkStr,const char* fromCode,const char* toCode)
{
iconv_t iconvH;
iconvH = iconv_open(toCode,fromCode);
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* GBKToUTF8(std::string &gbkStr)
//{
// GBKToUTF8(gbkStr,"gbk","utf-8"); //后面两个参数就默认了,免得后面再传参麻烦
//
// return gbkStr.c_str();
//}
void* GBKToUTF8(std::string &gbkStr)
{
GBKToUTF8(gbkStr,"gbk","utf-8"); //后面两个参数就默认了,免得后面再传参麻烦
return NULL;
}
#endif
参考文章:http://blog.youkuaiyun.com/aa4790139/article/details/8118536
该作者用的是fnt字体,只能显示fnt文件里有的中文,所以大家使用时要注意区分