颜色键透明度不适用于SDL_image和PNG文件

时间:2012-03-01 18:35:29

标签: c transparency sdl

我正在尝试使用颜色键控来实现SDL的透明度。虽然它适用于BMP文件,但它不适用于PNG文件。

这是我的代码:

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *displaySurface = SDL_SetVideoMode(200, 100, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);

    SDL_Surface *tmpSurface = NULL;
    SDL_Surface *backgroundSurface = NULL;
    SDL_Surface *bmpSurface = NULL;
    SDL_Surface *pngSurface = NULL;

    tmpSurface = IMG_Load("background.png");
    backgroundSurface = SDL_DisplayFormat(tmpSurface);
    SDL_FreeSurface(tmpSurface);

    tmpSurface = SDL_LoadBMP("bmpImage.bmp");
    bmpSurface = SDL_DisplayFormat(tmpSurface);
    SDL_FreeSurface(tmpSurface);
    SDL_SetColorKey(bmpSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmpSurface->format, 255, 0, 255));

    tmpSurface = IMG_Load("pngImage.png");
    pngSurface = SDL_DisplayFormat(tmpSurface);
    SDL_FreeSurface(tmpSurface);
    SDL_SetColorKey(pngSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(pngSurface->format, 255, 0, 255));

    SDL_Rect dest;

    dest.x = 0;
    dest.y = 0;

    SDL_BlitSurface(backgroundSurface, NULL, displaySurface, &dest);

    SDL_BlitSurface(bmpSurface, NULL, displaySurface, &dest);

    dest.x = 50;
    SDL_BlitSurface(pngSurface, NULL, displaySurface, &dest);

    SDL_Flip(displaySurface);

    SDL_Event event;

    while (1) {
        while (SDL_PollEvent(&event)) {
            ;
        }

        SDL_Delay(1);
    }

    return 0;
}

我上传了我在这里使用的代码和图片:

http://tobias.braun-abstatt.de/files/forums/transparency_test.zip

1 个答案:

答案 0 :(得分:1)

如果您使用的是SDL_image版本1.2.10或1.2.11,请务必将其更新至版本1.2.12。

#include <SDL/SDL_image.h>

#if (SDL_IMAGE_MAJOR_VERSION != 1) || (SDL_IMAGE_MINOR_VERSION != 2) || (SDL_IMAGE_PATCHLEVEL < 12)
#error "Invalid SDL_image version"
#endif