The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_RenderCopy alpha-blending strangeness
geoffp


Joined: 03 Oct 2016
Posts: 3
Here's a strange thing: if an ARGB pixel with value 0x01ffffff (white, but nearly transparent) is
rendered onto a pixel with value 0 (completely transparent) by SDL_RenderCopy with default settings,
the rather surprising result is 0xff000000 (opaque black).

How can two nearly transparent pixels combine to make an opaque one?
Have I discovered a bug, or just my own ineptitude?

The effect is different with SDL_BlitSurface, which seems to stick to the formula for alpha-blending
given in SDL_blendmode.h :
dstRGB = (srcRGB * srcA) + (dstRGB * (1-srcA))
dstA = srcA + (dstA * (1-srcA))


The code I'm using (with SDL 2.0.4) is

Code:

SDL_Init(SDL_INIT_VIDEO);

// A target surface:
SDL_Surface *target_surf = SDL_CreateRGBSurface(0, 12, 12, 32, 0xff0000, 0x00ff00, 0x0000ff, 0xff000000);
SDL_FillRect(target_surf, NULL, 0x00000000);      // completely transparent
cout << "target's initial pixel value: " << hex << *((Uint32*)(target_surf->pixels)) << endl;
   
// A surface to render onto the target:
SDL_Surface *src_surf = SDL_CreateRGBSurface(0, 12, 12, 32, 0xff0000, 0x00ff00, 0x0000ff, 0xff000000);
SDL_FillRect(src_surf, NULL, 0x01ffffff);         // white, but almost completely transparent
cout << "source pixel value: " << *((Uint32*)(src_surf->pixels)) << endl;

// Render to target_surf:
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(target_surf);
SDL_Texture *src_texture = SDL_CreateTextureFromSurface(renderer, src_surf);
SDL_RenderCopy(renderer, src_texture, NULL, NULL);
cout << "resulting pixel value: " << *((Uint32*)(target_surf->pixels)) << endl;