From 228063839a99fa896427b763e3eca4c5c434cb45 Mon Sep 17 00:00:00 2001
From: FunkyFr3sh <cc.red.alert.1@googlemail.com>
Date: Tue, 27 Sep 2022 22:45:13 +0200
Subject: [PATCH] #177 fix for blitter with negative coords

---
 src/ddsurface.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/ddsurface.c b/src/ddsurface.c
index 2d891a6..30aabae 100644
--- a/src/ddsurface.c
+++ b/src/ddsurface.c
@@ -91,10 +91,24 @@ HRESULT dds_Blt(
     }
 
     if (dst_rect.left < 0)
+    {
+        src_rect.left += abs(dst_rect.left);
+
+        if (src_rect.left > src_rect.right)
+            src_rect.left = src_rect.right;
+
         dst_rect.left = 0;
+    }
 
     if (dst_rect.top < 0)
+    {
+        src_rect.top += abs(dst_rect.top);
+
+        if (src_rect.top > src_rect.bottom)
+            src_rect.top = src_rect.bottom;
+
         dst_rect.top = 0;
+    }
 
     if (dst_rect.right > This->width)
         dst_rect.right = This->width;
@@ -213,14 +227,14 @@ HRESULT dds_Blt(
                     color_key.dwColorSpaceHighValue = color_key.dwColorSpaceLowValue;
             }
 
-            if (src_w == dst_w && src_h == dst_h && !mirror_left_right && !mirror_up_down)
+            if (!is_stretch_blt && !mirror_left_right && !mirror_up_down)
             {
                 blt_colorkey(
                     dst_buf,
                     dst_x,
                     dst_y,
-                    dst_w,
-                    dst_h,
+                    min(dst_w, src_w),
+                    min(dst_h, src_h),
                     This->l_pitch,
                     src_buf,
                     src_x,
@@ -252,7 +266,7 @@ HRESULT dds_Blt(
                     This->bpp);
             }
         }
-        else if (is_stretch_blt)
+        else if (is_stretch_blt && (src_w != dst_w || src_h != dst_h))
         {
             blt_stretch(
                 dst_buf,