Fix non-power-of-two shaped windows.
authorJørgen Tjernø <jorgen@uberent.com>
Tue, 02 Sep 2014 14:14:23 -0700
changeset 9122cd6696d55781
parent 9121 00d99eff96a6
child 9123 bad450d761eb
Fix non-power-of-two shaped windows.

This fixes a bug where SDL_SetWindowShape would render artifacts if the image
was not a power of two image, due to rounding of width / 2.
src/video/SDL_shape.c
     1.1 --- a/src/video/SDL_shape.c	Tue Sep 02 06:55:47 2014 -0700
     1.2 +++ b/src/video/SDL_shape.c	Tue Sep 02 14:14:23 2014 -0700
     1.3 @@ -128,6 +128,7 @@
     1.4      SDL_Color key;
     1.5      SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
     1.6      SDL_Rect next = {0,0,0,0};
     1.7 +
     1.8      for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
     1.9          for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
    1.10              pixel_value = 0;
    1.11 @@ -165,27 +166,37 @@
    1.12              if(last_opaque == -1)
    1.13                  last_opaque = pixel_opaque;
    1.14              if(last_opaque != pixel_opaque) {
    1.15 +                const int halfwidth = dimensions.w / 2;
    1.16 +                const int halfheight = dimensions.h / 2;
    1.17 +
    1.18                  result->kind = QuadShape;
    1.19 -                /* These will stay the same. */
    1.20 -                next.w = dimensions.w / 2;
    1.21 -                next.h = dimensions.h / 2;
    1.22 -                /* These will change from recursion to recursion. */
    1.23 +
    1.24                  next.x = dimensions.x;
    1.25                  next.y = dimensions.y;
    1.26 +                next.w = halfwidth;
    1.27 +                next.h = halfheight;
    1.28                  result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
    1.29 -                next.x += next.w;
    1.30 -                /* Unneeded: next.y = dimensions.y; */
    1.31 +
    1.32 +                next.x = dimensions.x + halfwidth;
    1.33 +                next.w = dimensions.w - halfwidth;
    1.34                  result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
    1.35 +
    1.36                  next.x = dimensions.x;
    1.37 -                next.y += next.h;
    1.38 +                next.w = halfwidth;
    1.39 +                next.y = dimensions.y + halfheight;
    1.40 +                next.h = dimensions.h - halfheight;
    1.41                  result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
    1.42 -                next.x += next.w;
    1.43 -                /* Unneeded: next.y = dimensions.y + dimensions.h /2; */
    1.44 +
    1.45 +                next.x = dimensions.x + halfwidth;
    1.46 +                next.w = dimensions.w - halfwidth;
    1.47                  result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
    1.48 +
    1.49                  return result;
    1.50              }
    1.51          }
    1.52      }
    1.53 +
    1.54 +
    1.55      /* If we never recursed, all the pixels in this quadrant have the same "value". */
    1.56      result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
    1.57      result->data.shape = dimensions;