原文地址: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" >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;}
复制去Google翻译 翻译结果 无符号 的char * 的pData = pimg- > 的getData ( ) ;
INT iIndex = 0 ;
的for(int i = 0 ; @H_419_69@我 < pimg- > 的getHeight ( ) ; 我 ++ )
{
对于 ( INT J = 0 ; Ĵ < pimg- > 的getWIDth ( ) ; J ++ )
{
INT iBPos = iIndex ;
unsigned int类型 IB = 的pData [ iIndex ]
iIndex ++;
unsigned int类型 iG的 = 的pData [ iIndex ]
iIndex ++;
unsigned int类型 IR = 的pData [ iIndex ]
iIndex ++;
#如果 ( CC_TARGET_PLATFORM == CC_PLATFORM_IOS )
iIndex ++;
#else指令
如果 ( pimg- > hasAlpha ()) iIndex ++;
#ENDIF
unsigned int类型 iGray = 0.3 * 红外 + 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- > 发行 ( ) ;
}
返回 pTexture ;
} 总结
以上是内存溢出为你收集整理的<转+备份>Cocos2d-x精灵加亮及灰度调整全部内容,希望文章能够帮你解决<转+备份>Cocos2d-x精灵加亮及灰度调整所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)