(5)Cocos2d-x在屏幕上渲染文字【转载】

大笑国际惯例,打个小广告,新手cocos2dx游戏开发学习群:131430516,欢迎各位一起加入讨论交流,本人QQ692187014

转载:http://blog.csdn.net/zhy_cheng/article/details/8268814

本来打算自己写的,后来发现这篇教程讲的很详细,那么就取之了。


这篇博文主要展示在屏幕上渲染文字,先来看看最后要实现的界面:

 

 

 

 

 

这个界面就是在屏幕的四个角落有不同字体不同颜色的Welcome来渲染,在屏幕的中间是汉字,颜色也是随机的。

 

 

下面来看看代码是怎么样实现的。

 

 

在main.cpp中将屏幕的大小设置为

[cpp]  view plain copy
  1. eglView->setFrameSize(800, 480);//设置界面大小  


在AppDelegate.cpp的applicationDidFinishLaunching函数中

[cpp]  view plain copy
  1. pDirector->setDisplayStats(0);  

不显示FPS

 

在HelloWorldSecne.cpp中,将init方法中的代码注释一部分,如下:

[cpp]  view plain copy
  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.         // 2. Add a label shows "Hello World". 
  38.  
  39.         // Create a label and initialize with string "Hello World". 
  40.         CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); 
  41.         CC_BREAK_IF(! pLabel); 
  42.  
  43.         // Get window size and place the label upper.  
  44.         CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  45.         pLabel->setPosition(ccp(size.width / 2, size.height - 50)); 
  46.  
  47.         // Add the label to HelloWorld layer as a child layer. 
  48.         this->addChild(pLabel, 1); 
  49.  
  50.         // 3. Add add a splash screen, show the cocos2d splash image. 
  51.         CCSprite* pSprite = CCSprite::create("HelloWorld.png"); 
  52.         CC_BREAK_IF(! pSprite); 
  53.  
  54.         // Place the sprite on the center of the screen 
  55.         pSprite->setPosition(ccp(size.width/2, size.height/2)); 
  56.  
  57.         // Add the sprite to HelloWorld layer as a child layer. 
  58.         this->addChild(pSprite, 0); 
  59.         */  
  60.   
  61.         bRet = true;  
  62.     } while (0);  
  63.   
  64.     return bRet;  
  65. }  


现在项目运行起来应该是这样的:


好了,下面开始写自己的代码

 

[cpp]  view plain copy
  1. CCLabelTTF *labelWelcome0=CCLabelTTF::create("Welcome","微软雅黑",48);  
  2. CCLabelTTF *labelWelcome1=CCLabelTTF::create("Welcome","宋体",48);  
  3. CCLabelTTF *labelWelcome2=CCLabelTTF::create("Welcome","Consolas",48);  
  4. CCLabelTTF *labelWelcome3=CCLabelTTF::create("Welcome","Bitstream Vera Sans Mono",48);  


这是“Welcome”的构造,使用create方法

第一个参数是const char *,表示要渲染的文字

第二个参数是const char*,表示文字的字体

第三个参数是float,表示字体的大小

 

 

[cpp]  view plain copy
  1. CCSize size=CCDirector::sharedDirector()->getWinSize();  
  2. float width=size.width;  
  3. float height=size.height;  


这里使用CCDirector获取屏幕的大小。

 

[cpp]  view plain copy
  1. CCSize s0=labelWelcome0->getContentSize();  
  2. CCSize s1=labelWelcome1->getContentSize();  
  3. CCSize s2=labelWelcome2->getContentSize();  
  4. CCSize s3=labelWelcome3->getContentSize();  

这里获取要渲染的文字的大小

[cpp]  view plain copy
  1. labelWelcome0->setPosition(CCPointMake(s0.width/2,size.height-s0.height/2));  
  2. labelWelcome1->setPosition(ccp(size.width-s1.width/2,size.height-s1.height/2));  
  3. labelWelcome2->setPosition(CCPoint(s2.width/2,s2.height/2));  
  4. labelWelcome3->setPosition(ccp(size.width-s3.width/2,s3.height/2));  
  5. //labelWelcome3->setPosition(CCPointZero);  



这里设置文字显示的位置,这里的CCPointMake,ccp,CCPoint都是传递两个float参数指定显示的地点,指定的是文字中心点的地点,CCPointZero是(0,,0)点。

 

最后将文字加到布景中:

[cpp]  view plain copy
  1. addChild(labelWelcome0,1);  
  2. addChild(labelWelcome1,1);  
  3. addChild(labelWelcome2,1);  
  4. addChild(labelWelcome3,1);  

 

现在,界面如下:

 

接下来为文字设置随机的颜色

 

[cpp]  view plain copy
  1.                 srand((unsigned) time(NULL));  
  2. ccColor3B c0,c1,c2,c3,c4;  
  3. c0.r=rand()%256;  
  4. c0.g=rand()%256;  
  5. c0.b=rand()%256;  
  6.   
  7. c1.r=rand()%256;  
  8. c1.g=rand()%256;  
  9. c1.b=rand()%256;  
  10.   
  11. c2.r=rand()%256;  
  12. c2.g=rand()%256;  
  13. c2.b=rand()%256;  
  14.   
  15. c3.r=rand()%256;  
  16. c3.g=rand()%256;  
  17. c3.b=rand()%256;  

这里随机设置颜色,使用了C++中的随机数。接下来

