$NetBSD: patch-ad,v 1.1 2000/08/17 18:38:00 fredb Exp $

From joda@pdc.kth.se Thu Aug 17 12:18:46 2000
Date: 17 Aug 2000 19:02:53 +0200
From: Johan Danielsson <joda@pdc.kth.se>
To: fb@enteract.com
Cc: gnats-bugs@gnats.netbsd.org
Subject: Re: pkg/10852: "xworld" fails to start

Frederick Bruckman <fb@enteract.com> writes:

> 	XIO:  fatal IO error 14 (Bad address) on X server "1ton:0.0"
> 	      after 32 requests (29 known processed) with 0 events remaining.

This is because it only works as-is on 8-bit displays. Try this patch:

--- work/xworld-2.0/xworld.c	Thu Aug 17 19:00:48 2000
+++ xworld.c	Thu Aug 17 19:00:32 2000
@@ -92,6 +92,26 @@
     exit(1);
 }
 
+
+static char *
+alloc_image(XImage *image)
+{
+    char *Map;
+    int ix, iy, j;
+    Map = (char *)malloc(image->height * image->bytes_per_line);
+    if (Map == NULL) {
+	fprintf(stderr, "xworld: not enough memory\n");
+	exit(1);
+    }
+    image->data = Map; 
+    
+    for(j = 0, ix = 0; ix < image->width; ix++)
+	for (iy = 0; iy < image->height; iy++)
+	    XPutPixel(image, ix, iy, j++ % NCOLORS);
+    
+    return Map;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -314,22 +334,7 @@
 /*
  * generate startup image
  */
-    Map = (char *)malloc(size*size*sizeof(char));
-    if (Map == NULL) {
-        fprintf(stderr, "xworld: not enough memory\n");
-        exit(1);
-    }
-  
-    j = 0;
-    for (iy = 0; iy < size; iy++) {
-        i = iy % (NCOLORS + 1);
-        for (ix = 0; ix < size; ix++) {
-            *(Map + j++) = xcolor[i].pixel;
-            if (i++ >= NCOLORS) i = 0;
-        }
-    }
-   
-    image->data = Map; 
+    Map = alloc_image(image);
 
     gc = XCreateGC(display, win, 0, 0);
 
@@ -416,21 +421,21 @@
  * Map elevations to colors
  */
                     if (i != SOK) {
-                        *(Map + j) = xcolor[BLACK].pixel;
+			XPutPixel(image, ix, iy, xcolor[BLACK].pixel);
                         continue;
                     }
                     if (Value > level[NLEVELS - 1]) {
-                        *(Map + j) = xcolor[HIGH].pixel;
+			XPutPixel(image, ix, iy, xcolor[HIGH].pixel);
                         continue;
                     }
                     for (i = 0; i <= NLEVELS - 1; i++) {
                         if (Value <= level[i]) {
-                            *(Map + j) = xcolor[i].pixel;
+			    XPutPixel(image, ix, iy, xcolor[i].pixel);
                             break;
                         }
                     }
                 } else
-                    *(Map + j) = xcolor[BLACK].pixel;
+		    XPutPixel(image, ix, iy, xcolor[BLACK].pixel);
             }
         }
         XPutImage(display, win, gc, image, 0, 0, 0, 0, size, size);
@@ -462,21 +467,8 @@
                             fprintf(stderr, "xworld: can't create XImage\n");
                             exit(1);
                         }
-                        Map = (char *)malloc(size*size*sizeof(char));
-                        if (Map == NULL) {
-                            fprintf(stderr, "xworld: not enough memory\n");
-                            exit(1);
-                        }
-                        image->data = Map; 
+			Map = alloc_image(image);
  
-                        j = 0;
-                        for (iy = 0; iy < size; iy++) {
-                            i = iy % (NCOLORS + 1);
-                            for (ix = 0; ix < size; ix++) {
-                                *(Map + j++) = xcolor[i].pixel;
-                                if (i++ >= NCOLORS) i = 0;
-                            }
-                        }
                         XPutImage(display, win, gc, image, 0, 0, 0, 0,
                           size, size);
                         break;
