I want to load a png into a texture and then draw another text texture on top of it. I'm doing something like this:
SDL_Surface *bgImg = IMG_Load(PNG_PATH);
SDL_Texture *bg = SDL_CreateTextureFromSurface(renderer, bgImg);
SDL_Surface *textSurface = TTF_RenderText_Blended(font, text, red);
SDL_Texture *txt = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_SetTextureBlendMode(bg, SDL_BLENDMODE_NONE);
SDL_SetRenderTarget(renderer, bg);
SDL_RenderCopy(renderer, txt, NULL, &textDstRect);
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderPresent(renderer);
I'm basing the code on this answer. The comment says that won't work in case the texture is loaded from a surface, and indeed it doesn't. The text appears behind the background image, instead of on top of it. I know the text is rendered, because it is visible if I set the blend mode to SDL_BLENDMODE_ADD
, but that's not what I want.
I thought of trying to change the access of the bg texture to SDL_TEXTUREACCESS_TARGET
, in case that's the problem, but I can't figure out a way to do that for textures created from surfaces.
Any other way to render text on a texture loaded from a PNG file? Performance isn't too important because this will happen only once on application startup.