使用SDL2和黑/白掩码动画过渡/擦除?

时间:2017-04-12 23:09:21

标签: c++ sdl sdl-2

我一直在研究如何做这个简单的效果。我有一个图像(见下文),当这个图像用于游戏时,它会产生顺时针过渡到黑色效果。我一直在尝试在SDL(2)中重现这种效果,但无济于事。我知道它与屏蔽有关,但我不知道如何在代码中做到这一点。

我能得到的最接近的是使用“SDL_SetColorKey”并递增RGB值,以便绘制动画的“擦除”部分。

Uint32 colorkey = SDL_MapRGBA(blitSurf->format, 
    0xFF - counter, 
    0xFF - counter, 
    0xFF - counter, 
    0
);
SDL_SetColorKey(blitSurf, SDL_TRUE, colorkey);

// Yes, I'm turning the surface into a texture every frame!
SDL_DestroyTexture(streamTexture);
streamTexture = SDL_CreateTextureFromSurface(RENDERER, blitSurf);

SDL_RenderCopy(RENDERER, streamTexture, NULL, NULL);

我已经搜遍了所有人,现在我只是急切地想要回答我自己的好奇心和理智!我想这个问题并不完全具体到SDL;我只需要知道怎么想这个!

1 个答案:

答案 0 :(得分:0)

任意想出一个解决方案。它很贵,但很有效。通过遍历图像中的每个像素并映射颜色,如下所示:

int tempAlpha = (int)alpha + (speed * 5) - (int)color;
int tempColor = (int)color - speed;
*pixel = SDL_MapRGBA(fmt,
                (Uint8)tempColor,
                (Uint8)tempColor,
                (Uint8)tempColor,
                (Uint8)tempAlpha
            );

其中alpha是像素的当前alpha,speed是动画的参数化速度,color是像素的当前颜色。 fmt是图像的SDL_PixelFormat。这是为了淡化为黑色,以下是从黑色淡入:

if ((255 - counter) > origColor)
                continue;
int tempAlpha = alpha - speed*5;
*pixel = SDL_MapRGBA(fmt,
                (Uint8)0,
                (Uint8)0,
                (Uint8)0,
                (Uint8)tempAlpha
            );

其中origColor是原始灰度图像中像素的颜色。

我制作了一个快速API来完成所有这些操作,因此请随时查看:https://github.com/Slynchy/SDL-AlphaMaskWipes

相关问题