这篇文章主要介绍了cocos2d-x 2.1下如何使用CCLabelTTF显示汉字,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
需要:
1,在你的工程中添加对头文件cocos2dx\platform\third_party\win32\iconv\iconv.h的引用。
2,相应的库引用:cocos2dx\platform\third_party\win32\libraries\libiconv.lib。
代码如下:(先写两个工具函数)
bool IConvConvert(const char *from_charset, const char *to_charset, const char *inbuf, int inlen, char *outbuf, int outlen) { iconv_t cd = iconv_open(to_charset, from_charset); if (cd == 0) return false; const char **pin = &inbuf; char **pout = &outbuf; memset(outbuf,0,outlen); size_t ret = iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen); iconv_close(cd); return ret == (size_t)(-1) ? false : true; } std::string IConvConvert_GBKToUTF8(const std::string& str) { const char* textIn = str.c_str(); char textOut[256]; bool ret = IConvConvert("gb2312", "utf-8", textIn, strlen(textIn),textOut, 256); return ret ? string(textOut) : string(); }
使用方法简单:
但是,遗憾的是,cocos2d-x仅自带了适合于WIN32的iconv库,对于其中平台的没有带。
不过,不要紧,上面提到的iconv库可以轻松搞到。具体细节在http://bbs.csdn.net/topics/390285784上有介绍。我把它摘要在此:
问题已解决,现给出解决方案: 下一个iconv库放到跟Classes同级目录,然后在iconv库根目录下编写Android.mk文件,内容: LOCAL_PATH:= $(call my-dir) #libiconv.so include $(CLEAR_VARS) LOCAL_MODULE := 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) 在Classes目录下的Android.mk文件里加入: LOCAL_C_INCLUDES += \ $(LOCAL_PATH)/../iconv/include \ $(LOCAL_PATH)/../libiconv/libcharset \ $(LOCAL_PATH)/../libiconv/lib \ $(LOCAL_PATH)/../libiconv/libcharset/include \ $(LOCAL_PATH)/../libiconv/srclib \ $(LOCAL_PATH)/../iconv LOCAL_WHOLE_STATIC_LIBRARIES += libiconv $(call import-module,iconv) 这样就可以了~~ |
//! convert from wstring to UTF8 using self-coding-converting inline void WStrToUTF8(std::string& dest, const 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 situation dest.push_back(0x80| ((w >> 12) & 0x3f)); dest.push_back(0x80| ((w >> 6) & 0x3f)); dest.push_back(0x80| (w & 0x3f)); } else dest.push_back('?'); } } //! simple warpper inline std::string WStrToUTF8(const std::wstring& str){ std::string result; WStrToUTF8(result, str); return result; } // add a label shows "Hello World" // create and initialize a label std::string text = WStrToUTF8(L"你好世界"); CCLabelTTF* pLabel = CCLabelTTF::labelWithString(text.c_str(), "Arial", 24);
感谢你能够认真阅读完这篇文章,希望小编分享的“cocos2d-x 2.1下如何使用CCLabelTTF显示汉字”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。