Finished implementing RenderReadPixels()
authorSam Lantinga <slouken@libsdl.org>
Sat, 12 Dec 2009 04:01:40 +0000
changeset 3549686f0e69cd37
parent 3548 79af7b478fae
child 3550 6b69c5def097
Finished implementing RenderReadPixels()
src/video/win32/SDL_d3drender.c
     1.1 --- a/src/video/win32/SDL_d3drender.c	Sat Dec 12 01:04:57 2009 +0000
     1.2 +++ b/src/video/win32/SDL_d3drender.c	Sat Dec 12 04:01:40 2009 +0000
     1.3 @@ -1206,44 +1206,67 @@
     1.4  D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
     1.5                       Uint32 format, void * pixels, int pitch)
     1.6  {
     1.7 -    BYTE * pBytes;
     1.8 -    D3DLOCKED_RECT lockedRect;
     1.9 -    BYTE b, g, r, a;
    1.10 -    unsigned long index;
    1.11 -    int cur_mouse;
    1.12 -    int x, y;
    1.13 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
    1.14 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
    1.15 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    1.16 +    D3DSURFACE_DESC desc;
    1.17 +    LPDIRECT3DSURFACE9 backBuffer;
    1.18 +    LPDIRECT3DSURFACE9 surface;
    1.19 +    RECT d3drect;
    1.20 +    D3DLOCKED_RECT locked;
    1.21 +    HRESULT result;
    1.22  
    1.23 -    LPDIRECT3DSURFACE9 backBuffer;
    1.24 -    LPDIRECT3DSURFACE9 pickOffscreenSurface;
    1.25 -    D3DSURFACE_DESC desc;
    1.26 +    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
    1.27 +    if (FAILED(result)) {
    1.28 +        D3D_SetError("GetBackBuffer()", result);
    1.29 +        return -1;
    1.30 +    }
    1.31  
    1.32 -    D3D_RenderData * data = (D3D_RenderData *) renderer->driverdata;
    1.33 -    
    1.34 -    IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
    1.35 -    
    1.36 -    
    1.37 -    IDirect3DSurface9_GetDesc(backBuffer, &desc);
    1.38 +    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
    1.39 +    if (FAILED(result)) {
    1.40 +        D3D_SetError("GetDesc()", result);
    1.41 +        IDirect3DSurface9_Release(backBuffer);
    1.42 +        return -1;
    1.43 +    }
    1.44  
    1.45 -    IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &pickOffscreenSurface, NULL);
    1.46 +    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
    1.47 +    if (FAILED(result)) {
    1.48 +        D3D_SetError("CreateOffscreenPlainSurface()", result);
    1.49 +        IDirect3DSurface9_Release(backBuffer);
    1.50 +        return -1;
    1.51 +    }
    1.52  
    1.53 -    IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, pickOffscreenSurface);
    1.54 +    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
    1.55 +    if (FAILED(result)) {
    1.56 +        D3D_SetError("GetRenderTargetData()", result);
    1.57 +        IDirect3DSurface9_Release(surface);
    1.58 +        IDirect3DSurface9_Release(backBuffer);
    1.59 +        return -1;
    1.60 +    }
    1.61  
    1.62 -    IDirect3DSurface9_LockRect(pickOffscreenSurface, &lockedRect, NULL, D3DLOCK_READONLY);
    1.63 -    pBytes = (BYTE*)lockedRect.pBits;
    1.64 -    IDirect3DSurface9_UnlockRect(pickOffscreenSurface);
    1.65 +    d3drect.left = rect->x;
    1.66 +    d3drect.right = rect->x + rect->w;
    1.67 +    d3drect.top = rect->y;
    1.68 +    d3drect.bottom = rect->y + rect->h;
    1.69  
    1.70 -    // just to debug -->
    1.71 -    cur_mouse = SDL_SelectMouse(-1);
    1.72 -    SDL_GetMouseState(cur_mouse, &x, &y);
    1.73 -    index = (x * 4 + (y * lockedRect.Pitch));
    1.74 +    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
    1.75 +    if (FAILED(result)) {
    1.76 +        D3D_SetError("LockRect()", result);
    1.77 +        IDirect3DSurface9_Release(surface);
    1.78 +        IDirect3DSurface9_Release(backBuffer);
    1.79 +        return -1;
    1.80 +    }
    1.81  
    1.82 -    b = pBytes[index];
    1.83 -    g = pBytes[index+1];
    1.84 -    r = pBytes[index+2];
    1.85 -    a = pBytes[index+3];
    1.86 -    // <--
    1.87 -    
    1.88 -    return -1;
    1.89 +    SDL_ConvertPixels(rect->w, rect->h,
    1.90 +                      display->current_mode.format, locked.pBits, locked.Pitch,
    1.91 +                      format, pixels, pitch);
    1.92 +
    1.93 +    IDirect3DSurface9_UnlockRect(surface);
    1.94 +
    1.95 +    IDirect3DSurface9_Release(surface);
    1.96 +    IDirect3DSurface9_Release(backBuffer);
    1.97 +
    1.98 +    return 0;
    1.99  }
   1.100  
   1.101  static int