From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / substrate.c
index 82c2bacd483b58c68703919e4aa047624a7b138b..278a3bd6a4fb18151c52f0f466977b335604c1ba 100644 (file)
@@ -122,6 +122,7 @@ struct field {
     unsigned int cycles;
 
     unsigned int wireframe;
+    unsigned int seamless;
 };
 
 struct state {
@@ -164,6 +165,7 @@ static struct field
     f->parsedcolors = NULL;
     f->cycles = 0;
     f->wireframe = 0;
+    f->seamless = 0;
     f->fgcolor = 0;
     f->bgcolor = 0;
     f->visdepth = 0;
@@ -285,7 +287,7 @@ static inline void point2rgb(int depth, unsigned long c, int *r, int *g, int *b)
     switch(depth) {
         case 32:
         case 24:
-#ifdef HAVE_COCOA
+#ifdef HAVE_JWXYZ
             /* This program idiotically does not go through a color map, so
                we have to hardcode in knowledge of how jwxyz.a packs pixels!
                Fix it to go through st->colors[st->ncolors] instead!
@@ -318,9 +320,8 @@ static inline unsigned long rgb2point(int depth, int r, int g, int b)
 
     switch(depth) {
         case 32:
-            ret = 0xff000000;
         case 24:
-#ifdef HAVE_COCOA
+#ifdef HAVE_JWXYZ
             /* This program idiotically does not go through a color map, so
                we have to hardcode in knowledge of how jwxyz.a packs pixels!
                Fix it to go through st->colors[st->ncolors] instead!
@@ -399,6 +400,10 @@ region_color(struct state *st, GC fgc, struct field *f, crack *cr)
 
         cx = (int) rx;
         cy = (int) ry;
+        if (f->seamless) {
+            cx %= f->width;
+            cy %= f->height;
+        }
 
         if ((cx >= 0) && (cx < f->width) && (cy >= 0) && (cy < f->height)) {
             /* safe to check */
@@ -432,6 +437,10 @@ region_color(struct state *st, GC fgc, struct field *f, crack *cr)
     for (i = 0; i < grains; i++) {
         drawx = (cr->x + (rx - cr->x) * sin(cr->sandp + sin((float) i * w)));
         drawy = (cr->y + (ry - cr->y) * sin(cr->sandp + sin((float) i * w)));
+        if (f->seamless) {
+            drawx = fmod(drawx + f->width, f->width);
+            drawy = fmod(drawy + f->height, f->height);
+        }
 
         /* Draw sand bit */
         c = trans_point(st, drawx, drawy, cr->sandcolor, (0.1 - i / (grains * 10.0)), f);
@@ -463,7 +472,12 @@ static void build_substrate(struct field *f)
 
     /* erase the crack grid */
     f->cgrid = (int *) xrealloc(f->cgrid, sizeof(int) * f->height * f->width);
-    memset(f->cgrid, 10001, f->height * f->width * sizeof(int));
+    {
+        int j;
+        int *p = f->cgrid;
+        for (j = 0; j < f->height * f->width; j++)
+            *p++ = 10001;
+    }
 
     /* Not necessary now that make_crack ensures we have usable default
      *  values in start_crack's timeout case 
@@ -494,26 +508,28 @@ movedrawcrack(struct state *st, GC fgc, struct field *f, int cracknum)
         cr->y += ((float) STEP * sin(cr->t * M_PI/180));
     }
     else {
-        float oldx, oldy;
-
-        oldx = cr->x;
-        oldy = cr->y;
-
         cr->x += ((float) cr->ys * cos(cr->t * M_PI/180));
         cr->y += ((float) cr->ys * sin(cr->t * M_PI/180));
 
         cr->x += ((float) cr->xs * cos(cr->t * M_PI/180 - M_PI / 2));
-        cr->x += ((float) cr->xs * sin(cr->t * M_PI/180 - M_PI / 2));
+        cr->y += ((float) cr->xs * sin(cr->t * M_PI/180 - M_PI / 2));
 
         cr->t += cr->t_inc;
-        cr->degrees_drawn += abs(cr->t_inc);
+        cr->degrees_drawn += fabsf(cr->t_inc);
+    }
+    if (f->seamless) {
+        cr->x = fmod(cr->x + f->width, f->width);
+        cr->y = fmod(cr->y + f->height, f->height);
     }
 
     /* bounds check */
     /* modification of random(-0.33,0.33) */
     cx = (int) (cr->x + (frand(0.66) - 0.33));
     cy = (int) (cr->y + (frand(0.66) - 0.33));
-
+    if (f->seamless) {
+        cx %= f->width;
+        cy %= f->height;
+    }
 
     if ((cx >= 0) && (cx < f->width) && (cy >= 0) && (cy < f->height)) {
         /* draw sand painter if we're not wireframe */
@@ -531,10 +547,10 @@ movedrawcrack(struct state *st, GC fgc, struct field *f, int cracknum)
         }
         /* safe to check */
         else if ((f->cgrid[cy * f->width + cx] > 10000) ||
-                 (abs(f->cgrid[cy * f->width + cx] - cr->t) < 5)) {
+                 (fabsf(f->cgrid[cy * f->width + cx] - cr->t) < 5)) {
             /* continue cracking */
             f->cgrid[cy * f->width + cx] = (int) cr->t;
-        } else if (abs(f->cgrid[cy * f->width + cx] - cr->t) > 2) {
+        } else if (fabsf(f->cgrid[cy * f->width + cx] - cr->t) > 2) {
             /* crack encountered (not self), stop cracking */
             start_crack(f, cr); /* restart ourselves */
             make_crack(f); /* generate a new crack */
@@ -586,6 +602,7 @@ substrate_init (Display *dpy, Window window)
     st->f->wireframe = (get_boolean_resource(st->dpy, "wireFrame", "Boolean"));
     st->f->grains = (get_integer_resource(st->dpy, "sandGrains", "Integer"));
     st->f->circle_percent = (get_integer_resource(st->dpy, "circlePercent", "Integer"));
+    st->f->seamless = (get_boolean_resource(st->dpy, "seamless", "Boolean"));
 
     if (st->f->initial_cracks <= 2) {
         fprintf(stderr, "%s: Initial cracks must be greater than 2\n", progname);
@@ -691,6 +708,7 @@ substrate_draw (Display *dpy, Window window, void *closure)
     XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
   }
 
+  /* #### mi->recursion_depth = st->f->cycles; */
   return st->growth_delay;
 }
 
@@ -704,6 +722,12 @@ substrate_reshape (Display *dpy, Window window, void *closure,
 static Bool
 substrate_event (Display *dpy, Window window, void *closure, XEvent *event)
 {
+  struct state *st = (struct state *) closure;
+  if (screenhack_event_helper (dpy, window, event))
+    {
+      st->f->cycles = st->max_cycles;
+      return True;
+    }
   return False;
 }
 
@@ -719,12 +743,16 @@ static const char *substrate_defaults[] = {
     ".foreground: black",
     "*fpsSolid:        true",
     "*wireFrame: false",
+    "*seamless: false",
     "*maxCycles: 10000",
     "*growthDelay: 18000",
     "*initialCracks: 3",
     "*maxCracks: 100",
     "*sandGrains: 64",
     "*circlePercent: 33",
+#ifdef HAVE_MOBILE
+  "*ignoreRotation: True",
+#endif
     0
 };
 
@@ -732,6 +760,7 @@ static XrmOptionDescRec substrate_options[] = {
     {"-background", ".background", XrmoptionSepArg, 0},
     {"-foreground", ".foreground", XrmoptionSepArg, 0},
     {"-wireframe", ".wireFrame", XrmoptionNoArg, "true"},
+    {"-seamless", ".seamless", XrmoptionNoArg, "true"},
     {"-max-cycles", ".maxCycles", XrmoptionSepArg, 0},
     {"-growth-delay", ".growthDelay", XrmoptionSepArg, 0},
     {"-initial-cracks", ".initialCracks", XrmoptionSepArg, 0},