How to use pointer returned by LockTexture


#1

How do I write to the texture returned by SDL_LockTexture? I’m using something like:

void* pixels;
int pitch;
Uint32* dst;

SDL_Texture* buffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
SDL_LockTexture(text,NULL, &pixels, &pitch)
dst = (Uint32 *) pixels;
for(int i=0;i<WIDTH;i++){
    for(int j=0;j<HEIGHT;j++){
            dst[ j*pitch + i ] = 0xFF00FF00; // or any other color
    }
}
SDL_UnlockTexture(text)

and I always get a blacck screen or a segmentation fault, that shows me that I’m accessing memory that I shouldn’t, but I can’t see where.

What I’m missing? I’m creating the texture wrong? I’m locking it wrong?


#2

Pitch is in bytes: https://wiki.libsdl.org/SDL_LockTexture . So it should be divided by 4 in this scenario because dst is pointer to Uint32.

In the example “buffer” and “text” variables seem mixed up.


#3

Thank you a lot man, i missed that about pitch in the docs.

the buffer/text is just copy/paste error, it’s working now.