1. 程式人生 > >cocos2dx圖片精靈裁剪圓角矩形方法

cocos2dx圖片精靈裁剪圓角矩形方法

在做專案中,需要對圖片進行裁剪已統一所有圖片的邊角,在網上查詢方法結合自己的專案,書寫了一個專門做裁剪的函式,供大家參考;

本方法是對圖片的邊角進行畫素點的裁剪,在使用中發現如果大料使用對圖片的裁剪,對程式的效能還是有點影響的,會降低cocos2dx的幀率;
 ClippingNode* ccDrawRoundRect(cocos2d::Sprite *bgSprite, cocos2d::Vec2 origin, cocos2d::Vec2 destination, float radius, unsigned int segments){


Sprite *thisbgSprite = bgSprite;
ClippingNode* pClip = ClippingNode::create();


pClip->setInverted(false);//設定是否反向,將決定畫出來的圓是透明的還是黑色的
//    this->addChild(pClip);
pClip->setAnchorPoint(Point(0, 0));
pClip->setPosition(-7, -7);


//算出1/4圓


const float coef = 0.5f * (float)M_PI / segments;
Point * vertices = new Point[segments + 1];
Point * thisVertices = vertices;
for (unsigned int i = 0; i <= segments; ++i, ++thisVertices)
{
float rads = (segments - i)*coef;
thisVertices->x = (int)(radius * sinf(rads));
thisVertices->y = (int)(radius * cosf(rads));
}
//
Point tagCenter;
float minX = MIN(origin.x, destination.x);
float maxX = MAX(origin.x, destination.x);
float minY = MIN(origin.y, destination.y);
float maxY = MAX(origin.y, destination.y);


unsigned int dwPolygonPtMax = (segments + 1) * 4;
Point * pPolygonPtArr = new Point[dwPolygonPtMax];
Point * thisPolygonPt = pPolygonPtArr;
int aa = 0;
//左上角
tagCenter.x = minX + radius;
tagCenter.y = maxY - radius;
thisVertices = vertices;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, ++thisVertices)
{
thisPolygonPt->x = tagCenter.x - thisVertices->x;
thisPolygonPt->y = tagCenter.y + thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}
//右上角
tagCenter.x = maxX - radius;
tagCenter.y = maxY - radius;
thisVertices = vertices + segments;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, --thisVertices)
{
thisPolygonPt->x = tagCenter.x + thisVertices->x;
thisPolygonPt->y = tagCenter.y + thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}
//右下角
tagCenter.x = maxX - radius;
tagCenter.y = minY + radius;
thisVertices = vertices;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, ++thisVertices)
{
thisPolygonPt->x = tagCenter.x + thisVertices->x;
thisPolygonPt->y = tagCenter.y - thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}
//左下角
tagCenter.x = minX + radius;
tagCenter.y = minY + radius;
thisVertices = vertices + segments;
for (unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, --thisVertices)
{
thisPolygonPt->x = tagCenter.x - thisVertices->x;
thisPolygonPt->y = tagCenter.y - thisVertices->y;
// log("%f , %f", thisPolygonPt->x, thisPolygonPt->y);
++aa;
}


//設定引數
static Color4F red(1, 0, 0, 1);//頂點顏色設定為紅色,引數是R,G,B,透明度


//注意不要將pStencil addChild
DrawNode *pStencil = DrawNode::create();
pStencil->drawPolygon(pPolygonPtArr, dwPolygonPtMax, red, 0, red);//繪製這個多邊形


pStencil->setPosition(Point(0, 0));


pClip->setStencil(pStencil);




pClip->addChild(thisbgSprite, 1);
pClip->setContentSize(thisbgSprite->getContentSize());


CC_SAFE_DELETE_ARRAY(vertices);
CC_SAFE_DELETE_ARRAY(pPolygonPtArr);
          return pClip;
 }