简体   繁体   中英

SDL_SetColorKey() is not removing the background colour

I'm trying to set the transparency of my image. This code works fine in C++, but when I try the same code in C it no longer works. On executing the code, the image displays, however the black background remains. This is the code I'm using. Can anyone help me identify the issue?

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

Well maybe in this specific line:

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

you are calling the same variable "surface" and maybe the you background it's in other variable for example:

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

and the variable surface it's the principal image that what you try to move or work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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