开发者

SDL blitting with a mask

I've an SDL_image/surface (the original) that I'd like to "blit" against another SDL_image/surface that is a mask to copy out portions from the original.

The mask uses 255 to define the portions to keep and 0 to define regions that need to be removed from the image.

I'm current doing pixel traversal of the mask and the original image and it's causing a lot of issues in the output.

Is there a pre-existing technique to do this using SDL's blitting functi开发者_如何转开发onality?


Sounds like your "mask" is an alpha channel. Create an SDL_image with alpha support.


Adding an SDL2 answer in case someone is looking for a solution. No additional libs.

The following function is available in the SDL2 API:

int SDL_SetColorKey(SDL_Surface* surface,
                    int          flag,
                    Uint32       key)

It is used to set the transparent pixel in a surface. In the below example snippet from my hobby hack it is used when the image is loaded.

void* MGWindow::loadBMPImage(std::string fileName, bool transparent) 
{
    SDL_Surface* loadedImage = NULL;
    SDL_Texture* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP(fileName.c_str());
    if(loadedImage != NULL)
    {
        if(transparent)
        {
            // TODO: Make it possible to have other color codes than
            // zero represent transparency
            SDL_SetColorKey(loadedImage, SDL_TRUE, 0);
        }
        optimizedImage = SDL_CreateTextureFromSurface(m_Renderer, loadedImage);
        SDL_FreeSurface(loadedImage);
    }
    return (void*)optimizedImage;
}

Reference:

https://wiki.libsdl.org/SDL_SetColorKey

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