0
\$\begingroup\$

I'm trying to find the most efficient way of rendering a large number of SDL2 objects to the screen.

Each of my objects have their own sprite sheets which are stored as SDL_Texture*.

Currently my render process looks like this:

  1. Update objects.
  2. Clear screen.
  3. Call each objects render function, which calls SDL_RenderCopyEx(...) to render to the screen.
  4. SDL_RenderPresent()

I was thinking I could improve this in one of two ways: Method 1:

  1. Update objects.
  2. Clear screen.
  3. Call SDL_SetRenderTarget(...) to set render target to a new texture 'X' which had previously been created with a SDL_TEXTUREACCESS_TARGET flag.
  4. Call each objects render function, which calls SDL_RenderCopyEx(...) to render to texture 'X'.
  5. Call SDL_SetRenderTarget(...) to set render target to screen.
  6. Call SDL_RenderCopyEx once more to render texture 'X' to the screen.
  7. SDL_RenderPresent()

Method 2: Method 2 would require all sprite sheets to be stored as SDL_Surface*. It would also require a 'global' SDL_Surface 'Y' to memcpy to.

  1. Update objects.
  2. Clear screen.
  3. Call each objects render function, which calls a custom function to memcpy(...) each current obj surface information to surface 'Y'
  4. Use surface 'Y' to create a new texture 'Z'.
  5. Call SDL_RenderCopyEx only once to render texture 'Z' to the screen.
  6. SDL_RenderPresent()

Unsure if any of these methods would make a difference in speed. Was looking for some input on whether it would be a good idea to implement either, or implement something new entirely.

Also, I'm not really looking for any new libraries to use, currently trying to use pure SDL. But if it's impossible to have an efficient rendering system with SDL, I would consider switching.

\$\endgroup\$
  • 1
    \$\begingroup\$ What does your testing & profiling so far say about these options? \$\endgroup\$ – DMGregory Jul 22 at 2:54
  • \$\begingroup\$ If you have blending enabled, memcpy is obviously going to be faster than that but isn’t going to produce the same result. What makes you predict that a texture render target is faster than the frame buffer? \$\endgroup\$ – Alex Jul 22 at 3:10
  • \$\begingroup\$ Define "large" in "large number of objects" \$\endgroup\$ – Casey Jul 22 at 4:20
  • \$\begingroup\$ When using C++, it's hard to say what is "The most efficient way" of doing anything. Depending on hardware, each system could have different performance with different ways. However using SDL, for the most part the hard part of rendering is handled by it, so unless your projects runs slowly, there is no point worrying about it. \$\endgroup\$ – TomTsagk Jul 22 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.