SDL_BlitSurface creating black or white textures only, TTF

Hello everyone,
I am trying to stitch together a couple of surfaces created with TTF into a bigger surface which I want to then render.
The TTF Surfaces work correctly and can be rendered with the text. However, when trying to blit them together I have no output at all. Either the Surface is completely black (When setting the Blend mode to SDL_BLENDMODE_NONE) or white when not changing the blendmode after ttf creates the surface.

SDL_Surface *WholeSurface = SDL_CreateRGBSurface(0,RendertargetBig.w,RendertargetBig.h,8,0,0,0,0);
SDL_Rect blitrect = {0,0,15,16};
SDL_Color writecolor = {30,0,0,255}; //create color for text
SDL_Surface *text_surface = TTF_RenderUTF8_Blended(Maincontext.font,"test", writecolor);//create text surface

blitrect.y = 0;
blitrect.x = 0;
blitrect.w = text_surface->w;
blitrect.h = text_surface->h;
//SDL_SetSurfaceBlendMode(text_surface,SDL_BLENDMODE_NONE);
int blit_success = SDL_BlitSurface(text_surface,NULL,WholeSurface, &blitrect);

SDL_Texture *mTexture = SDL_CreateTextureFromSurface(Maincontext.renderer,text_surface);

SDL_RenderSetViewport(Maincontext.renderer,NULL);
SDL_RenderCopy(Maincontext.renderer,mTexture,NULL,&RendertargetBig);

The “text_surface” is a 32 bit ARGB-Surface per ttf documentation. I am thinking it has something to do with me not understanding blitting and the pixel formats correctly but so far this eludes me.

The SDL wiki says:

If depth is 4 or 8 bits, an empty palette is allocated for the surface.

I don’t know what an “empty palette” is but it sounds like it might be a problem.

Have you tried creating a 32-bit surface instead?

1 Like

TTF_RenderUTF8_Blended() returns a surface in which only the alpha channel contains the character(s), all the pixels are the same color. Since your destination surface is only 8-bpp it has no alpha channel to blit into!

You probably want to create a 32-bpp surface as the destination, but if it must be 8-bpp for some reason define a suitable palette and use TTF_RenderUTF8_Shaded() instead.

1 Like

Thank you very much,

For anybody else looking for this: What fixed it for me in the end was creating the correct kind of destination surface with

SDL_Surface *WholeSurface = SDL_CreateRGBSurfaceWithFormat(0,RendertargetBig.w,RendertargetBig.h,32,SDL_PIXELFORMAT_RGBA32);

and setting

SDL_SetSurfaceBlendMode(text_surface,SDL_BLENDMODE_NONE);