Implemented read/write pixels for the X11 renderer
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Dec 2009 06:52:17 +0000
changeset 35595f26a7eb5ff0
parent 3558 5f038ec1a1af
child 3560 5543db4239e6
Implemented read/write pixels for the X11 renderer
src/video/x11/SDL_x11render.c
src/video/x11/SDL_x11sym.h
     1.1 --- a/src/video/x11/SDL_x11render.c	Mon Dec 14 06:35:59 2009 +0000
     1.2 +++ b/src/video/x11/SDL_x11render.c	Mon Dec 14 06:52:17 2009 +0000
     1.3 @@ -58,6 +58,10 @@
     1.4                             int count);
     1.5  static int X11_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
     1.6                            const SDL_Rect * srcrect, const SDL_Rect * dstrect);
     1.7 +static int X11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
     1.8 +                                Uint32 format, void * pixels, int pitch);
     1.9 +static int X11_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.10 +                                 Uint32 format, const void * pixels, int pitch);
    1.11  static void X11_RenderPresent(SDL_Renderer * renderer);
    1.12  static void X11_DestroyTexture(SDL_Renderer * renderer,
    1.13                                 SDL_Texture * texture);
    1.14 @@ -208,6 +212,8 @@
    1.15      renderer->RenderLines = X11_RenderLines;
    1.16      renderer->RenderRects = X11_RenderRects;
    1.17      renderer->RenderCopy = X11_RenderCopy;
    1.18 +    renderer->RenderReadPixels = X11_RenderReadPixels;
    1.19 +    renderer->RenderWritePixels = X11_RenderWritePixels;
    1.20      renderer->RenderPresent = X11_RenderPresent;
    1.21      renderer->DestroyTexture = X11_DestroyTexture;
    1.22      renderer->DestroyRenderer = X11_DestroyRenderer;
    1.23 @@ -937,6 +943,70 @@
    1.24      return 0;
    1.25  }
    1.26  
    1.27 +static int
    1.28 +X11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.29 +                     Uint32 format, void * pixels, int pitch)
    1.30 +{
    1.31 +    X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
    1.32 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
    1.33 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    1.34 +    Uint32 screen_format = display->current_mode.format;
    1.35 +    XImage *image;
    1.36 +
    1.37 +    image = XGetImage(data->display, data->drawable, rect->x, rect->y,
    1.38 +                      rect->w, rect->h, AllPlanes, ZPixmap);
    1.39 +
    1.40 +    SDL_ConvertPixels(rect->w, rect->h,
    1.41 +                      screen_format, image->data, image->bytes_per_line,
    1.42 +                      format, pixels, pitch);
    1.43 +
    1.44 +    XDestroyImage(image);
    1.45 +    return 0;
    1.46 +}
    1.47 +
    1.48 +static int
    1.49 +X11_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.50 +                      Uint32 format, const void * pixels, int pitch)
    1.51 +{
    1.52 +    X11_RenderData *data = (X11_RenderData *) renderer->driverdata;
    1.53 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
    1.54 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    1.55 +    Uint32 screen_format = display->current_mode.format;
    1.56 +    XImage *image;
    1.57 +    void *image_pixels;
    1.58 +    int image_pitch;
    1.59 +
    1.60 +    image_pitch = rect->w * SDL_BYTESPERPIXEL(screen_format);
    1.61 +    image_pixels = SDL_malloc(rect->h * image_pitch);
    1.62 +    if (!image_pixels) {
    1.63 +        SDL_OutOfMemory();
    1.64 +        return -1;
    1.65 +    }
    1.66 +
    1.67 +    image = XCreateImage(data->display, data->visual,
    1.68 +                         data->depth, ZPixmap, 0, image_pixels,
    1.69 +                         rect->w, rect->h,
    1.70 +                         SDL_BYTESPERPIXEL(screen_format) * 8,
    1.71 +                         image_pitch);
    1.72 +    if (!image) {
    1.73 +        SDL_SetError("XCreateImage() failed");
    1.74 +        return -1;
    1.75 +    }
    1.76 +
    1.77 +    SDL_ConvertPixels(rect->w, rect->h,
    1.78 +                      format, pixels, pitch,
    1.79 +                      screen_format, image->data, image->bytes_per_line);
    1.80 +
    1.81 +    XPutImage(data->display, data->drawable, data->gc,
    1.82 +              image, 0, 0, rect->x, rect->y, rect->w, rect->h);
    1.83 +
    1.84 +    image->data = NULL;
    1.85 +    XDestroyImage(image);
    1.86 +
    1.87 +    SDL_free(image_pixels);
    1.88 +    return 0;
    1.89 +}
    1.90 +
    1.91  static void
    1.92  X11_RenderPresent(SDL_Renderer * renderer)
    1.93  {
     2.1 --- a/src/video/x11/SDL_x11sym.h	Mon Dec 14 06:35:59 2009 +0000
     2.2 +++ b/src/video/x11/SDL_x11sym.h	Mon Dec 14 06:52:17 2009 +0000
     2.3 @@ -63,6 +63,7 @@
     2.4  SDL_X11_SYM(int,XFreeModifiermap,(XModifierKeymap* a),(a),return)
     2.5  SDL_X11_SYM(int,XFreePixmap,(Display* a,Pixmap b),(a,b),return)
     2.6  SDL_X11_SYM(int,XGetErrorDatabaseText,(Display* a,_Xconst char* b,_Xconst char* c,_Xconst char* d,char* e,int f),(a,b,c,d,e,f),return)
     2.7 +SDL_X11_SYM(XImage*,XGetImage,(Display* a,Drawable b,int c,int d,unsigned int e,unsigned int f,unsigned long g, int h),(a,b,c,d,e,f,g,h),return)
     2.8  SDL_X11_SYM(XModifierKeymap*,XGetModifierMapping,(Display* a),(a),return)
     2.9  SDL_X11_SYM(int,XGetPointerControl,(Display* a,int* b,int* c,int* d),(a,b,c,d),return)
    2.10  SDL_X11_SYM(int,XGetRGBColormaps,(Display* a,Window b,XStandardColormap **c,int *d,Atom e),(a,b,c,d,e),return)