【问题标题】:SDL Texture transparent backgroundSDL 纹理透明背景
【发布时间】:2016-02-22 12:48:14
【问题描述】:

这可能是一个相当简单的问题,但经过一个小时的搜索和尝试,我仍然没有设法解决它。 我有两个 png 文件。一个是背景图像,第二个是前景。前景有一个 Alpha 通道。我想在背景之上显示前景。

我正在使用以下方式加载前台:

SDL_Surface *clip = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0, 0, 0, 0xff);
SDL_Rect rect = { x, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
SDL_BlitSurface(map, &rect, clip, NULL);
*block = SDL_CreateTextureFromSurface(gRenderer, clip);

map 是一些 SDL_Surface。

我正在使用以下方式加载背景:

SDL_Surface* loadedSurface = IMG_Load(path);
//Create texture from surface pixels
SDL_Texture* newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
SDL_FreeSurface(loadedSurface);

然后我尝试连接它们:

SDL_RenderCopy(gRenderer, background, NULL, &cur);
SDL_RenderCopy(gRenderer, map, NULL, &cur);

但它会导致前景图像带有黑色背景。我做错了什么?

【问题讨论】:

  • 试试SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  • 没用。还有其他想法吗?
  • 我认为你不应该把渲染器和表面混为一谈。它们是不同的东西!也使用渲染器作为背景。

标签: c++ visual-c++ sdl sdl-2


【解决方案1】:

你应该添加这两行,

Uint32 colorkey = SDL_MapRGB(loadedSurface->format, 0, 0, 0);
SDL_SetColorKey(loadedSurface, SDL_TRUE, colorkey);

在代码中的这一行之前

SDL_Texture* newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);

【讨论】:

    猜你喜欢
    • 2011-04-03
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2021-08-30
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多