Textures & surfacesSDL 3.2

SDL_CreateTextureFromSurface

Copies an image from CPU memory (a surface) into a texture the renderer can draw.

Signature

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

How it works

Two kinds of image live in SDL. An SDL_Surface is pixels in ordinary RAM: image loaders (SDL_LoadBMP, IMG_Load from SDL3_image) and text renderers (TTF_RenderText_Blended) give you one. An SDL_Texture is pixels on the GPU, which is what the renderer draws fast.

This call uploads the surface. The surface is not changed or freed: once you have the texture, destroy the surface with SDL_DestroySurface.

Parameters

rendererSDL_Renderer *

The renderer that will own the texture.

surfaceSDL_Surface *

The image to copy.

Return value

A static texture, or NULL on failure.

Example

example.cpp
1SDL_Surface* surface = IMG_Load("assets/player.png");
2if (!surface) { SDL_Log("IMG_Load: %s", SDL_GetError()); return; }
3SDL_Texture* player = SDL_CreateTextureFromSurface(renderer, surface);
4SDL_DestroySurface(surface);    // the GPU has its own copy now
Official SDL wiki