cocos2d-x如何反白高亮CCSprite

在做游戏时遇到需要使用纯白色高亮精灵的场合,尝试了很多办法没有解决问题,目前有以下几种解法:

1. 最简单当然是切换成和精灵图片一样的另一张纯白图片。

2. 使用glTexEnvi。具体如下:

   2.1 重写一个类继承CCSprite

   2.2 改写draw 函数如下 :

void YourSprite::setHightlight(bool hightlight)
{
    if (m_hightlight != hightlight)
    {
        m_hightlight = hightlight;
        if (m_hightlight)
        {
            ccBlendFunc blendFunc = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
            setBlendFunc(blendFunc);
        }
        else
        {
            ccBlendFunc blendFunc = {CC_BLEND_SRC, CC_BLEND_DST};
            setBlendFunc(blendFunc);
        }
    }
}

    void     YourSprite::draw()

{

     CCNode::draw();


    CCAssert(! m_bUsesBatchNode, "");


    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Unneeded states: -
    bool newBlend = m_sBlendFunc.src != CC_BLEND_SRC || m_sBlendFunc.dst != CC_BLEND_DST;
    if (newBlend)
    {
        glBlendFunc(m_sBlendFunc.src, m_sBlendFunc.dst);
    }


#define kQuadSize sizeof(m_sQuad.bl)
    if (m_pobTexture)
    {
        glBindTexture(GL_TEXTURE_2D, m_pobTexture->getName());
    }
    else
    {
        glBindTexture(GL_TEXTURE_2D, 0);
    }


    if (m_hightlight)
    {
        glActiveTexture( GL_TEXTURE0 );


        //the magic
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
        glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD); //you can change me... GL_REPLACE, GL_SUBSTRACT produce nice effects
        glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS); //setColor
        glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE); //your texture
    }
    long offset = (long)&m_sQuad;


    // vertex
    int diff = offsetof(ccV3F_C4B_T2F, vertices);
    glVertexPointer(3, GL_FLOAT, kQuadSize, (void*)(offset + diff));


    // color
    diff = offsetof( ccV3F_C4B_T2F, colors);
    glColorPointer(4, GL_UNSIGNED_BYTE, kQuadSize, (void*)(offset + diff));


    // tex coords
    diff = offsetof( ccV3F_C4B_T2F, texCoords);
    glTexCoordPointer(2, GL_FLOAT, kQuadSize, (void*)(offset + diff));


    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


    if( newBlend )
    {
        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    }


#if CC_SPRITE_DEBUG_DRAW == 1
    // draw bounding box
    CCSize s = m_tContentSize;
    CCPoint vertices[4] = {
        ccp(0,0), ccp(s.width,0),
        ccp(s.width,s.height), ccp(0,s.height)
    };
    ccDrawPoly(vertices, 4, true);
#elif CC_SPRITE_DEBUG_DRAW == 2
    // draw texture box
    const CCSize& s = m_obRect.size;
    const CCPoint& offsetPix = getOffsetPositionInPixels();
    CCPoint vertices[4] = {
        ccp(offsetPix.x,offsetPix.y), ccp(offsetPix.x+s.width,offsetPix.y),
        ccp(offsetPix.x+s.width,offsetPix.y+s.height), ccp(offsetPix.x,offsetPix.y+s.height)
    };
    ccDrawPoly(vertices, 4, true);
#endif // CC_SPRITE_DEBUG_DRAW


    if (m_hightlight)
    {
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    }

}

    2.3  需要纯白高亮显示时,调用setHightlight(true)即可


你可能感兴趣的:(游戏,float,byte,Blend,DST,colors)