I know how to create textures in SDL, but I don't get why they need to be created with a renderer: SDL_CreateTexture(SDL_Renderer* renderer, ...)

When it comes to rendering, I can render my texture on every renderer, regardless from which renderer it was created on. Then why does SDL_CreateTexture require me to pass in a renderer?

  • 3
    \$\begingroup\$ I'm not sure but I would guess that it is because it is the renderers that hold the OpenGL context (and they all share the same context). \$\endgroup\$ – Vaillancourt Jan 27 '16 at 13:31
  • \$\begingroup\$ As a future reference, I would suggest downloading SDL's source code. Then you could check these things out in the source yourself. \$\endgroup\$ – Tyyppi_77 Jan 27 '16 at 16:17

SDL_Renderer is an abstraction over some concept in the underlying graphics API. In OpenGL that concept is the context and in D3D it's the device. In these underlying graphics APIs, creating textures is an operation you do from a context or device, and so the SDL abstractions over those APIs must carry forward that relationship (or weaken the API).

Why do the underlying graphics APIs work that way? Because the context or the device is sortof like a representation of the graphics card itself, and the graphics card will be where most of the important texture data actually lives, and thus creating textures has to be done via the interface to the graphics card.

| improve this answer | |
  • \$\begingroup\$ Well that makes sense, especially on systems with more than one graphics card... \$\endgroup\$ – Xriuk Jan 27 '16 at 16:54

This is the data I could gather from a quick glimpse at the SDL's source code.

Firstly, the renderer stores the driver data, and the maximum texture size depends on the driver/GPU, so firstly in SDL_CreateTexture the code makes sure that the wanted texture isn't too big.

Then the code splits into platform/rendering API specific code. As Alexandre Vaillancourt said in the comments, the renderer stores the OpenGL context data that is required for creating the texture.

I'm also pretty sure that you can also force SDL_Renderer to use a specific rendering API at least in some hacky way, and rendering textures that are created on a DirectX renderer sure won't work with an OpenGL-based renderer.

Lastly, as mentioned here, the renderer also manages its textures lifetime.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.