原理:opengl alpha混合
首先,需要一张底图,一张蒙版图。底图直接加载到当前layer。
其次,要搞个数组,记录一下触摸的轨迹,比较简单,就不说了。
蒙版图要跟底图分开并且在上层,触摸时擦掉蒙版,显示出底图。这里需要用到CCRenderTexture,。。。还是直接代码吧
void MoveOrbit::init(int width,int height,const CCPoint& aPoint,const char* pFileName)
{
index = 0;
m_checkNum = 0;
m_renderW = width;
m_renderH = height;
CCSize s = CCDirector::sharedDirector()->getWinSize();
this->setIsTouchEnabled(true);
for(int i= 0; i < pt_count; ++ i)
{
path[i] = ccp(0,0);
}
m_target = CCRenderTexture::renderTextureWithWidthAndHeight(m_renderW,m_renderH);
if (NULL == m_target)
{
return;
}
m_target->setPosition(aPoint);
m_points[0] = ccp(m_renderW / 2,m_renderH / 2);
m_points[1] = ccp(m_renderW / 4,m_renderH / 4);
m_points[2] = ccp(m_renderW / 4,m_renderH * 3 / 4);
m_points[3] = ccp(m_renderW * 3 / 4,m_renderH / 4);
m_points[4] = ccp(m_renderW * 3 / 4,m_renderH * 3 / 4);
m_sprite = CCSprite::spriteWithFile(pFileName);
m_sprite->setPosition(ccp(0,0));
m_sprite->retain();
addChild(m_target,1);
}
void MoveOrbit::draw()
{
m_target->begin();
m_sprite->draw();
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//===========================
glColor4f(0,0);
glBlendFunc(GL_SRC_ALPHA,GL_ZERO);
glPointSize((GLfloat)KDis);
ccDrawPoints(path,index);
//==============================
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBlendFunc( CC_BLEND_SRC,CC_BLEND_DST);
glColor4f(0,1);
m_target->end(false);
}
MoveOrbit* orbit = MoveOrbit::creat(300,200,ccp(size.width / 2,size.height / 4),"HelloWorld.png");
orbit->setObserver(this);
addChild(orbit,100);
CCSprite* sprite = CCSprite::spriteWithFile("test/dgg1.png");
sprite->setPosition(ccp(size.width / 2,size.height / 2));
addChild(sprite);
下面的代码是使用CCRenderTexture现将 要擦的东西 画在上面怎么画 就不说了然后用一个 透明矩形实现擦除 效果
最笨的 方法就是 像我这样遍历 像素的值
CGSize s = [rt.sprite.texture contentSizeInPixels];
int tx = s.width;
int ty = s.height;
int bitsPerPixel = 4 * 8;
int bytesPerPixel = bitsPerPixel / 8;
int bytesPerRow = bytesPerPixel * tx;
NSInteger myDataLength = bytesPerRow * ty;
GLubyte *buffer = calloc(myDataLength,1);
[rt begin];
glReadPixels(0,tx,ty,GL_RGBA,GL_UNSIGNED_BYTE,buffer);
[rt end];
for (int i=3; i<myDataLength; i+=8) {//RGBA 每次间隔4位(因为 每地4位个上面 是A的值 也可以跳着读取 快一点)
if(buffer[i]!=0)
{
f=NO;
free(buffer);
return;
}
free(buffer);