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.