SDL_LockTextureToSurface: robustness of locked region compared to texture size
authorSylvain Becker <sylvain.becker@gmail.com>
Tue, 01 Oct 2019 09:26:30 +0200
changeset 131017c641e182926
parent 13100 2bef0dd99b8b
child 13102 f54a77124843
SDL_LockTextureToSurface: robustness of locked region compared to texture size
src/render/SDL_render.c
     1.1 --- a/src/render/SDL_render.c	Tue Oct 01 00:05:50 2019 +0300
     1.2 +++ b/src/render/SDL_render.c	Tue Oct 01 09:26:30 2019 +0200
     1.3 @@ -1684,7 +1684,7 @@
     1.4  SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
     1.5                           SDL_Surface **surface)
     1.6  {
     1.7 -    SDL_Rect r;
     1.8 +    SDL_Rect real_rect;
     1.9      void *pixels = NULL;
    1.10      int pitch, ret;
    1.11  
    1.12 @@ -1692,24 +1692,21 @@
    1.13          return -1;
    1.14      }
    1.15  
    1.16 -    if (rect == NULL) {
    1.17 -        r.x = 0;
    1.18 -        r.y = 0;
    1.19 -        r.w = texture->w;
    1.20 -        r.h = texture->h;
    1.21 -    } else {
    1.22 -        r.x = rect->x;
    1.23 -        r.y = rect->y;
    1.24 -        r.w = SDL_min(texture->w - rect->x, rect->w);
    1.25 -        r.h = SDL_min(texture->h - rect->y, rect->h);
    1.26 +    real_rect.x = 0;
    1.27 +    real_rect.y = 0;
    1.28 +    real_rect.w = texture->w;
    1.29 +    real_rect.h = texture->h;
    1.30 +
    1.31 +    if (rect) {
    1.32 +        SDL_IntersectRect(rect, &real_rect, &real_rect);
    1.33      }
    1.34  
    1.35 -    ret = SDL_LockTexture(texture, &r, &pixels, &pitch);
    1.36 +    ret = SDL_LockTexture(texture, &real_rect, &pixels, &pitch);
    1.37      if (ret < 0) {
    1.38          return ret;
    1.39      }
    1.40  
    1.41 -    texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, r.w, r.h, 0, pitch, texture->format);
    1.42 +    texture->locked_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, real_rect.w, real_rect.h, 0, pitch, texture->format);
    1.43      if (texture->locked_surface == NULL) {
    1.44          SDL_UnlockTexture(texture);
    1.45          return -1;