Search code examples
texturesrenderrender-to-texturesdl-2

Creating a new texture from several textures SDL-2


I have several textures that will be staying stationary on my screen. Rather than rendering each one individually when I render, I would like to render them all as one texture. My code so far looks like:

SDL_Texture* target_texture = NULL;
SDL_SetRenderTarget(renderer, target_texture);
SDL_RenderCopy(renderer, texture1, NULL, &dst1);
SDL_RenderCopy(renderer, texture2, NULL, &dst2);
SDL_RenderCopy(renderer, texture3, NULL, &dst3);
.
.
.
SDL_SetRenderTarget(renderer, NULL);
SDL_RendererPresent(renderer);
// Here the screen displays as I want
SDL_Delay(2000);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, target_texture, NULL, NULL);
SDL_RendererPresent(renderer);
// Here the screen is black as though target_texture is still NULL

Solution

  • You have to create your target_texture, currently it's always NULL. Replace your first line for:

    target_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, target_width, target_height);