SDL_SetColorKey()未删除背景色

SDL_SetColorKey()未删除背景色,c,sdl,C,Sdl,我正在尝试设置图像的透明度。此代码在C++中运行良好,但是当我在C中尝试相同的代码时,它不再工作。执行代码时,会显示图像,但仍保留黑色背景。这是我正在使用的代码。有人能帮我确定这个问题吗 SDL_Texture* Utilities_loadImage( SDL_Renderer* r, const char* file ) { /* load an image into memory using SDL_image library function */ SDL_Surfac

我正在尝试设置图像的透明度。此代码在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))
您正在调用同一个变量“surface”,可能是您的背景,它位于其他变量中,例如:

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

<变量>代码>表面< /代码>:你试图移动或工作的主要图像。

什么意思,它在C++中工作良好,而不是在C中工作?工作情况和中断情况之间有什么特别的不同?我在这里没有看到任何特定于C的情况,因此不同的行为可能是由其他一些代码引起的(例如,lazyfoo教程因微妙地无法与C兼容而臭名昭著),或者完全是其他原因造成的。你能添加一个最小的完整的可编译的可验证的例子吗?@ OkoVKO,当我运行代码作为C++程序的一部分时,函数正确地设置透明度。但是,作为C函数,黑色背景仍然保留。我在这两种情况下使用的图像都是24位BMP。据我所知,这段代码是C89。如果它在C++中工作,它应该在C.工作。如果你需要更多帮助,你将不得不提供关于你所执行的精确编译的更多细节。