使用 SDL2 和 black/white 掩码的动画 transition/wipe?

Animated transition/wipe using SDL2 and black/white mask?

我一直在为如何制作这个简单的效果而焦头烂额。我有一张图片(见下文),当在游戏中使用此图片时,它会产生顺时针向黑色效果的过渡。我一直试图在 SDL(2) 中重新创建这种效果,但无济于事。我知道这与屏蔽有关,但我不知道如何在代码中做到这一点。

我能得到的最接近的方法是使用 "SDL_SetColorKey" 并增加 RGB 值,这样它 不会 绘制动画的 "wiping" 部分。

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 的;我只需要知道如何考虑这个!

随便想出了一个办法。它很昂贵,但有效。通过遍历图像中的每个像素并像这样映射颜色:

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