1
votes

I'm currently following LazyFoo's SDL tutorial and I've reached the Texture Manipulation part but I'm running into a problem. The first time I call SDL_LockTexture it works fine to update the texture. However, after SDL_UnlockTexture, if I attempt to lock the texture again it now returns a pointer to a set of zeroed pixels, instead of what they were set to after the last unlock.

    SDL_LockTexture(newTexture, NULL, &pixels, &pitch);
    memcpy(pixels, formattedSurface->pixels, formattedSurface->pitch * formattedSurface->h);

    SDL_UnlockTexture(newTexture);
    pixels = NULL;

This is the initial code that copies the pixels of a loaded surface to an empty texture, and if rendered at this point it displays the correct image.

However, if the texture is locked and unlocked again then the image displayed is a black box (the value of each pixel being 0)

I'm completely stumped. Please let me know if any more is needed.

1

1 Answers

3
votes

As stated on the wiki here the pixel data made available by SDL_LockTexture is intended only for writing data to a texture. It may not actually contain the current data for that texture. If you need a copy of a texture's pixel data you need to keep that stored somewhere else.