[SDL] Bad math in SDL_RenderCopy
authorSam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 16:23:16 +0000
changeset 3333 b334b4f7dfa0
parent 3332 da1cb54d74ef
child 3334 61ea9005fddf
[SDL] Bad math in SDL_RenderCopy Mason Wheeler to sdl When I tried to render an image using something other than NULL for srcrect, it got horribly distorted. I traced it down to the fact that the math in the rectangle adjustments performed just before the call to renderer->RenderCopy is written inside out. It should look like this: if (dstrect->w != real_dstrect.w) { int deltax = (dstrect->x - real_dstrect.x); int deltaw = (dstrect->w - real_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 = (dstrect->y - real_dstrect.y); int deltah = (dstrect->h - real_dstrect.h); real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h; real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
src/video/SDL_video.c
--- a/src/video/SDL_video.c	Fri Oct 02 13:50:36 2009 +0000
+++ b/src/video/SDL_video.c	Sat Oct 03 16:23:16 2009 +0000
@@ -2463,14 +2463,14 @@
         if (dstrect->w != real_dstrect.w) {
             int deltax = (real_dstrect.x - dstrect->x);
             int deltaw = (real_dstrect.w - dstrect->w);
-            real_srcrect.x += (deltax * dstrect->w) / real_srcrect.w;
-            real_srcrect.w += (deltaw * dstrect->w) / real_srcrect.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 * dstrect->h) / real_srcrect.h;
-            real_srcrect.h += (deltah * dstrect->h) / real_srcrect.h;
+            real_srcrect.y += (deltay * real_srcrect.h) / dstrect->h;
+            real_srcrect.h += (deltah * real_srcrect.h) / dstrect->h;
         }
     }