Cocos-2dx精灵部分变灰

前端之家收集整理的这篇文章主要介绍了Cocos-2dx精灵部分变灰前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CCSprite* CCSpotLight::makeSpiteGray(CCSprite* changeSprite)
{
	CCRenderTexture *render = CCRenderTexture::create(changeSprite->getContentSize().width,changeSprite->getContentSize().height,kCCTexture2DPixelFormat_RGBA8888);
	render->beginWithClear(0.0f,0.0f,0.0f);
	changeSprite->visit();
	render->end();
	//CCDirector::getInstance()->getRenderer()->render();

	CCImage *finalImage = render->newCCImage();

	unsigned char *pData = finalImage->getData();

	int iIndex = 0;

	for (int i = 0; i < finalImage->getHeight(); i ++)
	{
		if (i >= 100 && i < 200)
			continue;

		for (int j = 0; j < finalImage->getWidth(); j ++)
		{
			if (j >= 50 && j < 100)
				continue;
			// 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.6 * iG + 0.1 * iB;

			pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray;
		}

	}


	CCTexture2D *texture = new CCTexture2D;

	texture->initWithImage(finalImage);

	auto pSprite = CCSprite::createWithTexture(texture);

	delete finalImage;

	texture->release();

	return pSprite;

}

猜你在找的Cocos2d-x相关文章