• Cocos2dx 绘制中文


    在cocos2d-x中要绘制中文。然后我就选择和绘制英文一样的方法进行绘制,结果如下图:

    代码如下:

    1 CCSize size = CCDirector::sharedDirector()->getWinSize();
    2 
    3         CCLabelTTF* pLabel = CCLabelTTF::create("世界你好", "Arial", 24);
    4 
    5         pLabel->setPosition(ccp(size.width / 2, size.height / 2));
    6 
    7         this->addChild(pLabel, 1);

    这是怎么一回事呢?我通过查询各种资料才知道,原来是这么回事:

    以创建一个label为例,当我们使用
    CCLabelTTF * CCLabelTTF::labelWithString(const char *label, const char *fontName, float fontSize)
    来创建label的时候,实际上这里的const char *参数期望的是UTF-8编码的字符串(你可以查看CCImage_win32.cpp中BitmapDC的drawText函数来印证我的说法),如果传入一个非UTF-8编码的字符串,那么创建的label就会显示得不太正常。
    注意,这里所说的传入参数的编码是指程序运行时传入的字符串的编码,而不是源文件保存的编码。

    好吧,知道了原因就可以来进行解决方案了,解决后如下图:

    代码如下:

    1 CCSize size = CCDirector::sharedDirector()->getWinSize();
    2 
    3         CCLabelTTF* pLabel = CCLabelTTF::create(UtfToUnicode::Unicode2Utf8(L"世界你好").c_str(), "Arial", 24);
    4 
    5         pLabel->setPosition(ccp(size.width / 2, size.height / 2));
    6 
    7         this->addChild(pLabel, 1);

    是不是发现和上面的代码基本一样,只是多了一个

    UtfToUnicode::Unicode2Utf8(L"世界你好").c_str(), 这句对“世界你好”的转换,我们是把unicode直接转换成utf8就可以实现显示中文了。

    下面我把转换的源码放出来大家共享一下。

    View Code
      1 #include "UtfToUnicode.h"
      2 
      3 //utf8 转 Unicode
      4 
      5 
      6 std::wstring UtfToUnicode::Utf82Unicode(const std::string& utf8string)
      7 {
      8     int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
      9     if (widesize == ERROR_NO_UNICODE_TRANSLATION)
     10     {
     11         throw std::exception("Invalid UTF-8 sequence.");
     12     }
     13     if (widesize == 0)
     14     {
     15         throw std::exception("Error in conversion.");
     16     }
     17 
     18     std::vector<wchar_t> resultstring(widesize);
     19 
     20     int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
     21 
     22     if (convresult != widesize)
     23     {
     24         throw std::exception("La falla!");
     25     }
     26 
     27     return std::wstring(&resultstring[0]);
     28 }
     29 
     30 
     31 //unicode 转为 ascii
     32 
     33 
     34 std::string UtfToUnicode::WideByte2Acsi(std::wstring& wstrcode)
     35 {
     36     int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
     37     if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
     38     {
     39         throw std::exception("Invalid UTF-8 sequence.");
     40     }
     41     if (asciisize == 0)
     42     {
     43         throw std::exception("Error in conversion.");
     44     }
     45     std::vector<char> resultstring(asciisize);
     46     int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
     47 
     48     if (convresult != asciisize)
     49     {
     50         throw std::exception("La falla!");
     51     }
     52 
     53     return std::string(&resultstring[0]);
     54 }
     55 
     56 
     57 
     58 
     59 
     60 //utf-8 转 ascii
     61 
     62 
     63 std::string UtfToUnicode::UTF_82ASCII(std::string& strUtf8Code)
     64 {
     65     std::string strRet("");
     66     //先把 utf8 转为 unicode
     67     std::wstring wstr = Utf82Unicode(strUtf8Code);
     68     //最后把 unicode 转为 ascii
     69     strRet = WideByte2Acsi(wstr);
     70     return strRet;
     71 }
     72 
     73 
     74 ///////////////////////////////////////////////////////////////////////
     75 
     76 
     77 //ascii 转 Unicode
     78 
     79 
     80 std::wstring UtfToUnicode::Acsi2WideByte(std::string& strascii)
     81 {
     82     int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
     83     if (widesize == ERROR_NO_UNICODE_TRANSLATION)
     84     {
     85         throw std::exception("Invalid UTF-8 sequence.");
     86     }
     87     if (widesize == 0)
     88     {
     89         throw std::exception("Error in conversion.");
     90     }
     91     std::vector<wchar_t> resultstring(widesize);
     92     int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
     93 
     94 
     95     if (convresult != widesize)
     96     {
     97         throw std::exception("La falla!");
     98     }
     99 
    100     return std::wstring(&resultstring[0]);
    101 }
    102 
    103 
    104 //Unicode 转 Utf8
    105 
    106 
    107 std::string UtfToUnicode::Unicode2Utf8(const std::wstring& widestring)
    108 {
    109     int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
    110     if (utf8size == 0)
    111     {
    112         throw std::exception("Error in conversion.");
    113     }
    114 
    115     std::vector<char> resultstring(utf8size);
    116 
    117     int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
    118 
    119     if (convresult != utf8size)
    120     {
    121         throw std::exception("La falla!");
    122     }
    123 
    124     return std::string(&resultstring[0]);
    125 }
    126 
    127 
    128 //ascii 转 Utf8
    129 
    130 
    131 std::string UtfToUnicode::ASCII2UTF_8(std::string& strAsciiCode)
    132 {
    133     std::string strRet("");
    134     //先把 ascii 转为 unicode
    135     std::wstring wstr = Acsi2WideByte(strAsciiCode);
    136     //最后把 unicode 转为 utf8
    137     strRet = Unicode2Utf8(wstr);
    138     return strRet;
    139 }
    140 
    141 UtfToUnicode::UtfToUnicode(void)
    142 {
    143 }
    144 
    145 
    146 UtfToUnicode::~UtfToUnicode(void)
    147 {
    148 }
  • 相关阅读:
    2-7
    2-6
    2-5
    2-4
    2-3
    2-1
    2-2
    1-1
    5-7
    第六章例6-1
  • 原文地址:https://www.cnblogs.com/atong/p/2965428.html
Copyright © 2020-2023  润新知