Problem creating/copying transparent surfaces (attempt 2)

Greetings all,

I am reposting a question that I sent out yesterday because for some reason
the code sample I had put in it, got clobbered when it was sent to the
email list. This time around, I have moved the code sample into pastebin,
so that it both doesn’t get clobbered and also so that there is some syntax
highlighting… I am hoping that somebody will be able to spot what I am
doing wrong but, in any case, I won’t be posting this question again
because I don’t want to spam the list. Thanks again everybody.

The original email::

I have been struggling with an issue (SDL2) for the past week and I figured
that it was about time that I asked the community for help.

So what I am trying to is create a text render using
TTF_RenderText_Blended (which appears to use alpha blending), and to
apply/copy this text at a position onto a larger transparent surface
“referred to as the intermediate surface in my code sample”. Finally, I
would like to copy the contents of this largely transparent “intermediate
surface” onto the actual main window surface.

In my first attempt to do this, I had been using colorkey transparency on
the intermediate surface and had some horrible magenta color set as the
transparent color, the issue with this however was that when I would apply
the rendered text from TTF_RenderText_Blended, it would slightly change
the colors of some of the transparent background pixels, thereby making
them visible.

From this experience and reading documentation online, I came to the
conclusion that I probably wanted to be using alpha transparency and to
just set the initial background pixel colors to an alpha value of 0. The
problem that I am having is that even though I’ve set the alpha values to 0
when initially clearing the renderer of the intermediate surface, these
background pixels and their color still appear to be copied over onto the
main window surface.

I have prepared a code sample to illustrate the issue I am having:

This code produces a single SDL_Window with two instances of a
TTF_textRender on it. The first (top) instance of text is created using
TTF_RenderText_Blended and then applied directly onto the renderer for
the main window. The second (lower) instance of text was created using
TTF_RenderText_Blended, applied to an intermediate (supposedly
transparent) surface, and then copied onto the renderer for the main window


The link above shows a screenshot of the resulting operation (notice how
the second text instance is within a light blue box). What I would like to
accomplish is to make the second instance of text appear just like the
first while still preserving the structure of having an intermediate
“mostly transparent” surface inbetween the rendered text and the main
window surface.

I must admit that I am quite new to asking for help from mailing lists but,
I am hoping that I have explained me question clearly and have not
unintentionally exercised poor etiquette.

Many thanks,

Hello there,

You need to set the color key on the surface right before creating the texture, i.e.:

SDL_Color ck = {};
ck.r = 100;
ck.g = 100;
ck.b = 150;
ck.a = 0;
unsigned int encoded_pixel_color =
SDL_MapRGB(imSurf->format, ck.r, ck.g, ck.b);

SDL_SetColorKey(imSurf, true, encoded_pixel_color);

Full source:


Jeffrey Carpenter <@Jeffrey_Carpenter>

-------------- next part --------------
A non-text attachment was scrubbed…
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1572 bytes
Desc: not available