Not sure if this is a problem with SDL or if my expectations are just
too high. I was trying to do the following:
- Load a PNG into a texture.
- Get the texture pixel data to mirror the image
- Update the texture from the image data
To do number 1 and 3, I originally have the following code
void surface_sdl::set_data(void * data, u_int16 l, u_int16 h,
u_int8 bytes_per_pixel, u_int32 red_mask, u_int32 green_mask, u_int32
blue_mask, u_int32 alpha_mask)
if (Surface) SDL_DestroyTexture(Surface);
u_int32 format = SDL_MasksToPixelFormatEnum (bytes_per_pixel *
8, red_mask, green_mask, blue_mask, alpha_mask);
Surface = SDL_CreateTexture (display->Renderer, format,
SDL_TEXTUREACCESS_STREAMING, l, h);
SDL_UpdateTexture(Surface, NULL, data, bytes_per_pixel * l);
For number 2, there’s a complimentary method that uses SDL_LockTexture
to get hold of the pixel data.
To my dismay, I noticed that SDL_UpdateTexture does not update the
internal texture->pixels, even though a streaming texture is used. So
SDL_LockTexture returns garbage, while the original texture displays
fine on screen.
The question is, is this an oversight on the side of SDL, or is my
For now I worked around this issue by using SDL_LockTexture and
SDL_UnlockTexture instead of SDL_UpdateTexture, but that requires 11
lines of code instead of 1.
Somewhat unrelated, but something I would find useful:
- Allow to specify the texture access mode in SDL_CreateTextureFromSurface.
- Allow to change the access mode of an existing texture (although I
guess that can be had with a few lines of client code, but not without
recreating the whole texture).