简体   繁体   中英

Render texture on top of texture created from surface with SDL2

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.

我使用SDL_TEXTUREACCESS_TARGET创建了一个新纹理, SDL_TEXTUREACCESS_TARGET表面加载的纹理复制到其中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM