I'm currently working with SDL2 and keep coming up with practicality questions.
Let's say my application features a single window. I generate a renderer to render texture to this window. As of currently, I have a "master" renderer that is responsible for rendering everything to that window.
My question, would there ever be a reason to have multiple renderers for a single window? Right now, I can't think of any.
There is no reason (and no way) to create multiple renderers for a same window in SDL2, as pointed by keltar, in the file src/render/render.c
.
if (SDL_GetRenderer(window)) {
SDL_SetError("Renderer already associated with window");
return NULL;
}
So why SDL_Window
and SDL_Renderer
are two separate structs ? Because of polymorphism.
Let's see why, by browsing the SDL2 sources. This is the directory src/render
.
From the file sysrender.h
:
struct SDL_Renderer
{
const void *magic;
void (*WindowEvent) (SDL_Renderer * renderer, const SDL_WindowEvent *event);
int (*GetOutputSize) (SDL_Renderer * renderer, int *w, int *h);
SDL_bool (*SupportsBlendMode)(SDL_Renderer * renderer, SDL_BlendMode blendMode);
int (*CreateTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
int (*SetTextureColorMod) (SDL_Renderer * renderer,
SDL_Texture * texture);
int (*SetTextureAlphaMod) (SDL_Renderer * renderer,
SDL_Texture * texture);
int (*SetTextureBlendMode) (SDL_Renderer * renderer,
SDL_Texture * texture);
int (*RenderClear) (SDL_Renderer * renderer);
...
};
A renderer has a lot of function pointers, and these pointers are setted from the files in the subdirectories opengl
for example. Each driver defines its own instance of SDL_Renderer with its own functions. By doing this, SDL2 achieves polymorphism in C.
So you need only one instance of a renderer, but it should be different from a window ; a window is OS-specific while renderer is driver-specific.