Fixed GL_RenderWritePixels() - thanks Ryan!
authorSam Lantinga <slouken@libsdl.org>
Wed, 06 Jan 2010 06:12:01 +0000
changeset 36078b4c0320638e
parent 3606 1a4456a01995
child 3608 a5a37f850d83
Fixed GL_RenderWritePixels() - thanks Ryan!
src/video/SDL_renderer_gl.c
     1.1 --- a/src/video/SDL_renderer_gl.c	Wed Jan 06 04:33:31 2010 +0000
     1.2 +++ b/src/video/SDL_renderer_gl.c	Wed Jan 06 06:12:01 2010 +0000
     1.3 @@ -1454,8 +1454,11 @@
     1.4                       Uint32 pixel_format, const void * pixels, int pitch)
     1.5  {
     1.6      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     1.7 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
     1.8      GLint internalFormat;
     1.9      GLenum format, type;
    1.10 +    Uint8 *src, *dst, *tmp;
    1.11 +    int length, rows;
    1.12  
    1.13      if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
    1.14          /* FIXME: Do a temp copy to a format that is supported */
    1.15 @@ -1463,8 +1466,6 @@
    1.16          return -1;
    1.17      }
    1.18  
    1.19 -    /* FIXME: We need to copy the data and flip it */
    1.20 -
    1.21      if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) {
    1.22          data->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
    1.23      } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) {
    1.24 @@ -1474,8 +1475,21 @@
    1.25      data->glPixelStorei(GL_UNPACK_ROW_LENGTH,
    1.26                          (pitch / bytes_per_pixel(pixel_format)));
    1.27  
    1.28 -    data->glRasterPos2i(rect->x, rect->y);
    1.29 -    data->glDrawPixels(rect->w, rect->h, format, type, pixels);
    1.30 +    /* Flip the rows to be bottom-up */
    1.31 +    length = rect->h * rect->w * pitch;
    1.32 +    tmp = SDL_stack_alloc(Uint8, length);
    1.33 +    src = (Uint8*)pixels + (rect->h-1)*pitch;
    1.34 +    dst = (Uint8*)tmp;
    1.35 +    rows = rect->h;
    1.36 +    while (rows--) {
    1.37 +        SDL_memcpy(dst, src, pitch);
    1.38 +        dst += pitch;
    1.39 +        src -= pitch;
    1.40 +    }
    1.41 +
    1.42 +    data->glRasterPos2i(rect->x, (window->h-rect->y));
    1.43 +    data->glDrawPixels(rect->w, rect->h, format, type, tmp);
    1.44 +    SDL_stack_free(tmp);
    1.45  
    1.46      return 0;
    1.47  }