Fixed bug 1968 - SDL_RenderCopy stretch loses proportion on viewport boundaries for 3D renderers
authorSam Lantinga <slouken@libsdl.org>
Thu, 01 Aug 2013 09:15:36 -0700
changeset 7563c128ed448c30
parent 7562 5287c82340e3
child 7564 d2bc997ef5d6
Fixed bug 1968 - SDL_RenderCopy stretch loses proportion on viewport boundaries for 3D renderers

driedfruit

SDL_RenderCopy clips dstrect against the viewport. Then it adjusts the
srcrect by "appropriate" amount of pixels. This amount is actually
wrong, quite a lot, because of the rounding errors introduced in the "*
factor / factor" scale.

real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;

For example:

I have a 32 x 32 srcrect and a 64 x 64 dstrect. So far the
stretching is done perfectly, by a factor of 2.

Now, consider dstrect being clipped against the viewport, so it becomes
56 x 64. Now, the factor becomes 1.75 ! The adjustment to "srcrect"
can't handle this, cause srcrect is in integers.

And thus we now have incorrect mapping, with dstrect not being in the
right proportion to srcrect.

The problem is most evident when upscaling stuff, like displaying a 8x8
texture with a zoom of 64 or more, and moving it beyond the corners of
the screen. It *looks* really really bad.

Note: RenderCopyEX does no such clipping, and is right to do so. The fix would be to remove any such clipping from RenderCopy too. And then fix the software renderer, because it has the same fault, independently of RenderCopy.

[attached patch]
this leaves Software Renderer buggy, as it does it's own clipping later on
src/render/SDL_render.c
     1.1 --- a/src/render/SDL_render.c	Thu Aug 01 01:29:07 2013 -0700
     1.2 +++ b/src/render/SDL_render.c	Thu Aug 01 09:15:36 2013 -0700
     1.3 @@ -1549,22 +1549,10 @@
     1.4      real_dstrect.x = 0;
     1.5      real_dstrect.y = 0;
     1.6      if (dstrect) {
     1.7 -        if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) {
     1.8 +        if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
     1.9              return 0;
    1.10          }
    1.11 -        /* Clip srcrect by the same amount as dstrect was clipped */
    1.12 -        if (dstrect->w != real_dstrect.w) {
    1.13 -            int deltax = (real_dstrect.x - dstrect->x);
    1.14 -            int deltaw = (real_dstrect.w - dstrect->w);
    1.15 -            real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w;
    1.16 -            real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w;
    1.17 -        }
    1.18 -        if (dstrect->h != real_dstrect.h) {
    1.19 -            int deltay = (real_dstrect.y - dstrect->y);
    1.20 -            int deltah = (real_dstrect.h - dstrect->h);
    1.21 -            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
    1.22 -            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
    1.23 -        }
    1.24 +        real_dstrect = *dstrect;
    1.25      }
    1.26  
    1.27      if (texture->native) {