SDL_RenderReadPixels(3)

πŸ’– SDL_RenderReadPixels(3) πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS corner of the manual, where we're talking about SDL_RenderReadPixels β€” the little function that literally screenshots your renderer. no cap. let's get into it 🫢


✨ NAME ✨

SDL_RenderReadPixels β€” read pixels from the current rendering target. it's giving "grab the pixels off the screen and hand them to you in a surface." iconic behavior honestly πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_render.h>

SDL_Surface * SDL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect);

look at her. two params. no drama. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so the surface you get back? it contains the pixels inside the area you asked for, clipped to the current viewport. and bestie, when you're done with it you HAVE to free it with SDL_DestroySurface(). it's a whole new surface, she's not gonna clean up after herself. πŸ™ˆ

highkey important: this returns the actual pixels on the screen. so if you're out here using logical presentation, you should use SDL_GetRenderLogicalPresentationRect() to get the area that actually contains your content. otherwise you're gonna be reading the wrong region and be like "why is my screenshot cursed 😭". iykyk.

⚠️ WARNING: this is a very slow operation, and you should not use it frequently. like, not every frame, bestie, please. and if you're using this on the main rendering target, it should be called after rendering and before SDL_RenderPresent(). that's the window. that's the vibe check. don't miss it.

✨ FUNCTION PARAMETERS ✨

  • renderer β€” the rendering context. your main character. the renderer in question. πŸ‘‘
  • rect β€” an SDL_Rect structure representing the area to read, which will be clipped to the current viewport. or NULL if you want the entire viewport. your choice, gorgeous. 🌈

πŸ’œ RETURN VALUE πŸ’œ

returns a new SDL_Surface on success, or NULL on failure. and if you got NULL? call SDL_GetError() for more info, she'll tell you what went wrong fr fr. πŸ’€


🧚 THREAD SAFETY 🧚

this function should only be called on the main thread. periodt. don't try to be cute and call it from a worker thread, that's not the move. 🚫


⭐ AVAILABILITY ⭐

this function is available since SDL 3.2.0. so if you're on something older, no pixels for you, sorry bestie. πŸ“


πŸŽ€ SEE ALSO πŸŽ€

  • SDL_DestroySurface()
  • SDL_GetRenderLogicalPresentationRect()
  • SDL_RenderPresent()
  • SDL_GetError()

SDL_RenderReadPixels β€” go forth and screenshot responsibly, my love. don't spam it, free your surfaces, and stay on the main thread. that's the whole entire assignment. πŸ’–βœ¨πŸ¦‹

~ brought to you by Simple Directmedia Layer, SDL 3.4.18, dated SDL_RenderReadPixels(3) ~ πŸŒΈπŸ’…πŸŽ€

πŸ“¦ where this page comes from