gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:58:46 26 4
gpt4 key购买 nike

我正在尝试使用颜色键控在 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

最佳答案

如果您使用的是 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

关于色键透明度不适用于 SDL_image 和 PNG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com