cocos2d 中文显示问题解决

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. bool HelloWorld::init()    
  2. {    
  3.     bool bRet = false;    
  4.     do     
  5.     {    
  6.         //    
  7.         // super init first    
  8.         //    
  9.     
  10.         CC_BREAK_IF(! CCLayer::init());    
  11.     
  12.         //    
  13.         // add your codes below...    
  14.         //    
  15.     
  16.         // 1. Add a menu item with "X" image, which is clicked to quit the program.    
  17.     
  18.         // Create a "close" menu item with close icon, it's an auto release object.    
  19.         CCMenuItemImage *pCloseItem = CCMenuItemImage::create(    
  20.             "CloseNormal.png",    
  21.             "CloseSelected.png",    
  22.             this,    
  23.             menu_selector(HelloWorld::menuCloseCallback));    
  24.         CC_BREAK_IF(! pCloseItem);    
  25.     
  26.         // Place the menu item bottom-right conner.    
  27.         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));    
  28.     
  29.         // Create a menu with the "close" menu item, it's an auto release object.    
  30.         CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);    
  31.         pMenu->setPosition(CCPointZero);    
  32.         CC_BREAK_IF(! pMenu);    
  33.     
  34.         // Add the menu to HelloWorld layer as a child layer.    
  35.         this->addChild(pMenu, 1);    
  36.     
  37.     //创建显示文本    
  38.         CCLabelTTF * label=CCLabelTTF::create(FontToUTF8("这是“微软雅黑”字体类型的HelloWorld"),"微软雅黑",32);    
  39.     //获取屏幕大小    
  40.     CCSize size=CCDirector::sharedDirector()->getWinSize();    
  41.     //设置文本大小和颜色    
  42.     label->setPosition(ccp(size.width/2,size.height/2));    
  43.     label->setColor(ccGREEN);    
  44.     //添加进布景    
  45.     addChild(label,1);    
  46.     
  47.         bRet = true;    
  48.     } while (0);    
  49.     
  50.     return bRet;    
  51. }    

我们还要在HelloWorld.h文件里声明用到的字符编码转换方法FontToUTF8,添加如下代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. char* FontToUTF8(const char* font);   
最后实现此方法:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. char* HelloWorld::FontToUTF8(const char* font)    
  2. {    
  3.     int len=MultiByteToWideChar(CP_ACP,0,font,-1,NULL,0);    
  4.     wchar_t* wstr=new wchar_t[len+1];    
  5.     memset(wstr,0,len+1);    
  6.     MultiByteToWideChar(CP_ACP,0,font,-1,wstr,len);    
  7.     len=WideCharToMultiByte(CP_UTF8,0,wstr,-1,NULL,0,NULL,NULL);    
  8.     char* str=new char[len+1];    
  9.     memset(str,0,len+1);    
  10.     WideCharToMultiByte(CP_UTF8,0,wstr,-1,str,len,NULL,NULL);    
  11.     if(wstr)delete[] wstr;    
  12.     return str;    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值