在blitting时保留alpha

时间:2014-09-17 18:34:03

标签: c++ sdl sdl-1.2

如何在blitting时保留alpha值?我想将具有各种alpha值的多个黑色方块blit到表面上,然后将该表面blit到屏幕上。我尝试的所有东西都会打开一个坚固的不透明黑色表面。

编辑:

我想要的是:

wanted

我得到的是:

getting this

代码

#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;
}

0 个答案:

没有答案