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
Renderering text onto another textures with alpha blending
stewambo


Joined: 04 Jun 2016
Posts: 13
Hey guys,

I am trying to blend text that was created by SDL_TTF onto another texture using alpha blending. However that creates strange borders arround the text (pixels that are way too dark), which can be seen in the picture. It shows two textures, the first one is the "text texture" drawn directly and the second one is the "blenden texture", where the "text texture" has first been drawn onto another empty texture, which then is rendererd.


The code to create this picture looks like the following:
Code:

//creating the "text texture"
SDL_Color textCol = { 255, 0, 0, 255 };
SDL_Surface* textSurface = TTF_RenderText_Blended(resourceManager->getFont("arial", 20), "Test", textCol);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_SetTextureBlendMode(textTexture, SDL_BLENDMODE_BLEND);
SDL_FreeSurface(textSurface);
int textWidth, textHeight;
SDL_QueryTexture(textTexture, NULL, NULL, &textWidth, &textHeight);

//creating a new and empty texture and drawing the "text texture" ontop of it
SDL_Texture* blendedTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, textWidth, textHeight);
SDL_SetTextureBlendMode(blendedTexture, SDL_BLENDMODE_BLEND);
SDL_SetRenderTarget(renderer, blendedTexture);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_Rect renderRect = SDL_Rect{ 0, 0, textWidth, textHeight };
SDL_RenderCopy(renderer, textTexture, NULL, &renderRect);
SDL_SetRenderTarget(renderer, NULL);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

//finally rendering both textures to compare them
renderRect = SDL_Rect{ 100, 250, textWidth, textHeight };
SDL_RenderCopy(renderer, textTexture, NULL, &renderRect);
renderRect = SDL_Rect{ 100, 270, textWidth, textHeight };
SDL_RenderCopy(renderer, blendedTexture, NULL, &renderRect);


As you can see I set for almost everything (textures and renderer) the blend mode to blend, I also tried setting the alpha mod of the textures but nothing made both textures look the same. Is there something I'm missing or is there something wrong with what I'm doing here? Please help Smile

Thanks in advance!
Renderering text onto another textures with alpha blending
David Olofson
Guest

I believe the dark edges come from 'blendedTexture'. To generate an RGBA texture with "pure" edges, you need to make sure all pixels have the correct (text) color - even the invisible ones. So, that texture needs to be the same color as the text, or you have to prepare the texture in some other way. Otherwise, colors bleed into the edges through the antialiased edges of the text.​
stewambo


Joined: 04 Jun 2016
Posts: 13
Thanks for the hint. After experimenting a bit I found out that using SDL_RenderClear with the color of the text worked like a charm. So just insert the following after setting the render target to the texture:
Code:

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0); //should be the color of the text
SDL_RenderClear(renderer);