Implemented RenderReadPixels for the OpenGL ES 2.0 renderer.
authorSam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 02:55:21 -0400
changeset 6042ff2ab1e8d516
parent 6041 69d7539ff51f
child 6043 46d78e48ae8b
Implemented RenderReadPixels for the OpenGL ES 2.0 renderer.
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/opengles2/SDL_render_gles2.c	Mon Oct 31 02:44:21 2011 -0400
     1.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Mon Oct 31 02:55:21 2011 -0400
     1.3 @@ -786,6 +786,8 @@
     1.4  static int GLES2_RenderFillRects(SDL_Renderer *renderer, const SDL_Rect *rects, int count);
     1.5  static int GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect,
     1.6                              const SDL_Rect *dstrect);
     1.7 +static int GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
     1.8 +                    Uint32 pixel_format, void * pixels, int pitch);
     1.9  static void GLES2_RenderPresent(SDL_Renderer *renderer);
    1.10  
    1.11  
    1.12 @@ -1047,6 +1049,57 @@
    1.13      return 0;
    1.14  }
    1.15  
    1.16 +static int
    1.17 +GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.18 +                    Uint32 pixel_format, void * pixels, int pitch)
    1.19 +{
    1.20 +    SDL_Window *window = renderer->window;
    1.21 +    Uint32 temp_format = SDL_PIXELFORMAT_ABGR8888;
    1.22 +    void *temp_pixels;
    1.23 +    int temp_pitch;
    1.24 +    Uint8 *src, *dst, *tmp;
    1.25 +    int w, h, length, rows;
    1.26 +    int status;
    1.27 +
    1.28 +    GLES2_ActivateRenderer(renderer);
    1.29 +
    1.30 +    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
    1.31 +    temp_pixels = SDL_malloc(rect->h * temp_pitch);
    1.32 +    if (!temp_pixels) {
    1.33 +        SDL_OutOfMemory();
    1.34 +        return -1;
    1.35 +    }
    1.36 +
    1.37 +    SDL_GetWindowSize(window, &w, &h);
    1.38 +
    1.39 +    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    1.40 +
    1.41 +    glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
    1.42 +                       GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
    1.43 +
    1.44 +    /* Flip the rows to be top-down */
    1.45 +    length = rect->w * SDL_BYTESPERPIXEL(temp_format);
    1.46 +    src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
    1.47 +    dst = (Uint8*)temp_pixels;
    1.48 +    tmp = SDL_stack_alloc(Uint8, length);
    1.49 +    rows = rect->h / 2;
    1.50 +    while (rows--) {
    1.51 +        SDL_memcpy(tmp, dst, length);
    1.52 +        SDL_memcpy(dst, src, length);
    1.53 +        SDL_memcpy(src, tmp, length);
    1.54 +        dst += temp_pitch;
    1.55 +        src -= temp_pitch;
    1.56 +    }
    1.57 +    SDL_stack_free(tmp);
    1.58 +
    1.59 +    status = SDL_ConvertPixels(rect->w, rect->h,
    1.60 +                               temp_format, temp_pixels, temp_pitch,
    1.61 +                               pixel_format, pixels, pitch);
    1.62 +    SDL_free(temp_pixels);
    1.63 +
    1.64 +    return status;
    1.65 +}
    1.66 +
    1.67  static void
    1.68  GLES2_RenderPresent(SDL_Renderer *renderer)
    1.69  {
    1.70 @@ -1176,6 +1229,7 @@
    1.71      renderer->RenderDrawLines     = &GLES2_RenderDrawLines;
    1.72      renderer->RenderFillRects     = &GLES2_RenderFillRects;
    1.73      renderer->RenderCopy          = &GLES2_RenderCopy;
    1.74 +    renderer->RenderReadPixels    = &GLES2_RenderReadPixels;
    1.75      renderer->RenderPresent       = &GLES2_RenderPresent;
    1.76      renderer->DestroyTexture      = &GLES2_DestroyTexture;
    1.77      renderer->DestroyRenderer     = &GLES2_DestroyRenderer;