How to make texture transparent after SDL_RenderClear()

My problem is that I have an object which consists of several parts. Each part is a different texture.
What I want is to copy the parts to temporary texture and then render that texture to window.

But when I execute SDL_RenderClear for the temporary texture it is filled with opaque color.
And because of that when I render the texture to window the rectangle filled with opaque color with
parts on top is actually rendered.

Can I select params for SDL_SetRenderDrawColor so that the temporary texture would be
transparent after SDL_RenderClear?

P. S. I load parts from bmp file and set a specific color as a color key to make textures for the parts transparent where I need to
but I don’t know how to do that if the texture is created with SDL_CreateTexture

1 Like

Solution: Make an SDL_Surface*, do an SDL_SetColorKey, and then do an
SDL_CreateTextureFromSurfaceOn Sat, Nov 30, 2013 at 11:50 AM, riptor wrote:

My problem is that I have an object which consists of several parts.
Each part is a different texture.
What I want is to copy the parts to temporary texture and then render that
texture to window.

But when I execute SDL_RenderClear for the temporary texture it is filled
with opaque color.
And because of that when I render the texture to window the rectangle
filled with opaque color with
parts on top is actually rendered.

Can I select params for SDL_SetRenderDrawColor so that the temporary
texture would be
transparent after SDL_RenderClear?

P. S. I load parts from bmp file and set a specific color as a color key
to make textures for the parts transparent where I need to
but I don’t know how to do that if the texture is created with
SDL_CreateTexture


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

riptor wrote:

My problem is that I have an object which consists of several parts. Each part is a different texture.
What I want is to copy the parts to temporary texture and then render that texture to window.

But when I execute SDL_RenderClear for the temporary texture it is filled with opaque color.
And because of that when I render the texture to window the rectangle filled with opaque color with
parts on top is actually rendered.

Can I select params for SDL_SetRenderDrawColor so that the temporary texture would be
transparent after SDL_RenderClear?

P. S. I load parts from bmp file and set a specific color as a color key to make textures for the parts transparent where I need to
but I don’t know how to do that if the texture is created with SDL_CreateTexture

Try
Code:
SDL_SetRenderDrawColor(rend, 0, 0, 0, 0);
SDL_RenderClear(rend);

Setting the Alpha for the draw color to 0 before the RenderClear should give you a blank, transparent canvas (provided the pixel format for that texture supports an alpha channel).

Ivan Rubinson wrote:

Solution: Make an SDL_Surface*, do an SDL_SetColorKey, and then do an SDL_CreateTextureFromSurface

I plan refreshing the temporary texture every frame and that means I’ll have to createTextureFromSurface every frame. It will definitely degrade performance.

Setting the Alpha for the draw color to 0 before the RenderClear should give you a blank, transparent canvas (provided the pixel format for that texture supports an alpha channel).

I tried SDL_SetRenderDrawColor(rend, 0, 0, 0, 0); , but it simply fills the texture with black color. Texture pixel format is SDL_PIXELFORMAT_ARGB8888.[/quote]

I finally found the solution, it is easy but not intuitive and I think it is worth sharing.
Suppose we have SDL_Window win, SDL_Texture tex and SDL_Renderer ren

We want to make tex fully transparent
What we should do is

Code:

//SDL_RENDERER_TARGETTEXTURE is neccesary
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

//SDL_TEXTUREACCESS_TARGET, is neccesary
SDL_Texture *tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 500, 500);

//make render draw to tex
SDL_SetRenderTarget(ren,tex);

//will make pixels with alpha 0 fully transparent
//use SDL_SetTextureBlendMode . Not SDL_SetRenderDrawBlendMode
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);

//tell render to use colore with alpha 0
SDL_SetRenderDrawColor(ren, 0,0,0,0);

//fill texture with transparent pixels
SDL_RenderClear(ren);

//make render draw to window
SDL_SetRenderTarget(ren,NULL);

I tried using SDL_SetRenderDrawBlendMode but it didn’t work. SDL_SetTextureBlendMode is the way to go :slight_smile:

1 Like

riptor wrote:

I finally found the solution, it is easy but not intuitive and I think it is worth sharing.
Suppose we have SDL_Window win, SDL_Texture tex and SDL_Renderer ren

