From 90ed3044b2b1eaabeaa2c2a992f95db3bed35418 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 1 Aug 2013 09:15:36 -0700 Subject: [PATCH] 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 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c index 3cf467d64..5dfc38dcb 100644 --- a/src/render/SDL_render.c +++ b/src/render/SDL_render.c @@ -1549,22 +1549,10 @@ SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, real_dstrect.x = 0; real_dstrect.y = 0; if (dstrect) { - if (!SDL_IntersectRect(dstrect, &real_dstrect, &real_dstrect)) { + if (!SDL_HasIntersection(dstrect, &real_dstrect)) { return 0; } - /* Clip srcrect by the same amount as dstrect was clipped */ - if (dstrect->w != real_dstrect.w) { - int deltax = (real_dstrect.x - dstrect->x); - int deltaw = (real_dstrect.w - dstrect->w); - real_srcrect.x += (deltax * real_srcrect.w) / dstrect->w; - real_srcrect.w += (deltaw * real_srcrect.w) / dstrect->w; - } - if (dstrect->h != real_dstrect.h) { - int deltay = (real_dstrect.y - dstrect->y); - int deltah = (real_dstrect.h - dstrect->h); - real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h; - real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h; - } + real_dstrect = *dstrect; } if (texture->native) {