SDL_CreateTextureFromSurface(3)

πŸ’– SDL_CreateTextureFromSurface πŸ’–

okay bestie, welcome to SDL3 FUNCTIONS β€” this one's called SDL_CreateTextureFromSurface and it does exactly what it says on the tin: you hand it a surface, it hands you back a whole texture. glow-up arc, fr fr. ✨


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_render.h>

SDL_Texture * SDL_CreateTextureFromSurface(SDL_Renderer *renderer, SDL_Surface *surface);

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea, and it's important tea: the surface is not modified or freed by this function. like, it doesn't touch your surface, it doesn't take it out, it doesn't do anything to it. it just looks at it and copies the vibes over. you still own that surface, you still gotta deal with it yourself. no cap. πŸ’…

the SDL_TextureAccess hint for the created texture is SDL_TEXTUREACCESS_STATIC. that's just the way it is, that's its whole personality. πŸŽ€

and okay, this is the part people get caught off guard by: the pixel format of the created texture may be different from the pixel format of the surface. yeah. they don't have to match. if you need to know what format you actually ended up with, you can query it using the SDL_PROP_TEXTURE_FORMAT_NUMBER property. iykyk. πŸ”


πŸ’œ FUNCTION PARAMETERS πŸ’œ

  • renderer β€” the rendering context. this is your main character, the one doing the rendering. it needs to exist and it needs to be the one you want this texture to belong to.
  • surface β€” the SDL_Surface structure containing pixel data used to fill the texture. this is your source material bestie, the raw pixel data getting poured into the new texture. 🫢

⭐ RETURN VALUE ⭐

you get the created texture back! πŸŽ‰ icon. it ate.

but if it fails, you get NULL. and listen, don't just sit there wondering what went wrong β€” call SDL_GetError() for more information, she will tell you. 😭


πŸ”₯ THREAD SAFETY πŸ”₯

this function should only be called on the main thread. no side quests, no background threads, no sneaking it into a worker. main thread only, periodt. πŸ’€

🌈 AVAILABILITY 🌈

this function is available since SDL 3.2.0. so if you're on something older, bestie, it's giving "not found" and you gotta upgrade. πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘

(and yes, you WILL need that last one eventually β€” textures don't clean themselves up, that's your job πŸ’–)


Simple Directmedia Layer Β· SDL 3.4.18 Β· SDL_CreateTextureFromSurface(3)

okay that's the whole page bestie, go make some textures, you're literally gonna slay πŸ§šβœ¨πŸ’œ

πŸ“¦ where this page comes from