Skip to content

Instantly share code, notes, and snippets.

@dabing1022
Created April 22, 2014 09:28
Show Gist options
  • Save dabing1022/11171766 to your computer and use it in GitHub Desktop.
Save dabing1022/11171766 to your computer and use it in GitHub Desktop.
将图片更改为纯色
CCImage* img = new CCImage();
img->initWithImageFile("A4.png");
unsigned char* data = new unsigned char[img->getDataLen()*4];
CCLOG("data len %d", img->getDataLen());
data = img->getData();
for(int i = 0; i < img->getWidth(); i++)
{
for(int j = 0; j < img->getHeight(); j++)
{
unsigned char* pixel = data + (i + j * img->getWidth()) * 4;
// unsigned char r = *pixel;
// unsigned char g = *(pixel + 1);
// unsigned char b = *(pixel + 2);
if (*(pixel + 3) != 0) {
*pixel = 255;
*(pixel + 1) = 0;
*(pixel + 2) = 0;
}
}
}
CCTexture2D * texture = new CCTexture2D();
texture->initWithImage(img);
CCSprite* spr = CCSprite::createWithTexture(texture);
this->addChild(spr, 100);
spr->setPosition(ccp(240, 160));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment