使用掩码进行 SDL 位块传输

发布于 2024-11-08 21:54:43 字数 238 浏览 3 评论 0原文

我有一个 SDL_image/surface(原始图像),我想将其“blit”到另一个 SDL_image/surface 上,该 SDL_image/surface 是一个用于复制原始图像部分的掩码。

掩模使用 255 定义要保留的部分,使用 0 定义需要从图像中删除的区域。

我目前正在对蒙版和原始图像进行像素遍历,这在输出中引起了很多问题。

是否有预先存在的技术可以使用 SDL 的位块传送功能来执行此操作?

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 functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠我空喜 2024-11-15 21:54:43

听起来你的“面具”是一个阿尔法通道。创建具有 Alpha 支持的 SDL_image。

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

人疚 2024-11-15 21:54:43

添加 SDL2 答案,以防有人正在寻找解决方案。没有额外的库。

SDL2 API 中提供以下函数:

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

用于设置表面中的透明像素。在下面来自我的爱好黑客的示例片段中,它在加载图像时使用。

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;
}

参考:

https://wiki.libsdl.org/SDL_SetColorKey

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文