We want to make tex fully transparent
What we should do is

Code:

//SDL_RENDERER_TARGETTEXTURE is neccesary
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

//SDL_TEXTUREACCESS_TARGET, is neccesary
SDL_Texture *tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 500, 500);

//make render draw to tex
SDL_SetRenderTarget(ren,tex);

//will make pixels with alpha 0 fully transparent
//use SDL_SetTextureBlendMode . Not SDL_SetRenderDrawBlendMode
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);

//tell render to use colore with alpha 0
SDL_SetRenderDrawColor(ren, 0,0,0,0);

//fill texture with transparent pixels
SDL_RenderClear(ren);

//make render draw to window
SDL_SetRenderTarget(ren,NULL);

I tried using SDL_SetRenderDrawBlendMode but it didn’t work. SDL_SetTextureBlendMode is the way to go :slight_smile:

I’m wondering whether that one should qualify as a bug. My personal opinion is that SDL_RenderClear() should arbitrarily set all pixels of the target to the specified value, regardless of what blend mode has been selected for that target.

But after taking a quick look at the code, it doesn’t appear SDL is doing anything involving blend mode in the relevant RenderClear functions. So this may be the result of some basic assumption in OpenGL/Direct3D/etc that I simply don’t understand.

1 Like

I’m wondering whether that one should qualify as a bug. My personal opinion is that SDL_RenderClear() should arbitrarily set all pixels of the target to the specified value, regardless of what blend mode has been selected for that target.

Actually it looks like I didn’t understand why my code worked. It doesn’t matter which blend mode was set before SDL_RenderClear. But blend mode before SDL_RenderCopy matters and should be set to SDL_BLENDMODE_BLEND.

So my code snippet shoul be rewritten like that.

Code:

//SDL_RENDERER_TARGETTEXTURE is neccesary
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

//SDL_TEXTUREACCESS_TARGET, is neccesary
SDL_Texture *tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 500, 500);

//will make pixels with alpha 0 fully transparent during renderCopy
//use SDL_SetTextureBlendMode . Not SDL_SetRenderDrawBlendMode
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);

//You should perform the following operations each time you want to fill the texture with transparent pixels

//make render draw to tex
SDL_SetRenderTarget(ren,tex);

//tell render to use colore with alpha 0
SDL_SetRenderDrawColor(ren, 0,0,0,0);

//fill texture with transparent pixels
SDL_RenderClear(ren);

//make render draw to window
SDL_SetRenderTarget(ren,NULL);

1 Like

I believe SDL_RenderClear() failing to clear to transparent WAS
identified as a bug prior to 2.0.1 and corrected?

JosephOn Sat, Nov 30, 2013 at 09:50:33AM +0000, riptor wrote:

My problem is that I have an object which consists of several parts. Each part is a different texture.
What I want is to copy the parts to temporary texture and then render that texture to window.

But when I execute SDL_RenderClear for the temporary texture it is filled with opaque color.
And because of that when I render the texture to window the rectangle filled with opaque color with
parts on top is actually rendered.

Can I select params for SDL_SetRenderDrawColor so that the temporary texture would be
transparent after SDL_RenderClear?

P. S. I load parts from bmp file and set a specific color as a color key to make textures for the parts transparent where I need to
but I don’t know how to do that if the texture is created with SDL_CreateTexture


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Well, sdl 2.0.1 behaves the way I described, but I don’t think it should be considered a bug.

Joseph Carter wrote:> I believe SDL_RenderClear() failing to clear to transparent WAS

identified as a bug prior to 2.0.1 and corrected?

Joseph

On Sat, Nov 30, 2013 at 09:50:33AM +0000, riptor wrote:

My problem is that I have an object which consists of several parts. Each part is a different texture.
What I want is to copy the parts to temporary texture and then render that texture to window.

But when I execute SDL_RenderClear for the temporary texture it is filled with opaque color.
And because of that when I render the texture to window the rectangle filled with opaque color with
parts on top is actually rendered.

Can I select params for SDL_SetRenderDrawColor so that the temporary texture would be
transparent after SDL_RenderClear?

P. S. I load parts from bmp file and set a specific color as a color key to make textures for the parts transparent where I need to
but I don’t know how to do that if the texture is created with SDL_CreateTexture


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org