<转+备份>Cocos2d-x精灵加亮及灰度调整

前端之家收集整理的这篇文章主要介绍了<转+备份>Cocos2d-x精灵加亮及灰度调整前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址:http://i.kimiazhu.info/?p=280

转一篇,不过本位介绍的加亮和之前我想做的加亮闪缩效果还是有差别。


主要的思路是通过CCImage先载入图片,然后getData()获取图片数据,接着逐一获取各像素的RGB值(代码基于jpg,若是png则需要获取RGBA值),通过相应方法修改各像素点的值。之后将该CCImange加载到CCTexture2D中,再由CCSprite载入,然后即可自由操作CCSprite。


以上相当于是在生成CCSprite时载入已加亮或变灰了的图片,至于如何将已有的CCSprite加亮或变灰,尚等研究。


C++ Code:
//根据现有CCSprite,变亮和变灰
static CCSprite* graylightWithCCSprite(CCSprite* oldSprite,bool isLight)
{
    //CCSprite转成CCimage
   CCPoint p = oldSprite->getAnchorPoint();
    oldSprite->setAnchorPoint(ccp(0,0));
    CCRenderTexture *outTexture = CCRenderTexture::create((int)oldSprite->getContentSize().width,(int)oldSprite->getContentSize().height);
    outTexture->begin();
    oldSprite->visit();
    outTexture->end();
    oldSprite->setAnchorPoint(p);

    CCImage* finalImage = outTexture->newCCImage();
    unsigned char *pData = finalImage->getData();
    int iIndex = 0;

    if(isLight)
    {
        for (int i = 0; i < finalImage->getHeight(); i ++)
        {
            for (int j = 0; j < finalImage->getWidth(); j ++)
            {
                // highlight
                int iHightlightPlus = 50;
                int iBPos = iIndex;
                unsigned int iB = pData[iIndex];
                iIndex ++;
                unsigned int iG = pData[iIndex];
                iIndex ++;
                unsigned int iR = pData[iIndex];
                iIndex ++;
                //unsigned int o = pData[iIndex];
                iIndex ++;  //原来的示例缺少
                iB = (iB + iHightlightPlus > 255 ? 255 : iB + iHightlightPlus);
                iG = (iG + iHightlightPlus > 255 ? 255 : iG + iHightlightPlus);
                iR = (iR + iHightlightPlus > 255 ? 255 : iR + iHightlightPlus);
                //            iR = (iR < 0 ? 0 : iR);
                //            iG = (iG < 0 ? 0 : iG);
                //            iB = (iB < 0 ? 0 : iB);
                pData[iBPos] = (unsigned char)iB;
                pData[iBPos + 1] = (unsigned char)iG;
                pData[iBPos + 2] = (unsigned char)iR;
            }
        }
    }else{
        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 ++;
                //unsigned int o = pData[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();
    return newSprite;
}
<pre name="code" class="cpp">CCTexture2D* GetGaryTexture( string fileName )
{
	string garyFileName;
	for( int i = 0; i < fileName.size(); i++ )
	{
		if( fileName[ i ] == '.' )
		{
			garyFileName += "_Gary";
		}
		garyFileName += fileName[ i ];
	}
	CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->textureForKey( garyFileName.c_str() );
	if ( !pTexture )
	{
		CCImage* pImg = NULL;
		pImg = new CCImage();
		pImg->initWithImageFile( fileName.c_str() );
		unsigned char *pData = pImg->getData();
		int iIndex = 0;
		for (int i = 0; i < pImg->getHeight(); i ++)
		{
			for (int j = 0; j < pImg->getWidth(); j ++)
			{
				int iBPos = iIndex;
				unsigned int iB = pData[iIndex];
				iIndex ++;
				unsigned int iG = pData[iIndex];
				iIndex ++;
				unsigned int iR = pData[iIndex];
				iIndex ++;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
				iIndex ++;
#else
				if( pImg->hasAlpha() )iIndex++;
#endif
				unsigned int iGray = 0.3 * iR + 0.6 * iG + 0.1 * iB;
				pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray;
			}
		}
		pTexture = CCTextureCache::sharedTextureCache()->addUIImage(pImg,garyFileName.c_str() );//加入缓存中
		pImg->release();
	}
	return pTexture;
}


 
 原文链接:https://www.f2er.com/cocos2dx/344033.html

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