cocos2dx图片精灵裁剪圆角矩形方法
2016-06-14 17:17
489 查看
在做项目中,需要对图片进行裁剪已统一所有图片的边角,在网上查找方法结合自己的项目,书写了一个专门做裁剪的函数,供大家参考;
本方法是对图片的边角进行像素点的裁剪,在使用中发现如果大料使用对图片的裁剪,对程序的性能还是有点影响的,会降低cocos2dx的帧率;
ClippingNode* ccDrawRoundRect(cocos2d::Sprite *bgSprite, cocos2d::Vec2 origin, cocos2d::Vec2 destination, float radius, unsigned int segments){
Sprite *thisbg
4000
Sprite = 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;
}
本方法是对图片的边角进行像素点的裁剪,在使用中发现如果大料使用对图片的裁剪,对程序的性能还是有点影响的,会降低cocos2dx的帧率;
ClippingNode* ccDrawRoundRect(cocos2d::Sprite *bgSprite, cocos2d::Vec2 origin, cocos2d::Vec2 destination, float radius, unsigned int segments){
Sprite *thisbg
4000
Sprite = 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;
}
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- upload上传单张图片
- 图片引发的溢出危机(图)
- C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 如何使用C#从word文档中提取图片
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- C#图片添加水印的实现代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- C#实现图片加相框的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚
- 用css实现图片垂直居中的使用技巧
- 一起动手编写Android图片加载框架
- Cocos2d-x中背景音乐和音效使用实例