概述cocos2d-x中的CCSprite只能
渲染矩形,如果想随意渲染任意
形状的
贴图,我没找到什么现成的东西,自己扩展了一下CCSprite,如下: // // cPolySprite.h // cardmap // // Created by sunny on 12-12-4. // // #ifndef __cardmap__cPolySprite__ #define __cardmap__ cocos2d-x中的CCSprite只能渲染矩形,如果想随意渲染任意形状的贴图,我没找到什么现成的东西,自己扩展了一下CCSprite,如下:
//
// cpolySprite.h
// cardmap
// Created by sunny on 12-12-4.
//
#ifndef __cardmap__cpolySprite__
#define __cardmap__cpolySprite__
#include "cocos2d.h"
//多边形精灵
class cpolySprite :public cocos2d::CCSprite
{
public:
cpolySprite() : vertexs_(NulL),uvs_(NulL),indices_(NulL),verCnt_(0) {}
virtual ~cpolySprite();
static cpolySprite* create(const char *pfile,
constcocos2d::CCPoint *uvs,
intverCnt,
constint *indices);
//重载父类draw
voID draw();
@H_404_177@ private
: //初始化顶点信息
bool initWithUV(const cocos2d::CCPoint *uvs,
constint *indices,
intverCnt);
//计算中点
cocos2d::CCPoint getCenter();
voID translate(const cocos2d::CCPoint&);
voID drawpoly();
voID releasepoly();
@H_404_177@ private
: //多边形顶点
cocos2d::ccVertex2F *vertexs_;
//定点纹理坐标
cocos2d::ccVertex2F *uvs_;
//三角形索引
unsigned short *indices_;
//顶点颜色
unsigned char *colors_;
//顶点数目
int verCnt_;
};
#endif
//
//cpolySprite.cpp
//cardmap
//Created by sunny on 12-12-4.
#include "cpolySprite.h"
#include "cocos2d.h"
using namespacecocos2d;
cpolySprite*cpolySprite::create(constchar *pfile,
const cocos2d::CCPoint *uvs,0); margin:0px"> int verCnt,0); margin:0px"> const int *indices)
{
cpolySprite *pobSprite = new cpolySprite();
//创建精灵
if(pobSprite &&
pobSprite->initWithfile(pfile)&&
pobSprite->initWithUV(uvs,indices,verCnt)) {
pobSprite->autorelease();
return pobSprite;
}
CC_SAFE_DELETE(pobSprite);
return false;
}
cpolySprite::~cpolySprite()
releasepoly();
//初始化顶点信息
bool cpolySprite::initWithUV(const cocos2d::CCPoint *uvs,0); margin:0px"> const int *indices,0); margin:0px"> int verCnt)
//内存分配
vertexs_ = new ccVertex2F[verCnt];
uvs_ =new ccVertex2F[verCnt];
indices_ = new unsigned short[(verCnt-2)*3];
colors_ = new unsigned char[verCnt*4];
//失败处理
if(!vertexs_|| !uvs_ || !indices_ || !colors_) {
releasepoly();
returnfalse;
//贴图大小
CCSize rc =m_pobTexture->getContentSize();
for(inti = 0; i <verCnt; ++i) {
//根据纹理坐标以及纹理大小计算顶点坐标
vertexs_[i].x = uvs[i].x*rc.wIDth;
//cocos2dx纹理坐标以左上角为原点
vertexs_[i].y = (1.0-uvs[i].y)*rc.height;
uvs_[i].x= uvs[i].x;
uvs_[i].y= uvs[i].y;
for(inti = 0; i <(verCnt-2)*3; ++i)
indices_[i] = indices[i];
memset(colors_, 255,sizeof(unsigned char)*verCnt*4);
verCnt_ = verCnt;
translate(getCenter());
return true;
//计算中点
CCPointcpolySprite::getCenter()
if(!vertexs_) return ccp(0,0);
float minx =vertexs_[0].x,0); margin:0px"> maxx = vertexs_[0].x,0); margin:0px"> miny = vertexs_[0].y,0); margin:0px"> maxy = vertexs_[0].y;
//计算所有顶点坐标的中心点坐标
for(inti = 0; i <verCnt_; ++i) {
minx =minx>vertexs_[i].x?vertexs_[i].x:minx;
maxx =maxx
miny =miny>vertexs_[i].y?vertexs_[i].y:miny;
maxy =maxy
returnccp((minx+maxx)*0.5,(miny+maxy)*0.5);
voID cpolySprite::translate(const cocos2d::CCPoint&pos)
//设置锚点
setAnchorPoint(ccp(pos.x/rc.wIDth,pos.y/rc.height));
voID cpolySprite::drawpoly()
CC_NODE_DRAW_SETUP();
ccGLBlendFunc( m_sBlendFunc.src,m_sBlendFunc.dst );
if(m_pobTexture != NulL) {
ccGLBindTexture2D( m_pobTexture->getname() );
else {
ccGLBindTexture2D(0);
ccGLEnabLevertexAttribs(kCCVertexAttribFlag_PoscolorTex);
//顶点,纹理,颜色
glVertexAttribPointer(kCCVertexAttrib_position,2,GL_float,GL_FALSE,0,vertexs_);
glVertexAttribPointer(kCCVertexAttrib_TexCoords,uvs_);
glVertexAttribPointer(kCCVertexAttrib_color,4,GL_UNSIGNED_BYTE,GL_TRUE,colors_);
//根据索引draw三角形
glDrawElements(GL_TRIANGLES,(verCnt_-2)*3,GL_UNSIGNED_SHORT,indices_);
CC_INCREMENT_GL_DRAWS(1);
voID cpolySprite::releasepoly()
CC_SAFE_DELETE(vertexs_);
CC_SAFE_DELETE(uvs_);
CC_SAFE_DELETE(indices_);
CC_SAFE_DELETE(colors_);
voID cpolySprite::draw(voID)
drawpoly();
//示例
//CCPoint p[] ={ccp(0,1.0),ccp(0.3,0.3),ccp(0.4,0.4),0.2)};
//int index[] = {0,1,3,2,3};
//cpolySprite *csp =cpolySprite::create("HelloWorld.png",p,4,index);
有点笨拙,但是还能用,需要注意的地方就是坐标系,纹理坐标左上角为原点,屏幕坐标左下角是原点。
渲染了个四边形贴图:
原文地址:http://blog.sina.com.cn/s/blog_62b2318d0101du43.HTML
总结
以上是内存溢出为你收集整理的cocos2dx 渲染任意形状贴图全部内容,希望文章能够帮你解决cocos2dx 渲染任意形状贴图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)