Cocos2d-x Development--using ccrendertexture to crop a rectangular picture into a circular display

Source: Internet
Author: User

Recently, I encountered this need in the process of doing the project: it is necessary to turn the Facebook friend's square avatar into a circular avatar display. Through the online research, found that the use of ccrendertexture is a good method, summed up after the formation of the following method.

In fact, this method can not only cut the picture into a circle, but can be cut into any shape you want. The key is only the shape of the mask image you are using.

Here's how:

ccsprite * univcoretest::maskedsprite (ccsprite *texturesprite) {ccsprite * maskSprite = CCSPRITE::CR
    Eate ("Circle_mask.png"); Ccrendertexture * rendertexture = Ccrendertexture::create (Masksprite->getcontentsize (). width, maskSprite->
    
    Getcontentsize (). height); Masksprite->setposition (CCP (Masksprite->getcontentsize () WIDTH/2, Masksprite->getcontentsize (). Height/
    2)); Texturesprite->setposition (CCP (Texturesprite->getcontentsize () WIDTH/2, Texturesprite->getcontentsize (

    ). Height/2));
    Masksprite->setblendfunc ((ccblendfunc) {gl_one, gl_zero});
    
    Texturesprite->setblendfunc ((ccblendfunc) {gl_dst_alpha, gl_zero});
    Rendertexture->begin ();
    Masksprite->visit ();
    Texturesprite->visit ();

    Rendertexture->end ();
    Ccsprite * retval = Ccsprite::createwithtexture (Rendertexture->getsprite ()->gettexture ());
    Retval->setflipy (TRUE);
return retval; }

The function parameter "Texturesprite" is a square picture to be cropped, and of course the reader needs to first create the ccsprite and then pass it on.

The "circle_mask.png" image that appears in the method is the key to the entire cropping process, which determines what shape your original image will be cropped to.

Here the author needs to cut the original picture into a circle, thus using a mask image of the following style.


The picture is a white circle, surrounded by transparent.
The ccsprite that is returned by the method is a cropped picture that is displayed in a circular fashion.


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.