Textures & surfacesSDL 3.2
SDL_LockTexture
Gives you a pointer to write a STREAMING texture's pixels directly from the CPU.
Signature
bool SDL_LockTexture(SDL_Texture *texture, const SDL_Rect *rect, void **pixels, int *pitch);
How it works
Lock, write every pixel, unlock: the new image is uploaded on SDL_UnlockTexture. It is write-only: the memory you get may NOT hold the old picture, so fill the whole locked area each time.
pitch is the length of one row in BYTES, and it can be larger than width Ă— bytes per pixel (rows may be padded for speed). Step from row to row by pitch, never by width.
Parameters
textureSDL_Texture *
A texture created with SDL_TEXTUREACCESS_STREAMING.
rectconst SDL_Rect *
Area to lock (integers), or NULL for the whole texture.
pixelsvoid **
Receives the pointer to the first pixel.
pitchint *
Receives the row length in bytes.
Return value
true on success; false if the texture is not STREAMING.
Example
example.cpp
1void* pixels; int pitch;
2if (SDL_LockTexture(screen, nullptr, &pixels, &pitch)) {
3 for (int y = 0; y < 240; ++y) {
4 Uint8* row = static_cast<Uint8*>(pixels) + y * pitch; // by pitch!
5 for (int x = 0; x < 320; ++x) {
6 row[x * 4 + 0] = Uint8(x); // R (SDL_PIXELFORMAT_RGBA32)
7 row[x * 4 + 1] = Uint8(y); // G
8 row[x * 4 + 2] = 128; // B
9 row[x * 4 + 3] = 255; // A
10 }
11 }
12 SDL_UnlockTexture(screen);
13}