Cocos2d-x 在屏幕上渲染文字

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

 

原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/8268814

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

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

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

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

  1. eglView->setFrameSize(800, 480);//设置界面大小
eglView->setFrameSize(800, 480);//设置界面大小


在AppDelegate.cpp的applicationDidFinishLaunching函数中

  1. pDirector->setDisplayStats(0);
 pDirector->setDisplayStats(0);

不显示FPS

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

  1. bool HelloWorld::init()
  2. {
  3. bool bRet = false;
  4. do
  5. {
  6. //
  7. // super init first
  8. //
  9. CC_BREAK_IF(! CCLayer::init());
  10. //
  11. // add your codes below...
  12. //
  13. // 1. Add a menu item with "X" image, which is clicked to quit the program.
  14. // Create a "close" menu item with close icon, it's an auto release object.
  15. /*CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  16. "CloseNormal.png",
  17. "CloseSelected.png",
  18. this,
  19. menu_selector(HelloWorld::menuCloseCallback));
  20. CC_BREAK_IF(! pCloseItem);
  21. // Place the menu item bottom-right conner.
  22. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
  23. // Create a menu with the "close" menu item, it's an auto release object.
  24. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  25. pMenu->setPosition(CCPointZero);
  26. CC_BREAK_IF(! pMenu);
  27. // Add the menu to HelloWorld layer as a child layer.
  28. this->addChild(pMenu, 1);
  29. // 2. Add a label shows "Hello World".
  30. // Create a label and initialize with string "Hello World".
  31. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
  32. CC_BREAK_IF(! pLabel);
  33. // Get window size and place the label upper.
  34. CCSize size = CCDirector::sharedDirector()->getWinSize();
  35. pLabel->setPosition(ccp(size.width / 2, size.height - 50));
  36. // Add the label to HelloWorld layer as a child layer.
  37. this->addChild(pLabel, 1);
  38. // 3. Add add a splash screen, show the cocos2d splash image.
  39. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  40. CC_BREAK_IF(! pSprite);
  41. // Place the sprite on the center of the screen
  42. pSprite->setPosition(ccp(size.width/2, size.height/2));
  43. // Add the sprite to HelloWorld layer as a child layer.
  44. this->addChild(pSprite, 0);
  45. */
  46. bRet = true;
  47. } while (0);
  48. return bRet;
  49. }
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //
        // super init first
        //

        CC_BREAK_IF(! CCLayer::init());

        //
        // add your codes below...
        //

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        /*CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        // 2. Add a label shows "Hello World".

        // Create a label and initialize with string "Hello World".
        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1);

        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);
		*/

        bRet = true;
    } while (0);

    return bRet;
}


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


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

  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);
		CCLabelTTF *labelWelcome0=CCLabelTTF::create("Welcome","微软雅黑",48);
		CCLabelTTF *labelWelcome1=CCLabelTTF::create("Welcome","宋体",48);
		CCLabelTTF *labelWelcome2=CCLabelTTF::create("Welcome","Consolas",48);
		CCLabelTTF *labelWelcome3=CCLabelTTF::create("Welcome","Bitstream Vera Sans Mono",48);


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

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

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

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

  1. CCSize size=CCDirector::sharedDirector()->getWinSize();
  2. float width=size.width;
  3. float height=size.height;
		CCSize size=CCDirector::sharedDirector()->getWinSize();
		float width=size.width;
		float height=size.height;


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

  1. CCSize s0=labelWelcome0->getContentSize();
  2. CCSize s1=labelWelcome1->getContentSize();
  3. CCSize s2=labelWelcome2->getContentSize();
  4. CCSize s3=labelWelcome3->getContentSize();
		CCSize s0=labelWelcome0->getContentSize();
		CCSize s1=labelWelcome1->getContentSize();
		CCSize s2=labelWelcome2->getContentSize();
		CCSize s3=labelWelcome3->getContentSize();

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

 

  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);
		labelWelcome0->setPosition(CCPointMake(s0.width/2,size.height-s0.height/2));
		labelWelcome1->setPosition(ccp(size.width-s1.width/2,size.height-s1.height/2));
		labelWelcome2->setPosition(CCPoint(s2.width/2,s2.height/2));
		labelWelcome3->setPosition(ccp(size.width-s3.width/2,s3.height/2));
		//labelWelcome3->setPosition(CCPointZero);



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

最后将文字加到布景中:

  1. addChild(labelWelcome0,1);
  2. addChild(labelWelcome1,1);
  3. addChild(labelWelcome2,1);
  4. addChild(labelWelcome3,1);
		addChild(labelWelcome0,1);
		addChild(labelWelcome1,1);
		addChild(labelWelcome2,1);
		addChild(labelWelcome3,1);

现在,界面如下:

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

  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. c1.r=rand()%256;
  7. c1.g=rand()%256;
  8. c1.b=rand()%256;
  9. c2.r=rand()%256;
  10. c2.g=rand()%256;
  11. c2.b=rand()%256;
  12. c3.r=rand()%256;
  13. c3.g=rand()%256;
  14. c3.b=rand()%256;
                  srand((unsigned) time(NULL));
		ccColor3B c0,c1,c2,c3,c4;
		c0.r=rand()%256;
		c0.g=rand()%256;
		c0.b=rand()%256;

		c1.r=rand()%256;
		c1.g=rand()%256;
		c1.b=rand()%256;

		c2.r=rand()%256;
		c2.g=rand()%256;
		c2.b=rand()%256;

		c3.r=rand()%256;
		c3.g=rand()%256;
		c3.b=rand()%256;

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

  1. labelWelcome0->setColor(c0);
  2. labelWelcome1->setColor(c1);
  3. labelWelcome2->setColor(c2);
  4. labelWelcome3->setColor(c3);
                  labelWelcome0->setColor(c0);
		labelWelcome1->setColor(c1);
		labelWelcome2->setColor(c2);
		labelWelcome3->setColor(c3);


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

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

  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. }
char* HelloWorld::G2U(const char* gb2312)  
{  
 int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);  
 wchar_t* wstr = new wchar_t[len+1];  
 memset(wstr, 0, len+1);  
 MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);  
 len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);  
 char* str = new char[len+1];  
 memset(str, 0, len+1);  
 WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);  
 if(wstr) delete[] wstr;  
 return str;  
}



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

 

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


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

最后init方法的全部代码:

  1. bool HelloWorld::init()
  2. {
  3. bool bRet = false;
  4. do
  5. {
  6. //
  7. // super init first
  8. //
  9. CC_BREAK_IF(! CCLayer::init());
  10. //
  11. // add your codes below...
  12. //
  13. // 1. Add a menu item with "X" image, which is clicked to quit the program.
  14. // Create a "close" menu item with close icon, it's an auto release object.
  15. /*CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  16. "CloseNormal.png",
  17. "CloseSelected.png",
  18. this,
  19. menu_selector(HelloWorld::menuCloseCallback));
  20. CC_BREAK_IF(! pCloseItem);
  21. // Place the menu item bottom-right conner.
  22. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
  23. // Create a menu with the "close" menu item, it's an auto release object.
  24. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  25. pMenu->setPosition(CCPointZero);
  26. CC_BREAK_IF(! pMenu);
  27. // Add the menu to HelloWorld layer as a child layer.
  28. this->addChild(pMenu, 1);
  29. // 2. Add a label shows "Hello World".
  30. // Create a label and initialize with string "Hello World".
  31. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
  32. CC_BREAK_IF(! pLabel);
  33. // Get window size and place the label upper.
  34. CCSize size = CCDirector::sharedDirector()->getWinSize();
  35. pLabel->setPosition(ccp(size.width / 2, size.height - 50));
  36. // Add the label to HelloWorld layer as a child layer.
  37. this->addChild(pLabel, 1);
  38. // 3. Add add a splash screen, show the cocos2d splash image.
  39. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  40. CC_BREAK_IF(! pSprite);
  41. // Place the sprite on the center of the screen
  42. pSprite->setPosition(ccp(size.width/2, size.height/2));
  43. // Add the sprite to HelloWorld layer as a child layer.
  44. this->addChild(pSprite, 0);
  45. */
  46. CCLabelTTF *labelWelcome0=CCLabelTTF::create("Welcome","微软雅黑",48);
  47. CCLabelTTF *labelWelcome1=CCLabelTTF::create("Welcome","宋体",48);
  48. CCLabelTTF *labelWelcome2=CCLabelTTF::create("Welcome","Consolas",48);
  49. CCLabelTTF *labelWelcome3=CCLabelTTF::create("Welcome","Bitstream Vera Sans Mono",48);
  50. CCLabelTTF *labelWelcome4=CCLabelTTF::create(G2U("游戏开发的世界,我用Cocos2d-x来降服你了~!"),"Verdana",35);
  51. CCSize size=CCDirector::sharedDirector()->getWinSize();
  52. float width=size.width;
  53. float height=size.height;
  54. CCSize s0=labelWelcome0->getContentSize();
  55. CCSize s1=labelWelcome1->getContentSize();
  56. CCSize s2=labelWelcome2->getContentSize();
  57. CCSize s3=labelWelcome3->getContentSize();
  58. labelWelcome0->setPosition(CCPointMake(s0.width/2,size.height-s0.height/2));
  59. labelWelcome1->setPosition(ccp(size.width-s1.width/2,size.height-s1.height/2));
  60. labelWelcome2->setPosition(CCPoint(s2.width/2,s2.height/2));
  61. labelWelcome3->setPosition(ccp(size.width-s3.width/2,s3.height/2));
  62. //labelWelcome3->setPosition(CCPointZero);
  63. srand((unsigned) time(NULL));
  64. ccColor3B c0,c1,c2,c3,c4;
  65. c0.r=rand()%256;
  66. c0.g=rand()%256;
  67. c0.b=rand()%256;
  68. c1.r=rand()%256;
  69. c1.g=rand()%256;
  70. c1.b=rand()%256;
  71. c2.r=rand()%256;
  72. c2.g=rand()%256;
  73. c2.b=rand()%256;
  74. c3.r=rand()%256;
  75. c3.g=rand()%256;
  76. c3.b=rand()%256;
  77. c4.r=rand()%256;
  78. c4.g=rand()%256;
  79. c4.b=rand()%256;
  80. labelWelcome0->setColor(c0);
  81. labelWelcome1->setColor(c1);
  82. labelWelcome2->setColor(c2);
  83. labelWelcome3->setColor(c3);
  84. labelWelcome4->setPosition(ccp(size.width/2,size.height/2));
  85. labelWelcome4->setColor(c4);
  86. addChild(labelWelcome0,1);
  87. addChild(labelWelcome1,1);
  88. addChild(labelWelcome2,1);
  89. addChild(labelWelcome3,1);
  90. addChild(labelWelcome4,1);
  91. bRet = true;
  92. } while (0);
  93. return bRet;
  94. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值