Fixed bug 1162 (Error calling SDL_RenderReadPixels() with format=0)
authorSam Lantinga <slouken@libsdl.org>
Thu, 10 Mar 2011 01:03:43 -0800
changeset 546546bd121b04a2
parent 5464 bec064e902cf
child 5466 006883d5fa51
Fixed bug 1162 (Error calling SDL_RenderReadPixels() with format=0)
src/render/opengl/SDL_render_gl.c
     1.1 --- a/src/render/opengl/SDL_render_gl.c	Thu Mar 10 01:00:43 2011 -0800
     1.2 +++ b/src/render/opengl/SDL_render_gl.c	Thu Mar 10 01:03:43 2011 -0800
     1.3 @@ -921,44 +921,56 @@
     1.4  {
     1.5      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     1.6      SDL_Window *window = renderer->window;
     1.7 +    Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
     1.8 +    void *temp_pixels;
     1.9 +    int temp_pitch;
    1.10      GLint internalFormat;
    1.11      GLenum format, type;
    1.12      Uint8 *src, *dst, *tmp;
    1.13      int w, h, length, rows;
    1.14 +    int status;
    1.15  
    1.16      GL_ActivateRenderer(renderer);
    1.17  
    1.18 -    if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
    1.19 -        /* FIXME: Do a temp copy to a format that is supported */
    1.20 -        SDL_SetError("Unsupported pixel format");
    1.21 +    temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
    1.22 +    temp_pixels = SDL_malloc(rect->h * temp_pitch);
    1.23 +    if (!temp_pixels) {
    1.24 +        SDL_OutOfMemory();
    1.25          return -1;
    1.26      }
    1.27  
    1.28 +    convert_format(data, temp_format, &internalFormat, &format, &type);
    1.29 +
    1.30      SDL_GetWindowSize(window, &w, &h);
    1.31  
    1.32      data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
    1.33      data->glPixelStorei(GL_PACK_ROW_LENGTH,
    1.34 -                        (pitch / SDL_BYTESPERPIXEL(pixel_format)));
    1.35 +                        (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
    1.36  
    1.37      data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
    1.38 -                       format, type, pixels);
    1.39 +                       format, type, temp_pixels);
    1.40  
    1.41      /* Flip the rows to be top-down */
    1.42 -    length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
    1.43 -    src = (Uint8*)pixels + (rect->h-1)*pitch;
    1.44 -    dst = (Uint8*)pixels;
    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 += pitch;
    1.55 -        src -= pitch;
    1.56 +        dst += temp_pitch;
    1.57 +        src -= temp_pitch;
    1.58      }
    1.59      SDL_stack_free(tmp);
    1.60  
    1.61 -    return 0;
    1.62 +    status = SDL_ConvertPixels(rect->w, rect->h,
    1.63 +                               temp_format, temp_pixels, temp_pitch,
    1.64 +                               pixel_format, pixels, pitch);
    1.65 +    SDL_free(temp_pixels);
    1.66 +
    1.67 +    return status;
    1.68  }
    1.69  
    1.70  static void