Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make background of sprite transparent in SDL

Tags:

c++

c

sdl

How to change background color of sprites to transparent by changing alpha after loading it to a SDL_Surface. Are there any functions in SDL which use a floodfill kind of algorithm and change all pixel with a given color to transparent on the outside. I don't want it to happen inside the border of the sprite if the same color is used.

Sample Image:

Sprite Image

I would like to make the background blue here transparent before I blit it on the window surface using SDL_BlitSurface.

like image 671
CodeIngot Avatar asked Oct 17 '22 05:10

CodeIngot


1 Answers

Only a color key (SDL_SetColorKey()) or a full alpha channel are going to help you here.

Note, that you can provide an alpha channel in your source graphics if you use a format such as PNG. If you only sometimes need/want an alpha, then provide the alpha channel in your source graphics and use SDL_SetSurfaceBlendMode() with SDL_BLENDMODE_NONE to blend without the alpha and SDL_BLENDMODE_BLEND to blend with it.

Both SDL_Surface and SDL_Texture support SDL_BlendMode.

Even if SDL provided another method, such as the fill you mentioned. You wouldn't want to use that. It is more difficult, expensive, and unnecessary overhead. You should stick with best practices here.

You may want to look into SDL_Texture and SDL_Renderer and switch to using a "texture atlas" instead of individual surfaces/textures for each image to maximize performance.

like image 133
Brad Allred Avatar answered Oct 20 '22 16:10

Brad Allred