Bad math in SDL_RenderCopy

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;

Fixed, thanks!On Sat, Oct 3, 2009 at 8:16 AM, Mason Wheeler wrote:

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;


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC