这篇博文主要展示在屏幕上渲染文字,先来看看最后要实现的界面:
这个界面就是在屏幕的四个角落有不同字体不同颜色的Welcome来渲染,在屏幕的中间是汉字,颜色也是随机的。
下面来看看代码是怎么样实现的。
在main.cpp中将屏幕的大小设置为
- eglView->setFrameSize(800, 480);//设置界面大小
在AppDelegate.cpp的applicationDidFinishLaunching函数中
- pDirector->setDisplayStats(0);
不显示FPS
在HelloWorldSecne.cpp中,将init方法中的代码注释一部分,如下:
- 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;
- }
现在项目运行起来应该是这样的:
好了,下面开始写自己的代码
- 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,表示字体的大小
- CCSize size=CCDirector::sharedDirector()->getWinSize();
- float width=size.width;
- float height=size.height;
这里使用CCDirector获取屏幕的大小。
- CCSize s0=labelWelcome0->getContentSize();
- CCSize s1=labelWelcome1->getContentSize();
- CCSize s2=labelWelcome2->getContentSize();
- CCSize s3=labelWelcome3->getContentSize();
这里获取要渲染的文字的大小
- 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)点。
最后将文字加到布景中:
- addChild(labelWelcome0,1);
- addChild(labelWelcome1,1);
- addChild(labelWelcome2,1);
- addChild(labelWelcome3,1);
现在,界面如下:
接下来为文字设置随机的颜色
- 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++中的随机数。接下来
- labelWelcome0->setColor(c0);
- labelWelcome1->setColor(c1);
- labelWelcome2->setColor(c2);
- labelWelcome3->setColor(c3);
这段代码在将文字添加到布景之前设置。好了,现在的界面是下面的:
最后要添加中间的文字了,中间的文字是中文,Windows默认的字符集是GB2312,而Cocos2d-x使用的是UTF-8,所以要将我们的字符先转为UTF-8,转换方法是使用下面的函数:
- 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;
- }
现在可以将中间的文字显示出来了:
- CCLabelTTF *labelWelcome4=CCLabelTTF::create(G2U("游戏开发的世界,我用Cocos2d-x来降服你了~!"),"Verdana",35);
最后,最后就是我们前面的效果了。
最后init方法的全部代码:
- 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);
- */
- 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);
- CCLabelTTF *labelWelcome4=CCLabelTTF::create(G2U("游戏开发的世界,我用Cocos2d-x来降服你了~!"),"Verdana",35);
- CCSize size=CCDirector::sharedDirector()->getWinSize();
- float width=size.width;
- float height=size.height;
- CCSize s0=labelWelcome0->getContentSize();
- CCSize s1=labelWelcome1->getContentSize();
- CCSize s2=labelWelcome2->getContentSize();
- CCSize s3=labelWelcome3->getContentSize();
- 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);
- 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;
- c4.r=rand()%256;
- c4.g=rand()%256;
- c4.b=rand()%256;
- labelWelcome0->setColor(c0);
- labelWelcome1->setColor(c1);
- labelWelcome2->setColor(c2);
- labelWelcome3->setColor(c3);
- labelWelcome4->setPosition(ccp(size.width/2,size.height/2));
- labelWelcome4->setColor(c4);
- addChild(labelWelcome0,1);
- addChild(labelWelcome1,1);
- addChild(labelWelcome2,1);
- addChild(labelWelcome3,1);
- addChild(labelWelcome4,1);
- bRet = true;
- } while (0);
- return bRet;
- }