In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use CCLabelTTF to display Chinese characters under cocos2d-x 2.1. it has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Solution 1: use the iconv library that comes with cocos2d-x (under WIN32) and download one.
Need to:
1. Add a reference to the header file cocos2dx\ platform\ third_party\ win32\ iconv\ iconv.h in your project.
2, the corresponding library reference: cocos2dx\ platform\ third_party\ win32\ libraries\ libiconv.lib.
The code is as follows: (write two tool functions first)
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 ();}
It is easy to use:
Unfortunately, however, cocos2d-x only comes with an iconv library suitable for WIN32, not for the platform.
However, it doesn't matter, the iconv library mentioned above is easy to get. Details are described on http://bbs.csdn.net/topics/390285784. I summarize it here:
The problem has been solved and the solution is given:
Place the next iconv library in the same level as Classes, and then write the Android.mk file in the root directory of the iconv library, with the following contents:
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)
Add the following to the Android.mk file in the Classes directory:
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)
This is OK ~ ~ program 2: combined with the above principles, self-development (highly recommended) /! Convert from wstring to UTF8 using self-coding-convertinginline 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 6)& 0x1f));dest.push_back(0x80| (w & 0x3f));}else if (w >12) & 0x0f); dest.push_back (0x80 | (w > 6) & 0x3f); dest.push_back (0x80 | (w & 0x3f));} else if (sizeof (wchar_t) > 2 & & w > 18) & 0x07)); / / wchar_t 4-bytes situationdest.push_back (0x80 | (w > 12) & 0x3f)); dest.push_back (0x80 | (w > 6) & 0x3f)); dest.push_back (0x80 | (w & 0x3f)) } elsedest.push_back ('?');}} /! Simple warpperinline 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 labelstd::string text = WStrToUTF8 (L "Hello World"); CCLabelTTF* pLabel = CCLabelTTF::labelWithString (text.c_str (), "Arial", 24) Thank you for reading this article carefully. I hope the article "how to use CCLabelTTF to display Chinese characters under cocos2d-x 2.1" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.