SDL: How to blit a surface onto one created with SDL_CreateRGBSurface correctly?
我正在尝试将
通过实验,我发现如果在源代码表面调用
最大的问题是:为什么首先需要
这是复制此代码的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <SDL/SDL.h> #include <SDL_image/SDL_image.h> SDL_Surface* copy_surface(SDL_Surface* source) { SDL_Surface *target; target = SDL_CreateRGBSurface(0, source->w, source->h, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, source->format->Amask); /* * I really don't understand why this is necessary. This is supposed to * clear the SDL_SRCALPHA flag presumably set by IMG_Load. But why wouldn't * blitting work otherwise? */ SDL_SetAlpha(source, 0, 0); SDL_BlitSurface(source, 0, target, 0); return target; } int main(int argc, char* argv[]) { SDL_Event event; SDL_Surface *copy, *screen, *source; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(800, 600, 0, 0); SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff)); source = IMG_Load("source.png"); if (!source) { fprintf(stderr,"Unable to load source image\ "); return 1; } copy = copy_surface(source); SDL_BlitSurface(copy, 0, screen, 0); SDL_Flip(screen); while (SDL_WaitEvent(&event)) if (event.type == SDL_QUIT || (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)) break; SDL_Quit(); return 0; } |
您将需要source.png。 我正在使用带有一些黑色斑点的400x400透明PNG。
http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlsetalpha.html描述了每种可能的像素格式组合的划线。
执行alpha混合时,标准公式为
如果启用了Alpha混合,并且对源表面启用了SRCALPHA标志,则使用源Alpha重新计算(混合)更新的目标颜色,但保持目标Alpha通道不变。下次将此" dst"用作发短信的来源时,将使用其alpha。
为避免您的问题,有几种可能的方法:
另请注意,我看到的(4),(5)和(6)只是允许您保留源alpha的方式。所有的blitting操作都将丢弃源alpha,而为目标保留其自己的alpha值-因为blitting从不更新目标alpha。