SDL2 sprite batching and texture atlases

Posted by jms on Game Development See other posts from Game Development or by jms
Published on 2014-02-24T03:26:10Z Indexed on 2014/08/23 22:36 UTC
Read the original article Hit count: 963

I have been programming a 2D game in C++, using the SDL2 graphics API for rendering. My game concept currently features effects that could result in even tens of thousands of sprites being drawn simultaneously to the screen. I'd like to know what can be done for increasing rendering efficiency if the need arises, preferably using the SDL2 API only. I have previously given a quick look at OpenGL-based 2D rendering, and noticed that SDL2 lacks a command like

int SDL_RenderCopyMulti(SDL_Renderer* renderer,   SDL_Texture* texture,
                        const SDL_Rect* srcrects, SDL_Rect* dstrects,   int count)

Which would permit SDL to benefit from two common techniques used for efficient 2D graphics:

  • Texture batching: Sorting sprites by the texture used, and then simultaneously rendering as many sprites that use the same texture as possible, changing only the source area on the texture and the destination area on the render target between sprites. This allows the encapsulation of the whole operation in a single GPU command, reducing the overhead drastically from multiple distinct calls.

  • Texture atlases: Instead of creating one texture for each frame of each animation of each sprite, combining multiple animations and even multiple sprites into a single large texture. This lessens the impact of changing the current texture when switching between sprites, as the correct texture is often ready to be used from the previous draw call. Furthemore the GPU is optimized for handling large textures, in contrast to the many tiny textures typically used for sprites.

My question: Would SDL2 still get somewhat faster from any rudimentary sprite sorting or from combining multiple images into one texture thanks to automatic video driver optimizations? If I will encounter performance issues related to 2D rendering in the future, will I be forced to switch to OpenGL for lower level control over the GPU?

Edit: Are there any plans to include such functionality in the near future?

© Game Development or respective owner

Related posts about textures

Related posts about sprites