c++ - SDL2 透明背景
问题描述
我正在尝试从较大的 .PNG 图像创建 .PNG 图像。基本上剪辑原始的矩形区域并将结果另存为另一个 .PNG 有点像纹理解包器,如果你愿意的话。
我的问题是原始图像中透明的部分在剪切图像中是彩色的。
最初我使用硬件加速,背景是带有斑点的白色,切换到软件渲染器只是将背景更改为黑色。
我想保持原始图像的透明度。
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
void save_texture(const char* file_name, SDL_Renderer* renderer, SDL_Texture* texture); //
save png to disk
SDL_Texture* clipTexture(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source); // get
a new texture that is clipped from the original
int main(int argc, const char * argv[])
{
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Surface* surface = IMG_Load("LevelItems.png");
SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect frame_rect = { 189, 243,115, 50 };
SDL_Texture* tex_clip = clipTexture( frame_rect, renderer, texture );
save_texture("test1.png", renderer, tex_clip);
SDL_FreeSurface(surface);
return 0;
}
// return a new texture that is a part of the original texture.
SDL_Texture* clipTexture(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source)
{
SDL_Texture* result = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetRenderTarget(renderer, result);
SDL_RenderCopy(renderer, source, &rect, NULL);
return result;
}
// save png to disk
void save_texture(const char* file_name, SDL_Renderer* renderer, SDL_Texture* texture)
{
int width, height;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
SDL_RenderReadPixels(renderer, NULL, surface->format->format, surface->pixels, surface-
>pitch);
IMG_SavePNG(surface, file_name);
SDL_FreeSurface(surface);
}
解决方案
有两个问题:
禁用混合。使用
SDL_SetTextureBlendMode(source, SDL_BLENDMODE_NONE);
beforeRenderCopy
,否则你会得到错误的颜色(与默认黑色混合 - 结果会比它应该的更暗)。混合与纹理相关,因此如果您想稍后使用此纹理,您可能应该立即重置混合模式。有关在不同混合模式中使用的公式,请参阅SDL_SetTextureBlendMode文档。您生成的表面没有 Alpha 通道,因此无处复制结果。检查SDL_CreateRGBSurface的文档:虽然它允许使用
0
颜色蒙版来推断默认值,但它明确不允许它用于 alpha 通道,因此0
代替amask
“我不想要 alpha 通道,只需要 RGB”的结果。您生成的格式是 RGB888 打包为 32 位。你想要 alpha,所以你应该使用正确的颜色掩码 - 从文档中获取一个,在你的情况下,你甚至不需要检查字节序,但它永远不会受到伤害。
把它们加起来:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
void save_texture(const char* file_name, SDL_Renderer* renderer, SDL_Texture* texture);
SDL_Texture* clipTexture(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source);
int main(int argc, const char * argv[])
{
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Surface* surface = IMG_Load("test.png");
SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect frame_rect = { 189, 243,115, 50 };
SDL_Texture* tex_clip = clipTexture( frame_rect, renderer, texture );
save_texture("test1.png", renderer, tex_clip);
SDL_FreeSurface(surface);
return 0;
}
// return a new texture that is a part of the original texture.
SDL_Texture* clipTexture(SDL_Rect rect, SDL_Renderer* renderer, SDL_Texture* source)
{
SDL_Texture* result = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
SDL_SetRenderTarget(renderer, result);
SDL_SetTextureBlendMode(source, SDL_BLENDMODE_NONE);
SDL_RenderCopy(renderer, source, &rect, NULL);
return result;
}
// save png to disk
void save_texture(const char* file_name, SDL_Renderer* renderer, SDL_Texture* texture)
{
int width, height;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask);
SDL_RenderReadPixels(renderer, NULL, surface->format->format, surface->pixels, surface->pitch);
IMG_SavePNG(surface, file_name);
SDL_FreeSurface(surface);
}
推荐阅读
- neo4j - Neo4j Coactor 网络以及网络中的 coactor 之间的电影数量
- c - 如何使用 C 中的 while 循环和数组递归地计算字符串中的元音?
- gradle - 在 gradle ssh 插件中参考 spring-boot 组装的 jar
- ios - retrievePeripheralsWithIdentifiers:此函数将检索哪些设备?
- php - Ajax POST 数据,如何拆分数据?
- javascript - 如果某个功能不起作用,请在一段时间后停止该功能
- reactjs - 我可以从 componentDidMount 调用 onChange 事件吗
- java - 使用 Android 通过 OAuth1 调用 Open Bank Project
- html - CSS 块仅在输入两次时才有效
- c# - 使用大值时浮点到整数转换的错误值?