Render to texture and alpha

I’m rendering text to a texture, using SDL_SetRenderTarget.
It works, and the alpha mask is taken into account, with the following code:

Code:
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);

However, it seems that when rendering the texture, the alpha is multiplied by itself.
To be more specific, the text is created as follow:

Code:
SDL_Surface* surface = TTF_RenderText_Blended(font, “Hello”, color);
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_SetRenderTarget(renderer, texture);
SDL_RenderCopy(renderer, text_texture, src_rect, dst_rect);
SDL_SetRenderTarget(renderer, nullptr);

Finally texture is rendered.
The result is that the dithering is too strong.

Is there a way to achieve a similar result (basically caching all my rendered text) without this drawback?

Ownership of the cached texture should be the responsibility of the UI element responsible for generating it; not the responsibility of some giant combination texture.------------------------
Nate Fries

2013/4/20 li <elie.huvier at gmail.com>

**
I’m rendering text to a texture, using SDL_SetRenderTarget.
It works, and the alpha mask is taken into account, with the following
code:

Code:

SDL_Texture* texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);

However, it seems that when rendering the texture, the alpha is multiplied
by itself.
To be more specific, the text is created as follow:

Code:

SDL_Surface* surface = TTF_RenderText_Blended(font, “Hello”, color);
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer,
surface);
SDL_SetRenderTarget(renderer, texture);
SDL_RenderCopy(renderer, text_texture, src_rect, dst_rect);
SDL_SetRenderTarget(renderer, nullptr);

Finally texture is rendered.
The result is that the dithering is too strong.

Is there a way to achieve a similar result (basically caching all my
rendered text) without this drawback?

Try setting SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE); on your
text texture (preferred) or in your render target texture. That should
probably fix the problem.–
Gabriel.

Nathaniel J Fries wrote:

Ownership of the cached texture should be the responsibility of the UI element responsible for generating it; not the responsibility of some giant combination texture.

You’re absolutely right, I’m trying to take a shortcut that doesn’t make really sense. In my defense, it’s merely experimentation…

gabomdq wrote:

Try setting SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE); on your text texture (preferred) or in your render target texture. That should probably fix the problem.

It works perfectly well when using SDL_BLENDMODE_NONE on the text texture. Thanks!