这个函数用来实现灰化一个精灵帧,接口很简单
CCSprite* getGraySprite(CCSprite* normalsprite) {
if ( !normalsprite) {
return CreateDefaultSprite();
}
//CCSprite转成CCimage
CCPoint p = normalsprite->getAnchorPoint();
normalsprite->setAnchorPoint(ccp(0, 0));
CCRenderTexture *outTexture = CCRenderTexture::create((int)normalsprite->getContentSize().width, (int)normalsprite->getContentSize().height);
outTexture->begin();
normalsprite->visit();
outTexture->end();
normalsprite->setAnchorPoint(p);
CCImage* finalImage = outTexture->newCCImage();
unsigned char *pData = finalImage->getData();
int iIndex = 0;
for (int i = 0; i < finalImage->getHeight(); i ++){
for (int j = 0; j < finalImage->getWidth(); j ++){
// gray
int iBPos = iIndex;
unsigned int iB = pData[iIndex];
iIndex ++;
unsigned int iG = pData[iIndex];
iIndex ++;
unsigned int iR = pData[iIndex];
iIndex ++;
iIndex ++;
unsigned int iGray = 0.3 * iR + 0.4 * iG + 0.2 * iB;
pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray;
}
}
CCTexture2D *texture = new CCTexture2D;
texture->initWithImage(finalImage);
CCSprite* newSprite = CCSprite::createWithTexture(texture);
delete finalImage;
texture->release();
//setRealContentSize(newSprite, _scaleMode);
return newSprite;
}
传进去一个精灵,就可以得到灰化处理之后的精灵了
注意:前后不是同一个精灵,作为参数用得精灵在用完后可以删除,当然也可以修改接口以方便使用;