• cocos2d-x图片变灰或者变亮


    //根据现有CCSprite,变亮和变灰
    CCSprite* FlyLeaf::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);
                    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.299 * iR + 0.587 * iG + 0.114 * 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;
    }
  • 相关阅读:
    将课程中的所有动手动脑的问题以及课后实验性的问题,整理成一篇文档,以博客形式发表在博客园
    将课程中的所有动手动脑的问题以及课后实验性的问题,整理成一篇文档
    课堂作业 异常与处理
    课堂作业05继承与多态
    课后作业04
    课堂作业03程序设计
    课堂作业03动手动脑问题
    课堂作业02程序设计作业
    课堂作业02动手动脑的问题
    课堂中所有的问题及其课后实验性的问题的文档
  • 原文地址:https://www.cnblogs.com/shiweihappy/p/4246477.html
Copyright © 2020-2023  润新知