The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Reuse dynamically created figures
slash


Joined: 04 Jun 2014
Posts: 5
Hi,

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

Code:

  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

slash
mr_tawan


Joined: 13 Jan 2014
Posts: 161
I think you could use render target to create the texture.
mr_tawan


Joined: 13 Jan 2014
Posts: 161
I think you could use render target to create a texture, and resue it until you have to recreate a new one.
SDL_SetRenderTarget
slash


Joined: 04 Jun 2014
Posts: 5
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?

Cheers,

slash
mr_tawan


Joined: 13 Jan 2014
Posts: 161
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.
slash


Joined: 04 Jun 2014
Posts: 5
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.

Cheers,

slash
slash


Joined: 04 Jun 2014
Posts: 5
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

Wolfgang
mr_tawan


Joined: 13 Jan 2014
Posts: 161
Well if the object is not too complex, there're won't be much performance gain (or it may even have penalty instead).