src/render/opengl/SDL_render_gl.c
changeset 5465 46bd121b04a2
parent 5402 5d102cd8aef3
child 5484 e20c93bc9122
equal deleted inserted replaced
5464:bec064e902cf 5465:46bd121b04a2
   919 GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   919 GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   920                     Uint32 pixel_format, void * pixels, int pitch)
   920                     Uint32 pixel_format, void * pixels, int pitch)
   921 {
   921 {
   922     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   922     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   923     SDL_Window *window = renderer->window;
   923     SDL_Window *window = renderer->window;
       
   924     Uint32 temp_format = SDL_PIXELFORMAT_ARGB8888;
       
   925     void *temp_pixels;
       
   926     int temp_pitch;
   924     GLint internalFormat;
   927     GLint internalFormat;
   925     GLenum format, type;
   928     GLenum format, type;
   926     Uint8 *src, *dst, *tmp;
   929     Uint8 *src, *dst, *tmp;
   927     int w, h, length, rows;
   930     int w, h, length, rows;
       
   931     int status;
   928 
   932 
   929     GL_ActivateRenderer(renderer);
   933     GL_ActivateRenderer(renderer);
   930 
   934 
   931     if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
   935     temp_pitch = rect->w * SDL_BYTESPERPIXEL(temp_format);
   932         /* FIXME: Do a temp copy to a format that is supported */
   936     temp_pixels = SDL_malloc(rect->h * temp_pitch);
   933         SDL_SetError("Unsupported pixel format");
   937     if (!temp_pixels) {
       
   938         SDL_OutOfMemory();
   934         return -1;
   939         return -1;
   935     }
   940     }
       
   941 
       
   942     convert_format(data, temp_format, &internalFormat, &format, &type);
   936 
   943 
   937     SDL_GetWindowSize(window, &w, &h);
   944     SDL_GetWindowSize(window, &w, &h);
   938 
   945 
   939     data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
   946     data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
   940     data->glPixelStorei(GL_PACK_ROW_LENGTH,
   947     data->glPixelStorei(GL_PACK_ROW_LENGTH,
   941                         (pitch / SDL_BYTESPERPIXEL(pixel_format)));
   948                         (temp_pitch / SDL_BYTESPERPIXEL(temp_format)));
   942 
   949 
   943     data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
   950     data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
   944                        format, type, pixels);
   951                        format, type, temp_pixels);
   945 
   952 
   946     /* Flip the rows to be top-down */
   953     /* Flip the rows to be top-down */
   947     length = rect->w * SDL_BYTESPERPIXEL(pixel_format);
   954     length = rect->w * SDL_BYTESPERPIXEL(temp_format);
   948     src = (Uint8*)pixels + (rect->h-1)*pitch;
   955     src = (Uint8*)temp_pixels + (rect->h-1)*temp_pitch;
   949     dst = (Uint8*)pixels;
   956     dst = (Uint8*)temp_pixels;
   950     tmp = SDL_stack_alloc(Uint8, length);
   957     tmp = SDL_stack_alloc(Uint8, length);
   951     rows = rect->h / 2;
   958     rows = rect->h / 2;
   952     while (rows--) {
   959     while (rows--) {
   953         SDL_memcpy(tmp, dst, length);
   960         SDL_memcpy(tmp, dst, length);
   954         SDL_memcpy(dst, src, length);
   961         SDL_memcpy(dst, src, length);
   955         SDL_memcpy(src, tmp, length);
   962         SDL_memcpy(src, tmp, length);
   956         dst += pitch;
   963         dst += temp_pitch;
   957         src -= pitch;
   964         src -= temp_pitch;
   958     }
   965     }
   959     SDL_stack_free(tmp);
   966     SDL_stack_free(tmp);
   960 
   967 
   961     return 0;
   968     status = SDL_ConvertPixels(rect->w, rect->h,
       
   969                                temp_format, temp_pixels, temp_pitch,
       
   970                                pixel_format, pixels, pitch);
       
   971     SDL_free(temp_pixels);
       
   972 
       
   973     return status;
   962 }
   974 }
   963 
   975 
   964 static void
   976 static void
   965 GL_RenderPresent(SDL_Renderer * renderer)
   977 GL_RenderPresent(SDL_Renderer * renderer)
   966 {
   978 {