cocos2d-x移植iconv到Android平台

前端之家收集整理的这篇文章主要介绍了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文件内容为:

  1. LOCAL_PATH:= $(call my-dir)
  2. #libiconv.so
  3. include $(CLEAR_VARS)
  4. LOCAL_MODULE := libiconv_static
  5. LOCAL_MODULE_FILENAME := libiconv
  6. LOCAL_CFLAGS := \
  7. -Wno-multichar \
  8. -DAndroid \
  9. -DLIBDIR="c" \
  10. -DBUILDING_LIBICONV \
  11. -DIN_LIBRARY
  12. LOCAL_SRC_FILES := \
  13. libcharset/lib/localcharset.c \
  14. lib/iconv.c \
  15. lib/relocatable.c
  16. LOCAL_C_INCLUDES += \
  17. $(LOCAL_PATH)/include \
  18. $(LOCAL_PATH)/libcharset \
  19. $(LOCAL_PATH)/lib \
  20. $(LOCAL_PATH)/libcharset/include \
  21. $(LOCAL_PATH)/srclib
  22. include $(BUILD_STATIC_LIBRARY)

2.修改自己工程中的Andriod.mk文件

  1. LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static libiconv_static
  2. include $(BUILD_SHARED_LIBRARY)
  3. $(call import-module,CocosDenshion/android) \
  4. $(call import-module,cocos2dx) \
  5. $(call import-$(call import-module,libiconv)
蓝色为变化的内容

3.iconv.h头文件拷贝

将程序中cocos2dx\platform\third_party\win32目录下的iconv文件夹拷贝到自己工程的Classes目录下

4.程序修改

.h文件

  1. #include iconv/iconv.h"
  2. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  3. #pragma comment(lib,"libiconv.lib")
  4. #endif
  5.  
  6. /*---------------------------------------------------*/
  7. // iconv转码
  8. 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 );
  9. static std::string u2a( char *inbuf );
  10. string a2u( char *inbuf );
.cpp文件

  1. int CCommonTool::code_convert( char *temp = inbuf;
  2. char **pin = &temp;
  3. char **pout = &outbuf;
  4. memset(outbuf,0,outlen);
  5. cd = iconv_open(to_charset,from_charset);
  6. if(cd==0) return -1;
  7. if(iconv(cd,pin,&inlen,pout,&outlen)==-1) 1;
  8. iconv_close(cd);
  9. return 0;
  10. }
  11. UTF8 To GB2312*/
  12. string CCommonTool::u2a( char *inbuf )
  13. {
  14. size_t inlen = strlen(inbuf);
  15. char * outbuf = new char[inlen * 2 + 2];
  16. string strRet;
  17. if(code_convert(utf-8",gb23122) == 0)
  18. {
  19. strRet = outbuf;
  20. }
  21. delete [] outbuf;
  22. return strRet;
  23. }
  24. GB2312 To UTF8string CCommonTool::a2u( return strRet;
  25. }

猜你在找的Cocos2d-x相关文章