繁体   English   中英

SDL_SetColorKey() 不会删除背景颜色

[英]SDL_SetColorKey() is not removing the background colour

我正在尝试设置图像的透明度。 这段代码在 C++ 中运行良好,但是当我在 C 中尝试相同的代码时,它不再起作用。 执行代码时,图像显示,但黑色背景仍然存在。 这是我正在使用的代码。 谁能帮我找出问题所在?

SDL_Texture* Utilities_loadImage( SDL_Renderer* r, const char* file )
{

    /* load an image into memory using SDL_image library function */
    SDL_Surface* surface = SDL_LoadBMP(file);
    if(!surface)
    {
        printf("error creating surface: %s\n", SDL_GetError());
        return NULL;
    }

    if(SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 0, 0, 0)) != 0)
    {
        printf("Unable to set colourkey: %s", SDL_GetError());
    }

    /* convert image into a texture for use by the grafx hardware */
    SDL_Texture* texture = SDL_CreateTextureFromSurface(r, surface);

    /* get rid of surface */
    SDL_FreeSurface(surface);
    if(!texture)
    {
        printf("error creating texture: %s\n", SDL_GetError());
        return NULL;
    }

    return texture;
}

好吧,也许在这个特定的行中:

SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 0, 0, 0))

您正在调用相同的变量“表面”,也许您的背景在其他变量中,例如:

SDL_SetColorKey(background, SDL_TRUE, SDL_MapRGB(surface->format, 0, 0, 0))

可变surface是您尝试移动或工作的主要图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM