Skip to content

Commit

Permalink
Fix non-power-of-two shaped windows.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jorgenpt committed Sep 2, 2014
1 parent 7242e81 commit 782d590
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/video/SDL_shape.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rec
SDL_Color key; SDL_Color key;
SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree)); SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
SDL_Rect next = {0,0,0,0}; SDL_Rect next = {0,0,0,0};

for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) { for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) { for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
pixel_value = 0; pixel_value = 0;
Expand Down Expand Up @@ -165,27 +166,37 @@ RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rec
if(last_opaque == -1) if(last_opaque == -1)
last_opaque = pixel_opaque; last_opaque = pixel_opaque;
if(last_opaque != pixel_opaque) { if(last_opaque != pixel_opaque) {
const int halfwidth = dimensions.w / 2;
const int halfheight = dimensions.h / 2;

result->kind = QuadShape; result->kind = QuadShape;
/* These will stay the same. */
next.w = dimensions.w / 2;
next.h = dimensions.h / 2;
/* These will change from recursion to recursion. */
next.x = dimensions.x; next.x = dimensions.x;
next.y = dimensions.y; next.y = dimensions.y;
next.w = halfwidth;
next.h = halfheight;
result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
next.x += next.w;
/* Unneeded: next.y = dimensions.y; */ next.x = dimensions.x + halfwidth;
next.w = dimensions.w - halfwidth;
result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);

next.x = dimensions.x; next.x = dimensions.x;
next.y += next.h; next.w = halfwidth;
next.y = dimensions.y + halfheight;
next.h = dimensions.h - halfheight;
result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
next.x += next.w;
/* Unneeded: next.y = dimensions.y + dimensions.h /2; */ next.x = dimensions.x + halfwidth;
next.w = dimensions.w - halfwidth;
result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);

return result; return result;
} }
} }
} }


/* If we never recursed, all the pixels in this quadrant have the same "value". */ /* If we never recursed, all the pixels in this quadrant have the same "value". */
result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape); result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
result->data.shape = dimensions; result->data.shape = dimensions;
Expand Down

0 comments on commit 782d590

Please sign in to comment.