Is that possible to do a texture copy similar to SDL_RenderCopy?


In my simple VNC client with SDL, I am in trouble to find a proper way to do screen copy operation. It should copy a source rectangular content to a destination rectangular within a texture.

I would like to avoid to have a local copy of the screen content.
I am using a stream based texture for updating the screen.

I search around the web and could not found a proper way except to keep a local copy of the screen content.

  1. SDL_RenderCopy could not be used, it will mess up the display content, as it does not update the texture.
  2. Using SDL_RenderGetPixels looks a little bit better, but it is not always successful to get a clean screen, some artifacts would be introduced some times if moving window quickly.
  3. SDL_UnlockTexure does not guarantee the pixel pointer returns contain the proper data, it is for write only.

If using a targeted texture, I got segmentation fault some times in the GL driver when there a lots of changes on screen (even with SDL_GL_SetSwapInterval(1);).

Is there a better way to do that, or I have to keep a local copy of the screen content?

I use a target texture, and have never noticed any problems of this sort (on multiple platforms, including Windows, Linux, MacOS, Android, iOS and Emscripten/Wasm) so I don’t know why it’s failing for you. It’s the best solution for copying from the ‘screen’, in my opinion (SDL_RenderGetPixels() can be very slow).

I modified my code, with target texture, it no longer crashed. But some how the the copy operation does not always correct, if I just move a terminate window with some fixed color text shown on it, some times some artifacts are showing on the final display, even without SDL_RENDERER_ACCELERATED. The code that do the copies are:

// src and dst has been setup
if (SDL_SetRenderTarget(ctx->renderer, ctx->texture) < 0) {
    printf( "SDL_SetRenderTarget() failed: %s\n", SDL_GetError());
    return -1;
} // if
if (SDL_RenderCopy(ctx->renderer, ctx->texture, &src, &dst) < 0) {
    fprintf("SDL_RenderCopy() failed: %s\n", SDL_GetError());
    return -1;
} // if
if (SDL_SetRenderTarget(ctx->renderer, NULL) < 0) {
    printf("SDL_SetRenderTarget() failed: %s\n", SDL_GetError());
    return -1;
} // if

A question, could the region of src and dst be overlapped?

I’ve always created a temporary, intermediate, texture (copy src to temp, copy temp to dst); it’s more steps, but should be guaranteed to work (and the source and destination can overlap without it making any difference).

Thanks rtrussell, that solves the problem! Copy without problem now.

1 Like