gpt4 book ai didi

image - SDL_image PNG 透明度错误

转载 作者:行者123 更新时间:2023-12-04 17:56:31 27 4
gpt4 key购买 nike

我正在将 PNG 文件(带有一些透明的地方)加载到我的 SDL 应用程序中。

谷歌搜索如何做到这一点为我提供了以下代码示例:

SDL_Surface *LoadImage(std::string filename)
{
SDL_Surface* loaded_image = 0, compatible_image = 0;

if (!filename.c_str())
return 0;

loaded_image = IMG_Load(filename.c_str());

if (!loaded_image)
return 0;

compatible_image = SDL_DisplayFormat(loaded_image);

SDL_FreeSurface(loaded_image);

return compatible_image;
}

但是当行 compatible_image = SDL_DisplayFormat(loaded_image);到达时,应用程序因无法捕获的异常而停止(即使 try { /* ... */ } catch (...) { /* ... */ } 也无济于事)。更换 SDL_DisplayFormat()SDL_DisplayFormatAlpha()也没有帮助。所以,我只是删除了异常可trowable 行并让这段代码工作来加载图像:
SDL_Surface *LoadImage(std::string filename)
{
if (!filename.c_str())
return 0;

return IMG_Load(filename.c_str());
}

我发现了这样一件令人不快的事情:当一些 Sprite 与另一个 Sprite 的透明部分重叠时,就会出现伪像。像这样的东西:

normal state

artifacts appearing

我正在用这个简单的算法为我的“英雄”制作动画:
// SDL_Surface sprite = LoadImage("hero.bmp");
// hero.bmp contains animation frames followed one-by-one in a single line
// spriteFrameCnt is a number of animation frames
// spriteWidth and spriteHeight contain single frame params

SDL_Rect srcRect;
srcRect.x = spriteFrame * spriteWidth;
srcRect.w = spriteWidth;
srcRect.y = 0;
srcRect.h = spriteHeight;
spriteFrame = ++spriteFrame % spriteFrameCnt;

SDL_BlitSurface(sprite, &srcRect, screen, &rcSprite);

如何解释和解决这个问题?

最佳答案

使用 SDL_DisplayFormatAlpha而不是 SDL_DisplayFormat .它适本地保留了透明度。

关于image - SDL_image PNG 透明度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8027026/

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