簡體   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