This code works for OpenGL ES 1.1 as well! :)
authorSam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 03:06:32 -0400
changeset 604346d78e48ae8b
parent 6042 ff2ab1e8d516
child 6044 35448a5ea044
This code works for OpenGL ES 1.1 as well! :)
src/render/opengles/SDL_render_gles.c
     1.1 --- a/src/render/opengles/SDL_render_gles.c	Mon Oct 31 02:55:21 2011 -0400
     1.2 +++ b/src/render/opengles/SDL_render_gles.c	Mon Oct 31 03:06:32 2011 -0400
     1.3 @@ -64,6 +64,8 @@
     1.4  static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
     1.5                             const SDL_Rect * srcrect,
     1.6                             const SDL_Rect * dstrect);
     1.7 +static int GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
     1.8 +                    Uint32 pixel_format, void * pixels, int pitch);
     1.9  static void GLES_RenderPresent(SDL_Renderer * renderer);
    1.10  static void GLES_DestroyTexture(SDL_Renderer * renderer,
    1.11                                  SDL_Texture * texture);
    1.12 @@ -216,6 +218,7 @@
    1.13      renderer->RenderDrawLines = GLES_RenderDrawLines;
    1.14      renderer->RenderFillRects = GLES_RenderFillRects;
    1.15      renderer->RenderCopy = GLES_RenderCopy;
    1.16 +    renderer->RenderReadPixels = GLES_RenderReadPixels;
    1.17      renderer->RenderPresent = GLES_RenderPresent;
    1.18      renderer->DestroyTexture = GLES_DestroyTexture;
    1.19      renderer->DestroyRenderer = GLES_DestroyRenderer;
    1.20 @@ -744,6 +747,57 @@
    1.21      return 0;
    1.22  }
    1.23  
    1.24 +static int
    1.25 +GLES_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.26 +                    Uint32 pixel_format, void * pixels, int pitch)
    1.27 +{
    1.28 +    SDL_Window *window = renderer->window;
    1.29 +    Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
    1.30 +    void *temp_pixels;
    1.31 +    int temp_pitch;
    1.32 +    Uint8 *src, *dst, *tmp;
    1.33 +    int w, h, length, rows;
    1.34 +    int status;
    1.35 +
    1.36 +    GLES_ActivateRenderer(renderer);
    1.37 +
    1.38 +    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
    1.39 +    temp_pixels = SDL_malloc(rect->h * temp_pitch);
    1.40 +    if (!temp_pixels) {
    1.41 +        SDL_OutOfMemory();
    1.42 +        return -1;
    1.43 +    }
    1.44 +
    1.45 +    SDL_GetWindowSize(window, &w, &h);
    1.46 +
    1.47 +    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    1.48 +
    1.49 +    glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
    1.50 +                       GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
    1.51 +
    1.52 +    /* Flip the rows to be top-down */
    1.53 +    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
    1.54 +    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
    1.55 +    dst = (Uint8*)temp_pixels;
    1.56 +    tmp = SDL_stack_alloc(Uint8, length);
    1.57 +    rows = rect->h / 2;
    1.58 +    while (rows--) {
    1.59 +        SDL_memcpy(tmp, dst, length);
    1.60 +        SDL_memcpy(dst, src, length);
    1.61 +        SDL_memcpy(src, tmp, length);
    1.62 +        dst += temp_pitch;
    1.63 +        src -= temp_pitch;
    1.64 +    }
    1.65 +    SDL_stack_free(tmp);
    1.66 +
    1.67 +    status = SDL_ConvertPixels(rect->w, rect->h,
    1.68 +                               temp_format, temp_pixels, temp_pitch,
    1.69 +                               pixel_format, pixels, pitch);
    1.70 +    SDL_free(temp_pixels);
    1.71 +
    1.72 +    return status;
    1.73 +}
    1.74 +
    1.75  static void
    1.76  GLES_RenderPresent(SDL_Renderer * renderer)
    1.77  {