cocos2d-x如何反白高亮CCSprite

本文详细介绍了在Cocos2d-x游戏中使用glTexEnvi函数实现纯白色高亮精灵的方法,包括重写自定义精灵类、修改draw函数以动态切换高亮效果。通过简单的代码示例,展示了如何在不同场景下快速应用这一技巧,提高游戏视觉表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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)即可


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值