Does SDL_RenderCopy check dstrect boundaries?

Hi there,

I just wonder if the SDL_RenderCopy is checking the boundaries of the dstrect rectangle ?
I mean : if the dstrect rectangle boundaries are partially outside the scope of the renderer, does the SDL_RenderCopy automatically cut out the “not visible” part ? Or will it mess the memory ? Because I want to know if I have to make the check by myself and adapt the dstrect, or will it be redundant…

I made some testings and it seems that dstrect rectangle can be anywhere, inside the renderer, over its boudaries, and even completly outside. I also tried to read the SDL2 sources, and it seems to me that this intersection check is here, but I am not sure.

Does anyone have a more precise idea on the subject ?


SDL2 does not stop you trying to draw things outside of the window.
It will still draw but you will only see the part inside the window dimensions.
You need to implement this yourself,

if (m_dst.x < 0) { 
    m_dst.x = 0
// etc

Apparently, it does not stop me from drawing, and it does what i’m waiting for, but for the invisible part, I still wonder if it write something somewhere in memory, wasting ressources/time ? Because for me the basic renderer (not taking account of any viewport feature) has the same size as the window, and has the same limited amount of memory, no ?

Example : inside a classic 1920x1080 HD window/connected renderer, I draw a 32x32 texture, using a huge dstrect, like {-100000,-100000,100000,100000} ; it works and it draws only the portion inside the window (which is an overzoomed pixel). I suppose that the rest of this 200000x200000 pattern is not written anywhere in memory ?..

No, nothing will get messed up.

SDL_Renderer uses the GPU for the actual drawing, and the GPU itself clips polygons to the set viewport and scissor rectangle before rasterizing them. So while it’d be a good idea to do a quick bounding box check so you aren’t submitting sprites that are completely outside the view bounds (to prevent SDL, the backing API, and the GPU from wasting time setting up and submitting a sprite that will ultimately be completely clipped), nothing bad will happen or get messed up if you don’t.

edit: and if you’re pretty sure you won’t have a lot of sprites outside the view bounds, it might be faster to just skip the check, since on a modern GPU a few textured quads being offscreen won’t make a difference in performance.

1 Like

thx for your clear answer. That’s what I suspected.

Infact, I want to implement a very large map, with no predefined limits, (with a camera view that can pan and zoom everywhere), so I may have a lot of sprites completly outside the camera view : I’d better do some simple checks to avoid calling useless sprite copys…