转自:http://blog.csdn.net/zhy_cheng/article/details/8268814
原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/8268814。
这篇博文主要展示在屏幕上渲染文字,先来看看最后要实现的界面:
这个界面就是在屏幕的四个角落有不同字体不同颜色的Welcome来渲染,在屏幕的中间是汉字,颜色也是随机的。
下面来看看代码是怎么样实现的。
在main.cpp中将屏幕的大小设置为
- eglView->setFrameSize(800, 480);//设置界面大小
- eglView->setFrameSize(800, 480);//设置界面大小
在AppDelegate.cpp的applicationDidFinishLaunching函数中
- pDirector->setDisplayStats(0);
- 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;
- }
- 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);