blitting 时如何保留 alpha 值?我想将具有各种 alpha 值的多个黑色方块粘贴到一个表面上,然后将该表面粘贴到屏幕上。我尝试的一切都只是在一个坚实的不透明黑色表面上涂抹。
编辑:
我想要的是这样的:
我得到的是:
编码
#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;
}