SDL_LoadPNG(3)

πŸ“¦
version 3.4.18 Β· 5 versions
, in fedora 43, fedora 44

πŸ’– SDL_LoadPNG πŸ’–

okay bestie, so this page is straight out of the SDL3 FUNCTIONS manual, and it's literally just "how do I get a PNG off my disk and into a surface" β€” and honestly? iconic of it. πŸŽ€


✨ NAME ✨

SDL_LoadPNG β€” loads a PNG image from a file, no cap. πŸ¦‹


✨ SYNOPSIS ✨

#include <SDL3/SDL_surface.h>

SDL_Surface * SDL_LoadPNG(const char *file);

like. that's the whole vibe. one include, one function, we go. πŸ’…


✨ DESCRIPTION ✨

so here's the tea, and you need to hear this part: this function is meant to be a convenience function for loading images from trusted sources. trusted. like, images you made, images your bestie made, images that are not out here trying to do crimes to your computer. 🫢

if you want to load arbitrary images β€” like random stuff from the internet, mystery files, whatever a stranger slid into your downloads folder β€” you should be using libpng or another image loading library designed with security in mind. this is not that. this is the "i trust this file" function, periodt.

also!! the new surface you get back? you should free it with SDL_DestroySurface(). and not in a "eh, maybe later" way, in a "actually do it" way, because not doing so will result in a memory leak. 😭 your RAM will simply sit there holding onto vibes it doesn't need. don't do that to your RAM.


✨ FUNCTION PARAMETERS ✨

  • file β€” the PNG file to load. that's it, that's the whole job. just hand it the file and let it cook. πŸ”₯

✨ RETURN VALUE ✨

it hands you back a pointer to a brand new SDL_Surface structure β€” main character energy, freshly allocated, yours to keep (and to free πŸ‘€).

or… it returns NULL on failure, which is the sad path. πŸ’€ if that happens, call SDL_GetError() for more information so you can find out what went wrong instead of just staring at the NULL like πŸ™ˆ.


✨ THREAD SAFETY ✨

good news, bestie: it is safe to call this function from any thread. 🧚 go wild. threads everywhere. slay.


✨ AVAILABILITY ✨

this function has been available since SDL 3.4.0. so if you're on something older, that's a you problem, sorry not sorry. 🌸


✨ SEE ALSO ✨


fr fr, the whole takeaway: trusted sources only, and free your surfaces. that's the lore. πŸ“

and that's the whole page, written by the Simple Directmedia Layer girlies, living in SDL 3.4.18, dated SDL_LoadPNG(3) β€” mwah, love that for us. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from