[cpp]  view plain copy
  1.                 labelWelcome0->setColor(c0);  
  2. labelWelcome1->setColor(c1);  
  3. labelWelcome2->setColor(c2);  
  4. labelWelcome3->setColor(c3);  


这段代码在将文字添加到布景之前设置。好了,现在的界面是下面的:

 

最后要添加中间的文字了,中间的文字是中文,Windows默认的字符集是GB2312,而Cocos2d-x使用的是UTF-8,所以要将我们的字符先转为UTF-8,转换方法是使用下面的函数:

 

[cpp]  view plain copy
  1. char* HelloWorld::G2U(const char* gb2312)    
  2. {    
  3.  int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);    
  4.  wchar_t* wstr = new wchar_t[len+1];    
  5.  memset(wstr, 0, len+1);    
  6.  MultiByteToWideChar(CP_ACP, 0, gb2312, -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;    
  13. }  



 

现在可以将中间的文字显示出来了:

[cpp]  view plain copy
  1. CCLabelTTF *labelWelcome4=CCLabelTTF::create(G2U("游戏开发的世界,我用Cocos2d-x来降服你了~!"),"Verdana",35);  


最后,最后就是我们前面的效果了。

最后init方法的全部代码:

[cpp]  view plain copy
  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.         // 2. Add a label shows "Hello World". 
  38.  
  39.         // Create a label and initialize with string "Hello World". 
  40.         CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); 
  41.         CC_BREAK_IF(! pLabel); 
  42.  
  43.         // Get window size and place the label upper.  
  44.         CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  45.         pLabel->setPosition(ccp(size.width / 2, size.height - 50)); 
  46.  
  47.         // Add the label to HelloWorld layer as a child layer. 
  48.         this->addChild(pLabel, 1); 
  49.  
  50.         // 3. Add add a splash screen, show the cocos2d splash image. 
  51.         CCSprite* pSprite = CCSprite::create("HelloWorld.png"); 
  52.         CC_BREAK_IF(! pSprite); 
  53.  
  54.         // Place the sprite on the center of the screen 
  55.         pSprite->setPosition(ccp(size.width/2, size.height/2)); 
  56.  
  57.         // Add the sprite to HelloWorld layer as a child layer. 
  58.         this->addChild(pSprite, 0); 
  59.         */  
  60.         CCLabelTTF *labelWelcome0=CCLabelTTF::create("Welcome","微软雅黑",48);  
  61.         CCLabelTTF *labelWelcome1=CCLabelTTF::create("Welcome","宋体",48);  
  62.         CCLabelTTF *labelWelcome2=CCLabelTTF::create("Welcome","Consolas",48);  
  63.         CCLabelTTF *labelWelcome3=CCLabelTTF::create("Welcome","Bitstream Vera Sans Mono",48);  
  64.         CCLabelTTF *labelWelcome4=CCLabelTTF::create(G2U("游戏开发的世界,我用Cocos2d-x来降服你了~!"),"Verdana",35);  
  65.   
  66.           
  67.   
  68.   
  69.         CCSize size=CCDirector::sharedDirector()->getWinSize();  
  70.         float width=size.width;  
  71.         float height=size.height;  
  72.   
  73.   
  74.         CCSize s0=labelWelcome0->getContentSize();  
  75.         CCSize s1=labelWelcome1->getContentSize();  
  76.         CCSize s2=labelWelcome2->getContentSize();  
  77.         CCSize s3=labelWelcome3->getContentSize();  
  78.   
  79.         labelWelcome0->setPosition(CCPointMake(s0.width/2,size.height-s0.height/2));  
  80.         labelWelcome1->setPosition(ccp(size.width-s1.width/2,size.height-s1.height/2));  
  81.         labelWelcome2->setPosition(CCPoint(s2.width/2,s2.height/2));  
  82.         labelWelcome3->setPosition(ccp(size.width-s3.width/2,s3.height/2));  
  83.         //labelWelcome3->setPosition(CCPointZero);  
  84.   
  85.         srand((unsigned) time(NULL));  
  86.         ccColor3B c0,c1,c2,c3,c4;  
  87.         c0.r=rand()%256;  
  88.         c0.g=rand()%256;  
  89.         c0.b=rand()%256;  
  90.   
  91.         c1.r=rand()%256;  
  92.         c1.g=rand()%256;  
  93.         c1.b=rand()%256;  
  94.   
  95.         c2.r=rand()%256;  
  96.         c2.g=rand()%256;  
  97.         c2.b=rand()%256;  
  98.   
  99.         c3.r=rand()%256;  
  100.         c3.g=rand()%256;  
  101.         c3.b=rand()%256;  
  102.   
  103.         c4.r=rand()%256;  
  104.         c4.g=rand()%256;  
  105.         c4.b=rand()%256;  
  106.   
  107.   
  108.         labelWelcome0->setColor(c0);  
  109.         labelWelcome1->setColor(c1);  
  110.         labelWelcome2->setColor(c2);  
  111.         labelWelcome3->setColor(c3);  
  112.   
  113.         labelWelcome4->setPosition(ccp(size.width/2,size.height/2));  
  114.         labelWelcome4->setColor(c4);  
  115.   
  116.   
  117.         addChild(labelWelcome0,1);  
  118.         addChild(labelWelcome1,1);  
  119.         addChild(labelWelcome2,1);  
  120.         addChild(labelWelcome3,1);  
  121.         addChild(labelWelcome4,1);  
  122.   
  123.         bRet = true;  
  124.     } while (0);  
  125.   
  126.     return bRet;  
  127. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值