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,--thisvertices) { thispolygonPt->x = tagCenter.x + thisvertices->x; thispolygonPt->y = tagCenter.y + thisvertices->y; // log("%f,thispolygonPt->y); ++aa; } //右下角 tagCenter.x = maxX - radius; tagCenter.y = minY + radius; thisvertices = vertices; for (unsigned int i = 0; i <= segments; ++i,++thisvertices) { thispolygonPt->x = tagCenter.x + thisvertices->x; thispolygonPt->y = tagCenter.y - thisvertices->y; // log("%f,thispolygonPt->y); ++aa; } //左下角 tagCenter.x = minX + radius; tagCenter.y = minY + radius; thisvertices = vertices + segments; for (unsigned int i = 0; i <= segments; ++i,--thisvertices) { thispolygonPt->x = tagCenter.x - thisvertices->x; thispolygonPt->y = tagCenter.y - thisvertices->y; // log("%f,thispolygonPt->y); ++aa; } //设置参数 static Color4F red(1,1);//顶点颜色设置为红色,参数是R,G,B,透明度 //注意不要将pStencil addChild DrawNode *pStencil = DrawNode::create(); pStencil->drawpolygon(ppolygonPtArr,dwpolygonPtMax,red,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; }

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...