C++ 在blitting时保留alpha

C++ 在blitting时保留alpha,c++,sdl,sdl-1.2,C++,Sdl,Sdl 1.2,我如何在blitting时保留alpha值?我想在一个表面上blit多个具有不同alpha值的黑色正方形,然后在屏幕上blit该表面。我尝试的所有东西都只是一个不透明的黑色固体表面 编辑: 我想要的是: 我得到的是: 代码 #include <SDL.h> void putPixel(SDL_Surface *surface, int x, int y, Uint32 pixel){ Uint32 *pixels = (Uint32*)surface->pixels;

我如何在blitting时保留alpha值?我想在一个表面上blit多个具有不同alpha值的黑色正方形,然后在屏幕上blit该表面。我尝试的所有东西都只是一个不透明的黑色固体表面

编辑:

我想要的是:

我得到的是:

代码

#include <SDL.h>

void putPixel(SDL_Surface *surface, int x, int y, Uint32 pixel){

  Uint32 *pixels = (Uint32*)surface->pixels;
  pixels[(y * surface->w) + x] = pixel;
}

void FillAlpha(SDL_Surface* Surface){
  Uint32 Pixel;
  Uint8 RGBA[4] = {0, 0, 0, 100};
  Pixel = RGBA[3]<<24 | RGBA[2]<<16 | RGBA[1]<<8 | RGBA[0];
  for(int x=0; x<32; x++){
    for(int y=0; y<32; y++){
      putPixel(Surface, x, y, Pixel);
    }
  }
  RGBA[3] = 150;
  Pixel = RGBA[3]<<24 | RGBA[2]<<16 | RGBA[1]<<8 | RGBA[0];
  for(int x=32; x<64; x++){
    for(int y=0; y<32; y++){
      putPixel(Surface, x, y, Pixel);
    }
  }
}

int main(int argc, char* args[])
{
  SDL_Window* Window = NULL;
  SDL_Surface* Screen = NULL;
  SDL_Surface* Alpha = NULL;

  SDL_Event Event;

  bool Quit = false;

  Window = SDL_CreateWindow("Alpha test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, SDL_WINDOW_SHOWN);
  if(Window == NULL) return 1;
  Screen = SDL_GetWindowSurface(Window);
  if(Screen == NULL) return 2;

  Alpha = SDL_CreateRGBSurface(NULL, 64, 32, 32,
                               Screen->format->Rmask, Screen->format->Gmask,
                               Screen->format->Bmask, Screen->format->Amask);
  if(Alpha == NULL) return 3;
  SDL_SetSurfaceBlendMode(Alpha, SDL_BLENDMODE_BLEND);
  FillAlpha(Alpha);

  while(!Quit){
    while(SDL_PollEvent(&Event)){
      if(Event.type == SDL_KEYDOWN){
        if(Event.key.keysym.sym == SDLK_ESCAPE){
          Quit = true;
        }
      }else if(Event.type == SDL_QUIT){
        Quit = true;
      }
    }
    SDL_FillRect(Screen, NULL, SDL_MapRGB(Screen->format, 255, 255, 255));
    SDL_BlitSurface(Alpha, NULL, Screen, NULL);
    SDL_UpdateWindowSurface(Window);
  }

  SDL_Quit();
  return 0;
}

你能发布相关代码吗,包括创建曲面的部分和复制?我希望您使用的曲面具有不同的像素格式,但我们必须查看代码才能知道。我找到了一种方法来确保alpha正确添加到曲面,但不会在任何blendmode上正确混合到屏幕上。以下是有关alpha混合的教程:。现在,我知道这不完全是你正在做的,但它可能是一个开始:让Lazy Foo的版本运行,然后更改为你自己的曲面类型。setSurfaceAlphaMod是问题所在,我必须使用它来进行混合,然后它覆盖所有曲面的alpha。