前端之家收集整理的这篇文章主要介绍了
cocos2d-x移植iconv到Android平台,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
iconv下载(Android)
源码下载地址,已编译完的iconv包(用这个即可)
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-$(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,char *to_charset,255); line-height:1.5!important">char *inbuf,size_t inlen,255); line-height:1.5!important">char *outbuf,size_t outlen );
- static std::string u2a( char *inbuf );
- string a2u( char *inbuf );
.cpp文件
- int CCommonTool::code_convert( char *temp = inbuf;
- 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) 1;
- iconv_close(cd);
- return 0;
- }
- UTF8 To GB2312*/
- string CCommonTool::u2a( char *inbuf )
- {
- size_t inlen = strlen(inbuf);
- char * outbuf = new char[inlen * 2 + 2];
- string strRet;
- if(code_convert(utf-8",gb23122) == 0)
- {
- strRet = outbuf;
- }
- delete [] outbuf;
- return strRet;
- }
- GB2312 To UTF8string CCommonTool::a2u( return strRet;
- }