Cocos2d-x game engine development notes (2) Rendering text on the screen

Source: Internet
Author: User
Tags addchild

Original article. For more information, see http://blog.csdn.net/zhy_cheng/article/details/8268814.

 

This blog post mainly shows how to render text on the screen. First, let's take a look at the final interface to be implemented:

 

 

 

 

 

This interface is rendered with welcome of different fonts and colors in the four corners of the screen. The Chinese characters are in the middle of the screen, and the colors are random.

 

 

Let's take a look at how the code is implemented.

 

 

In Main. cpp, set the screen size

Eglview-> setframesize (800,480); // you can specify the page size.

In the applicationdidfinishlaunching function of appdelegate. cpp

 pDirector->setDisplayStats(0);

Do Not Display FPS

 

In helloworldsecne. cpp, annotate the code in the init method as follows:

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;}

Now the project should run like this:

Now, write your own code.

 

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 );

This is the construction of "welcome". Use the create method.

The first parameter is const char *, indicating the text to be rendered

The second parameter is const char *, indicating the font of the text.

The third parameter is float, indicating the font size.

 

 

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

Use ccdirector to obtain the screen size.

 

CCSize s0=labelWelcome0->getContentSize();CCSize s1=labelWelcome1->getContentSize();CCSize s2=labelWelcome2->getContentSize();CCSize s3=labelWelcome3->getContentSize();

Obtain the size of the text to be rendered.

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);

Set the text display position here. The ccpointmake, CCP, and ccpoint parameters both specify the display location, specify the text center location, and ccpointzero is (0, 0) point.

 

Add the text to the set:

addChild(labelWelcome0,1);addChild(labelWelcome1,1);addChild(labelWelcome2,1);addChild(labelWelcome3,1);

 

Now, the interface is as follows:

 

Next, set a random color for the text.

 

                  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;

Here, the color is set randomly, and the random number in C ++ is used. Next

                  labelWelcome0->setColor(c0);labelWelcome1->setColor(c1);labelWelcome2->setColor(c2);labelWelcome3->setColor(c3);

This code is set before the text is added to the set. Now the interface is as follows:

 

Finally to add the middle of the text, the middle of the text is Chinese, the default Windows character set is gb2312, And the Cocos2d-x is the UTF-8, so we want to convert our character first to UTF-8, the conversion method is to use the following function:

 

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;  }

 

Now we can display the text in the middle:

Cclabelttf * labelwelcome4 = cclabelttf: Create (g2u ("The World of game development, I used a Cocos2d-x to bring you down ~! ")," Verdana ", 35 );

Finally, let's see the previous results.

The code for the init method is as follows:

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: CREA Te ("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: shareddire()-> 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); */MAID :: create ("welcome", "", 48); maid: Create ("welcome", "consolas", 48); c Clabelttf * encoding = cclabelttf: Create ("welcome", "bitstream Vera sans mono", 48); cclabelttf * labelwelcome4 = cclabelttf: Create (g2u ("The World of game development, I used Cocos2d-x to drop you down ~! ")," 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 ;}

 


 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.