关于cocos2dx中文显示,流行有三种解决方案。
1、客户端全部使用UTF-8文件。
这种办法对于写在客户端上的文字还是挺有用的,但对于服务端发送过来的中文就显示乱码了,但如果是单机还是挺推荐的。可以用宏定义的方法将固定的内容定义在某个文件,如果想发展国外市场版本,可以直接拿这个文件给翻译。
2、使用iconv库。
3、使用xml文件读取。
这个对于单机游戏来说,也是不错的,省很多麻烦,特别是以后想发展国外市场需要多语言版的。配上rapidxml解析库,还是挺不错的。
关于iconv转码的用法
cocos2d_x iconv转码
作者:何卫,参考资料原文链接:http://www.cnblogs.com/hewei2012/p/3374147.html
iconv下载(Android)
ios自带libiconv.dylib,只需#include <iconv.h>即可
步骤
1.libiconv解压文件放置
直接将解压的libiconv文件夹放在cocos2dx游戏引擎cocos2d-2.1rc0-x-2.1.2目录下
libiconv中Andriod.mk文件内容为:
LOCAL_PATH:= $(call my-dir)
#libiconv.so
include $(CLEAR_VARS)
LOCAL_MODULE := libiconv_static
LOCAL_MODULE_FILENAME := libiconv
LOCAL_CFLAGS := \
-Wno-multichar \
-DAndroid \
-DLIBDIR="c" \
-DBUILDING_LIBICONV \
-DIN_LIBRARY
LOCAL_SRC_FILES := \
libcharset/lib/localcharset.c \
lib/iconv.c \
lib/relocatable.c
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/libcharset \
$(LOCAL_PATH)/lib \
$(LOCAL_PATH)/libcharset/include \
$(LOCAL_PATH)/srclib
include $(BUILD_STATIC_LIBRARY)
2.修改自己工程中的Andriod.mk文件
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static libiconv_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions) \
$(call import-module,libiconv)
蓝色为变化的内容
3.iconv.h头文件拷贝
将程序中cocos2dx\platform\third_party\win32目录下的iconv文件夹拷贝到自己工程的Classes目录下
4.程序修改
.h文件
#include "iconv/iconv.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#pragma comment(lib,"libiconv.lib")
#endif
/*---------------------------------------------------*/
// iconv转码
/*---------------------------------------------------*/
static int code_convert( const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen );
static std::string u2a( const char *inbuf );
static std::string a2u( const char *inbuf );
.cpp文件
int CCommonTool::code_convert( const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen )
{
iconv_t cd;
const char *temp = inbuf;
const char **pin = &temp;
char **pout = &outbuf;
memset(outbuf,0,outlen);
cd = iconv_open(to_charset,from_charset);
if(cd==0) return -1;
if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
/*UTF8 To GB2312*/
string CCommonTool::u2a( const char *inbuf )
{
size_t inlen = strlen(inbuf);
char * outbuf = new char[inlen * 2 + 2];
string strRet;
if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
{
strRet = outbuf;
}
delete [] outbuf;
return strRet;
}
/*GB2312 To UTF8*/
string CCommonTool::a2u( const char *inbuf )
{
size_t inlen = strlen(inbuf);
char * outbuf = new char[inlen * 2 + 2];
string strRet;
if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
{
strRet = outbuf;
}
delete [] outbuf;
return strRet;
}
程序使用
CCString ccStr;
ccStr.m_sString = CCommonTool::a2u(ccStr.m_sString.c_str());
参考资料:
http://blog.csdn.net/alex_my/article/details/10567541 cocos2dx使用iconv
http://www.cnblogs.com/hewei2012/p/3374147.html cocos2d_x iconv转码