I'm not able to get the alpha channel to work properly in SDL using a pixel map. Essentially it seems to get fairly transparent at low values, but as the alpha values approach 0, the pixels become black.
I've tried setting the blend mode for both the texture and the renderer, and searched around for other possible solutions. So far the documentation hasn't helped me figure out a solution. So here is a minimal example I made:
#include "SDL.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
unsigned int* PixelMap;
SDL_Window* Window;
SDL_Renderer* Renderer;
SDL_Texture* Texture;
int
SDL_main(int argc, char* args[])
{
PixelMap = (unsigned int*)malloc(SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(*PixelMap));
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN, &Window, &Renderer);
Texture = SDL_CreateTexture(Renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetTextureBlendMode(Texture, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawBlendMode(Renderer, SDL_BLENDMODE_BLEND);
while(1)
{
for(int I = 0; I < SCREEN_WIDTH * SCREEN_HEIGHT; ++I)
{
PixelMap[I] = 0xFFFCCFFF;
}
for(int I = SCREEN_WIDTH/2 - 100; I < SCREEN_WIDTH/2 + 100; ++I)
{
PixelMap[I + (SCREEN_WIDTH * SCREEN_HEIGHT/2)] = 0x00000000;
}
SDL_UpdateTexture(Texture, 0, PixelMap, SCREEN_WIDTH * sizeof(*PixelMap));
SDL_RenderClear(Renderer);
SDL_RenderCopy(Renderer, Texture, 0, 0);
SDL_RenderPresent(Renderer);
}
}
This isn't the exact code I'm using, but it seems to produce either the same or a similar issue. Here I would expect the line drawn to be transparent since I'm assigning 0 to the location, the blend mode has been set, and the pixel format is set to RGBA8888. But I'm seeing a black line instead.
What am I missing?
Thanks for reading!
EDIT: Oh, duh! I'm overwriting the data, so it has nothing to blend with-- I guess the default background color is black, so it's blending with that. So the solution would be to render the background first, then add the changed pixels on top of that.
I'll leave this up in case it helps anyone else in the future.
User contributions licensed under CC BY-SA 3.0