Add a hacky environment variable that lets me arrange a soak-test of a
[sgt/puzzles] / osx.m
diff --git a/osx.m b/osx.m
index 1794cf9..b59999d 100644 (file)
--- a/osx.m
+++ b/osx.m
@@ -80,6 +80,7 @@
 #define COMBINED /* we put all the puzzles in one binary in this port */
 
 #include <ctype.h>
+#include <time.h>
 #include <sys/time.h>
 #import <Cocoa/Cocoa.h>
 #include "puzzles.h"
@@ -1341,6 +1342,8 @@ static void osx_draw_line(void *handle, int x1, int y1, int x2, int y2, int colo
     [path moveToPoint:p1];
     [path lineToPoint:p2];
     [path stroke];
+    NSRectFill(NSMakeRect(x1, fe->h-y1-1, 1, 1));
+    NSRectFill(NSMakeRect(x2, fe->h-y2-1, 1, 1));
 }
 static void osx_draw_rect(void *handle, int x, int y, int w, int h, int colour)
 {
@@ -1421,11 +1424,42 @@ static void osx_blitter_free(void *handle, blitter *bl)
 static void osx_blitter_save(void *handle, blitter *bl, int x, int y)
 {
     frontend *fe = (frontend *)handle;
+    int sx, sy, sX, sY, dx, dy, dX, dY;
     [fe->image unlockFocus];
     [bl->img lockFocus];
-    [fe->image drawInRect:NSMakeRect(0, 0, bl->w, bl->h)
-       fromRect:NSMakeRect(x, fe->h - y - bl->h, bl->w, bl->h)
-       operation:NSCompositeCopy fraction:1.0];
+
+    /*
+     * Find the intersection of the source and destination rectangles,
+     * so as to avoid trying to copy from outside the source image,
+     * which GNUstep dislikes.
+     *
+     * Lower-case x,y coordinates are bottom left box corners;
+     * upper-case X,Y are the top right.
+     */
+    sx = x; sy = fe->h - y - bl->h;
+    sX = sx + bl->w; sY = sy + bl->h;
+    dx = dy = 0;
+    dX = bl->w; dY = bl->h;
+    if (sx < 0) {
+        dx += -sx;
+        sx = 0;
+    }
+    if (sy < 0) {
+        dy += -sy;
+        sy = 0;
+    }
+    if (sX > fe->w) {
+        dX -= (sX - fe->w);
+        sX = fe->w;
+    }
+    if (sY > fe->h) {
+        dY -= (sY - fe->h);
+        sY = fe->h;
+    }
+
+    [fe->image drawInRect:NSMakeRect(dx, dy, dX-dx, dY-dy)
+                 fromRect:NSMakeRect(sx, sy, sX-sx, sY-sy)
+                operation:NSCompositeCopy fraction:1.0];
     [bl->img unlockFocus];
     [fe->image lockFocus];
     bl->x = x;