SDL_CreateSurfaceFrom(3)

๐Ÿ’– SDL_CreateSurfaceFrom(3) ๐Ÿ’–

okay bestie, so this is a page from the SDL3 FUNCTIONS manual, and today's main character is SDL_CreateSurfaceFrom โ€” the function that hands you a brand-new surface built around pixel data you already have. no copy, no drama, just vibes ๐Ÿฆ‹


โœจ NAME โœจ

SDL_CreateSurfaceFrom โ€” allocate a new surface with a specific pixel format and existing pixel data. it's giving "i brought my own pixels, you bring the surface" ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <SDL3/SDL_surface.h>

SDL_Surface * SDL_CreateSurfaceFrom(int width, int height, SDL_PixelFormat format, void *pixels, int pitch);

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

okay so here's the tea, and it's important tea: no copy is made of the pixel data. like, none. zero. the surface just points at your pixels. which means the pixel data is not managed automatically โ€” you have to free the surface before you free the pixel data. do it in that order, bestie, or you're gonna have a bad time ๐Ÿ˜ญ

pitch is the offset in bytes from one row of pixels to the next. so like, width*4 for SDL_PIXELFORMAT_RGBA8888. that's the math, no cap ๐Ÿงฎ

and the fun part: you may pass NULL for pixels and 0 for pitch to create a surface that you'll fill in with valid values later. build-a-bear but for surfaces, lowkey iconic ๐Ÿงธ


๐Ÿซถ FUNCTION PARAMETERS ๐Ÿซถ

  • width โ€” the width of the surface. yeah. that one. ๐Ÿ“
  • height โ€” the height of the surface. the other one. ๐Ÿ“
  • format โ€” the SDL_PixelFormat for the new surface's pixel format. pick your aesthetic โœจ
  • pixels โ€” a pointer to existing pixel data. your pixels. your problem (affectionately) ๐Ÿ’œ
  • pitch โ€” the number of bytes between each row, including padding. padding is real and it counts ๐ŸŽ€

โญ RETURN VALUE โญ

returns the new SDL_Surface structure that is created, or NULL on failure. if you get NULL, don't panic โ€” just call SDL_GetError() for more information and it'll spill the whole story ๐Ÿ’€


๐Ÿ”ฅ THREAD SAFETY ๐Ÿ”ฅ

it is safe to call this function from any thread. yes, any thread. multitask slay ๐Ÿ‘‘


๐ŸŒˆ AVAILABILITY ๐ŸŒˆ

this function is available since SDL 3.2.0. if your SDL is older than that, it's simply not invited to the party ๐Ÿ™ˆ


๐Ÿ’– SEE ALSO ๐Ÿ’–


and that's the whole page, bestie โ€” go forth and make surfaces ๐Ÿฆ‹

with love from the Simple Directmedia Layer girlies, SDL 3.4.18 era, SDL_CreateSurfaceFrom(3) forever ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from