Reuse dynamically created figures


I’m just working on a game where bitmaps are dynamically created by drawing.
E.g. a missile is created like that:

  rect.x = missileX;
  rect.y = missileY;
  rect.w = missileWidth;
  rect.h = missileHeight;

  SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xef);
  SDL_RenderFillRect(renderer, &rect);

Since the missile always looks the same, I’d like to create the corresponding object once in a surface or the like, and instead of recreating it on demand, just copyRect it to the destination.

I did not find a possibility to draw such a figure to a figure in a way I can reuse it. Using a loaded bitmap file is not an alternative for me here, since the size of the missile is computed dynamically. Is there anyway to draw to an in-memory buffer/surface, and to copy this to the appropriate position on the screen?

Thanx in advance


I think you could use render target to create the texture.

I think you could use render target to create a texture, and resue it until you have to recreate a new one.

Thanx mr_tawan, So the suggestion is

1.) Create a new texture.
2.) Use SDL_SetRenderTarget to set the target to the new texture
3.) Draw my object as before
4.) Copy the texture to the window

Am I right?



Yes. That should work.

The point is you have to clear the texture and redraw a new object into that texture only when it needs to (eg. size changed). Otherwise it will be the same as draw it to the screen directly, even worse render target can cause performance drop.

That’s exactly my use case. The figures are normally created exactly once. They are only created dynamically to handle different screen sizes.

Thanx again, I’ll try this.



Hi mr_tawn,

thanx, I got it working. Still it got not expected results. I thought drawing a triangle every frame would be slower than to create it once in a texture and to copy it at the appropriate position all the time. Maybe I was misled here, a graphics processor may be faster in drawing directly to the window than copying the containing SDL_Rect to the window.

Anyway, your proposal did work, although not with the expected performance gain.

Thanx again


Well if the object is not too complex, there’re won’t be much performance gain (or it may even have penalty instead).