Cocos2d-x win32顯示中文亂碼解決方法

歡迎轉載!轉載時請註明出處:http://blog.csdn.net/aa4790139/article/details/8118536

上一講由於沒有解決中文亂碼的問題,所以不得不研究下如何解決這個問題。

vs默認源文件的字符集是多字節字符集,既本地化語言字符集,如果你用的系統是中文系統,簡體中文,默認字符集是GBK,源碼是不包含非ASCLL碼。

要讓其在win32上正常顯示,就需要將其轉成UTF-8。下面就和大家講解解決這個問題方法。

解決方法一:函數轉換編碼

由於爲了以後開發方便,我一個單獨的類將其寫成了....

Tools.h

  1. #ifndef _TOOLS_H_  
  2. #define  _TOOLS_H_  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  7.   
  8. #include "iconv\iconv.h"  
  9.   
  10. int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);  
  11.   
  12.   
  13. #endif  
  14.   
  15. #endif  

 

Tools.cpp

  1. #include "tools.h"  
  2. #include "iconv\iconv.h"  
  3.   
  4. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  5. //字符轉換,使cocos2d-x在win32平臺支持中文顯示  
  6. int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)  
  7. {  
  8.     iconv_t iconvH;  
  9.     iconvH = iconv_open(formCode,toCode);  
  10.     if(iconvH == 0)  
  11.     {  
  12.         return -1;  
  13.     }  
  14.   
  15.     const char* strChar = gbkStr.c_str();  
  16.     const char** pin = &strChar;  
  17.   
  18.     size_t strLength = gbkStr.length();  
  19.     char* outbuf = (char*)malloc(strLength*4);  
  20.     char* pBuff = outbuf;  
  21.     memset(outbuf,0,strLength*4);  
  22.     size_t outLength = strLength*4;  
  23.     if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))  
  24.     {  
  25.         iconv_close(iconvH);  
  26.         return -1;  
  27.     }  
  28.   
  29.     gbkStr = pBuff;  
  30.     iconv_close(iconvH);  
  31.     return 0;  
  32. }  
  33. /** 
  34. **在封裝一層,直接傳入一個string,轉換後還回對應的編碼給你 
  35. */  
  36. const char* GBKToUTF(std::string &gbkStr)  
  37. {  
  38.          GBKToUTF8(gbkStr,"gbk","utf-8"); //後面兩個參數就默認了,免得後面再傳參麻煩  
  39.   
  40.          return gbkStr.c_str();  
  41. }  
  42.   
  43. #endif  


呵呵~  現在就只要使用GBKToUTF(string &gbkstr),處理後返回對應的編碼給你了..

我們再來看怎麼使用的...

(注意:對工程右鍵-> 屬性 -> 連接器 -> 輸入 -> 附加依賴項    欄目->後面有個按鈕,點擊打開,換一行加入libiconv.lib,或者在最後空一格加上libiconv.lib也行)

  1. std::string china="中文!哈哈";  
  2.         #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)    
  3.          GBKToUTF8(china,"gbk","utf-8");    
  4.         #endif   
  5.   
  6.         CCMenuItem *chinaItem = CCMenuItemFont::create(china.c_str(),this,NULL);  
  7.         chinaItem->setPosition(ccp(size.width/2,size.height/2));   
  8.         this->addChild(chinaItem);  


運行效果:

解決方法二:從外部文件讀取UTF-8

推薦大家使用資源文件進行配置保存,如xml將其採用的UTF-8的編碼方式保存,自然會讓我想到,日文、韓文等待各種國家的語言都可以正常顯示了,爲了你的軟件國際化...儘量採用這種方式吧!到時候根據手機系統的語言,然後動態的來讀取你文件中的資源...

先看下我們的xml文件:

[html] view plaincopy
  1. <dict>  
  2.     <key>chinese1</key>  
  3.     <string>美好的一天</string>  
  4.     <key>japanese</key>  
  5.     <string>良い一日を</string>  
  6.     <key>spanish</key>  
  7.     <string>Buen día</string>  
  8. </dict>  
  9. </plist>  

然後再來看如何使用:

  1. //利用CCDictionary來讀取xml  
  2.     CCDictionary *strings = CCDictionary::create("fonts/strings.xml");  
  3.     //中文,日語,西班牙語:objectForKey根據key,獲取對應的string  
  4.     const char *chinese = ((CCString*)strings->objectForKey("chinese1"))->m_sString.c_str();  
  5.     const char *japanese = ((CCString*)strings->objectForKey("japanese"))->m_sString.c_str();  
  6.     const char *spanish = ((CCString*)strings->objectForKey("spanish"))->m_sString.c_str();  
  7.   
  8.     CCLabelBMFont *label1 = CCLabelBMFont::create(spanish, "fonts/arial-unicode-26.fnt");  
  9.     addChild(label1);  
  10.     label1->setPosition(ccp(s.width/2, s.height/4*3-20));  
  11.   
  12.     CCLabelBMFont *label2 = CCLabelBMFont::create(chinese, "fonts/arial-unicode-26.fnt");  
  13.     addChild(label2);  
  14.     label2->setPosition(ccp(s.width/2, s.height/4*2));  
  15.   
  16.     CCLabelBMFont *label3 = CCLabelBMFont::create(japanese, "fonts/arial-unicode-26.fnt");  
  17.     addChild(label3);  
  18.     label3->setPosition(ccp(s.width/2, s.height/4*1));  

運行效果:

呵呵~  顯示出來了....   在此感謝大家閱覽我的博文,只是沒有看到大家的留言啊!希望也能看到大家的腳印...呵呵!
如果講述得有誤,或者不對的地方,還望各位指出!

源碼下載地址:http://download.csdn.net/detail/aa4790139/4694562

參考文章:http://blog.csdn.net/xiaoxiangp/article/details/7693343

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章