2 * Substrate (dragorn@kismetwireless.net)
3 * Directly ported code from complexification.net Substrate art
4 * http://complexification.net/gallery/machines/substrate/applet_s/substrate_s.pde
8 * Albuquerque, New Mexico
13 * 1.1 dragorn Jan 04 2005 Fixed some indenting, typo in errors for parsing
15 * 1.1 dagraz Jan 04 2005 Added option for circular cracks (David Agraz)
16 * Cleaned up issues with timeouts in start_crack (DA)
17 * 1.0 dragorn Oct 10 2004 First port done
19 * Directly based the hacks of:
21 * xscreensaver, Copyright (c) 1997, 1998, 2002 Jamie Zawinski <jwz@jwz.org>
23 * Permission to use, copy, modify, distribute, and sell this software and its
24 * documentation for any purpose is hereby granted without fee, provided that
25 * the above copyright notice appear in all copies and that both that
26 * copyright notice and this permission notice appear in supporting
27 * documentation. No representations are made about the suitability of this
28 * software for any purpose. It is provided "as is" without express or
33 #include "screenhack.h"
35 /* this program goes faster if some functions are inline. The following is
36 * borrowed from ifs.c */
37 #if !defined( __GNUC__ ) && !defined(__cplusplus) && !defined(c_plusplus)
44 /* Raw colormap extracted from pollockEFF.gif */
45 static const char *rgb_colormap[] = {
46 "#201F21", "#262C2E", "#352626", "#372B27",
47 "#302C2E", "#392B2D", "#323229", "#3F3229",
48 "#38322E", "#2E333D", "#333A3D", "#473329",
49 "#40392C", "#40392E", "#47402C", "#47402E",
50 "#4E402C", "#4F402E", "#4E4738", "#584037",
51 "#65472D", "#6D5D3D", "#745530", "#755532",
52 "#745D32", "#746433", "#7C6C36", "#523152",
53 "#444842", "#4C5647", "#655D45", "#6D5D44",
54 "#6C5D4E", "#746C43", "#7C6C42", "#7C6C4B",
55 "#6B734B", "#73734B", "#7B7B4A", "#6B6C55",
56 "#696D5E", "#7B6C5D", "#6B7353", "#6A745D",
57 "#727B52", "#7B7B52", "#57746E", "#687466",
58 "#9C542B", "#9D5432", "#9D5B35", "#936B36",
59 "#AA7330", "#C45A27", "#D95223", "#D85A20",
60 "#DB5A23", "#E57037", "#836C4B", "#8C6B4B",
61 "#82735C", "#937352", "#817B63", "#817B6D",
62 "#927B63", "#D9893B", "#E49832", "#DFA133",
63 "#E5A037", "#F0AB3B", "#8A8A59", "#B29A58",
64 "#89826B", "#9A8262", "#888B7C", "#909A7A",
65 "#A28262", "#A18A69", "#A99968", "#99A160",
66 "#99A168", "#CA8148", "#EB8D43", "#C29160",
67 "#C29168", "#D1A977", "#C9B97F", "#F0E27B",
68 "#9F928B", "#C0B999", "#E6B88F", "#C8C187",
69 "#E0C886", "#F2CC85", "#F5DA83", "#ECDE9D",
70 "#F5D294", "#F5DA94", "#F4E784", "#F4E18A",
71 "#F4E193", "#E7D8A7", "#F1D4A5", "#F1DCA5",
72 "#F4DBAD", "#F1DCAE", "#F4DBB5", "#F5DBBD",
73 "#F4E2AD", "#F5E9AD", "#F4E3BE", "#F5EABE",
74 "#F7F0B6", "#D9D1C1", "#E0D0C0", "#E7D8C0",
75 "#F1DDC6", "#E8E1C0", "#F3EDC7", "#F6ECCE",
76 "#F8F2C7", "#EFEFD0", 0
80 /* Synthesis of data from Crack:: and SandPainter:: */
83 float ys, xs, t_inc; /* for curvature calculations */
87 unsigned long sandcolor;
100 unsigned int initial_cracks;
103 unsigned int max_num;
105 int grains; /* number of grains in the sand painting */
109 crack *cracks; /* grid of cracks */
110 int *cgrid; /* grid of actual crack placement */
112 /* Raw map of pixels we need to keep for alpha blending */
113 unsigned long int *off_img;
117 unsigned long *parsedcolors;
118 unsigned long fgcolor;
119 unsigned long bgcolor;
124 unsigned int wireframe;
125 unsigned int seamless;
133 unsigned int max_cycles;
136 XWindowAttributes xgwa;
142 *xrealloc(void *p, size_t size)
145 if ((ret = realloc(p, size)) == NULL) {
146 fprintf(stderr, "%s: out of memory\n", progname);
155 struct field *f = xrealloc(NULL, sizeof(struct field));
158 f->initial_cracks = 0;
165 f->parsedcolors = NULL;
173 f->circle_percent = 0;
177 /* Quick references to pixels in the offscreen map and in the crack grid */
178 #define ref_pixel(f, x, y) ((f)->off_img[(y) * (f)->width + (x)])
179 #define ref_cgrid(f, x, y) ((f)->cgrid[(y) * (f)->width + (x)])
181 static inline void start_crack(struct field *f, crack *cr)
183 /* synthesis of Crack::findStart() and crack::startCrack() */
190 /* shift until crack is found */
191 while ((!found) && (timeout++ < 10000)) {
192 px = (int) (random() % f->width);
193 py = (int) (random() % f->height);
195 if (ref_cgrid(f, px, py) < 10000)
200 /* We timed out. Use our default values */
204 /* Sanity check needed */
206 if (px >= f->width) px = f->width - 1;
208 if (py >= f->height) py = f->height - 1;
210 ref_cgrid(f, px, py) = cr->t;
214 a = ref_cgrid(f, px, py);
216 if ((random() % 100) < 50) {
217 /* conversion of the java int(random(-2, 2.1)) */
218 a -= 90 + (frand(4.1) - 2.0);
220 a += 90 + (frand(4.1) - 2.0);
223 if ((random() % 100) < f->circle_percent) {
224 float r; /* radius */
228 cr->degrees_drawn = 0;
230 r = 10 + (random() % ((f->width + f->height) / 2));
232 if ((random() % 100) < 50) {
236 /* arc length = r * theta => theta = arc length / r */
237 radian_inc = STEP / r;
238 cr->t_inc = radian_inc * 360 / 2 / M_PI;
240 cr->ys = r * sin(radian_inc);
241 cr->xs = r * ( 1 - cos(radian_inc));
248 /* Condensed from Crack::startCrack */
249 cr->x = px + ((float) 0.61 * cos(a * M_PI / 180));
250 cr->y = py + ((float) 0.61 * sin(a * M_PI / 180));
255 static inline void make_crack(struct field *f)
259 if (f->num < f->max_num) {
260 /* make a new crack */
261 f->cracks = (crack *) xrealloc(f->cracks, sizeof(crack) * (f->num + 1));
263 cr = &(f->cracks[f->num]);
266 cr->sandg = (frand(0.2) - 0.01);
267 cr->sandcolor = f->parsedcolors[random() % f->numcolors];
268 cr->crack_num = f->num;
270 cr->degrees_drawn = 0;
272 /* We could use these values in the timeout case of start_crack */
274 cr->x = random() % f->width;
275 cr->y = random() % f->height;
276 cr->t = random() % 360;
285 static inline void point2rgb(int depth, unsigned long c, int *r, int *g, int *b)
291 /* This program idiotically does not go through a color map, so
292 we have to hardcode in knowledge of how jwxyz.a packs pixels!
293 Fix it to go through st->colors[st->ncolors] instead!
295 *r = (c & 0x00ff0000) >> 16;
296 *g = (c & 0x0000ffff) >> 8;
297 *b = (c & 0x000000ff);
299 *g = (c & 0xff00) >> 8;
300 *r = (c & 0xff0000) >> 16;
305 *g = ((c >> 5) & 0x3f) << 2;
306 *r = ((c >> 11) & 0x1f) << 3;
307 *b = (c & 0x1f) << 3;
310 *g = ((c >> 5) & 0x1f) << 3;
311 *r = ((c >> 10) & 0x1f) << 3;
312 *b = (c & 0x1f) << 3;
317 static inline unsigned long rgb2point(int depth, int r, int g, int b)
319 unsigned long ret = 0;
325 /* This program idiotically does not go through a color map, so
326 we have to hardcode in knowledge of how jwxyz.a packs pixels!
327 Fix it to go through st->colors[st->ncolors] instead!
329 ret = 0xFF000000 | (r << 16) | (g << 8) | b;
331 ret |= (r << 16) | (g << 8) | b;
335 ret = ((r>>3) << 11) | ((g>>2)<<5) | (b>>3);
338 ret = ((r>>3) << 10) | ((g>>3)<<5) | (b>>3);
345 /* alpha blended point drawing -- this is Not Right and will likely fail on
346 * non-intel platforms as it is now, needs fixing */
347 static inline unsigned long
348 trans_point(struct state *st,
349 int x1, int y1, unsigned long myc, float a,
352 if ((x1 >= 0) && (x1 < f->width) && (y1 >= 0) && (y1 < f->height)) {
354 ref_pixel(f, x1, y1) = myc;
356 int or = 0, og = 0, ob = 0;
357 int r = 0, g = 0, b = 0;
361 c = ref_pixel(f, x1, y1);
363 point2rgb(f->visdepth, c, &or, &og, &ob);
364 point2rgb(f->visdepth, myc, &r, &g, &b);
366 nr = or + (r - or) * a;
367 ng = og + (g - og) * a;
368 nb = ob + (b - ob) * a;
370 c = rgb2point(f->visdepth, nr, ng, nb);
372 ref_pixel(f, x1, y1) = c;
382 region_color(struct state *st, GC fgc, struct field *f, crack *cr)
384 /* synthesis of Crack::regionColor() and SandPainter::render() */
397 /* move perpendicular to crack */
398 rx += (0.81 * sin(cr->t * M_PI/180));
399 ry -= (0.81 * cos(cr->t * M_PI/180));
408 if ((cx >= 0) && (cx < f->width) && (cy >= 0) && (cy < f->height)) {
410 if (f->cgrid[cy * f->width + cx] > 10000) {
420 /* SandPainter stuff here */
423 cr->sandg += (frand(0.1) - 0.050);
429 if (cr->sandg > maxg)
434 /* Lay down grains of sand */
435 w = cr->sandg / (grains - 1);
437 for (i = 0; i < grains; i++) {
438 drawx = (cr->x + (rx - cr->x) * sin(cr->sandp + sin((float) i * w)));
439 drawy = (cr->y + (ry - cr->y) * sin(cr->sandp + sin((float) i * w)));
441 drawx = fmod(drawx + f->width, f->width);
442 drawy = fmod(drawy + f->height, f->height);
446 c = trans_point(st, drawx, drawy, cr->sandcolor, (0.1 - i / (grains * 10.0)), f);
448 XSetForeground(st->dpy, fgc, c);
449 XDrawPoint(st->dpy, st->window, fgc, (int) drawx, (int) drawy);
450 XSetForeground(st->dpy, fgc, f->fgcolor);
454 static void build_substrate(struct field *f)
473 /* erase the crack grid */
474 f->cgrid = (int *) xrealloc(f->cgrid, sizeof(int) * f->height * f->width);
475 memset(f->cgrid, 10001, f->height * f->width * sizeof(int));
477 /* Not necessary now that make_crack ensures we have usable default
478 * values in start_crack's timeout case
479 * make random crack seeds *
480 for (tx = 0; tx < 16; tx++) {
481 ty = (int) (random() % (f->width * f->height - 1));
482 f->cgrid[ty] = (int) random() % 360;
486 /* make the initial cracks */
487 for (tx = 0; tx < f->initial_cracks; tx++)
493 movedrawcrack(struct state *st, GC fgc, struct field *f, int cracknum)
495 /* Basically Crack::move() */
498 crack *cr = &(f->cracks[cracknum]);
500 /* continue cracking */
502 cr->x += ((float) STEP * cos(cr->t * M_PI/180));
503 cr->y += ((float) STEP * sin(cr->t * M_PI/180));
506 cr->x += ((float) cr->ys * cos(cr->t * M_PI/180));
507 cr->y += ((float) cr->ys * sin(cr->t * M_PI/180));
509 cr->x += ((float) cr->xs * cos(cr->t * M_PI/180 - M_PI / 2));
510 cr->y += ((float) cr->xs * sin(cr->t * M_PI/180 - M_PI / 2));
513 cr->degrees_drawn += abs(cr->t_inc);
516 cr->x = fmod(cr->x + f->width, f->width);
517 cr->y = fmod(cr->y + f->height, f->height);
521 /* modification of random(-0.33,0.33) */
522 cx = (int) (cr->x + (frand(0.66) - 0.33));
523 cy = (int) (cr->y + (frand(0.66) - 0.33));
529 if ((cx >= 0) && (cx < f->width) && (cy >= 0) && (cy < f->height)) {
530 /* draw sand painter if we're not wireframe */
532 region_color(st, fgc, f, cr);
534 /* draw fgcolor crack */
535 ref_pixel(f, cx, cy) = f->fgcolor;
536 XDrawPoint(st->dpy, st->window, fgc, cx, cy);
538 if ( cr->curved && (cr->degrees_drawn > 360) ) {
539 /* completed the circle, stop cracking */
540 start_crack(f, cr); /* restart ourselves */
541 make_crack(f); /* generate a new crack */
544 else if ((f->cgrid[cy * f->width + cx] > 10000) ||
545 (abs(f->cgrid[cy * f->width + cx] - cr->t) < 5)) {
546 /* continue cracking */
547 f->cgrid[cy * f->width + cx] = (int) cr->t;
548 } else if (abs(f->cgrid[cy * f->width + cx] - cr->t) > 2) {
549 /* crack encountered (not self), stop cracking */
550 start_crack(f, cr); /* restart ourselves */
551 make_crack(f); /* generate a new crack */
554 /* out of bounds, stop cracking */
556 /* need these in case of timeout in start_crack */
557 cr->x = random() % f->width;
558 cr->y = random() % f->height;
559 cr->t = random() % 360;
561 start_crack(f, cr); /* restart ourselves */
562 make_crack(f); /* generate a new crack */
568 static void build_img(Display *dpy, Window window, XWindowAttributes xgwa, GC fgc,
576 f->off_img = (unsigned long *) xrealloc(f->off_img, sizeof(unsigned long) *
577 f->width * f->height);
579 memset(f->off_img, f->bgcolor, sizeof(unsigned long) * f->width * f->height);
584 substrate_init (Display *dpy, Window window)
586 struct state *st = (struct state *) calloc (1, sizeof(*st));
591 st->f = init_field();
593 st->growth_delay = (get_integer_resource(st->dpy, "growthDelay", "Integer"));
594 st->max_cycles = (get_integer_resource(st->dpy, "maxCycles", "Integer"));
595 st->f->initial_cracks = (get_integer_resource(st->dpy, "initialCracks", "Integer"));
596 st->f->max_num = (get_integer_resource(st->dpy, "maxCracks", "Integer"));
597 st->f->wireframe = (get_boolean_resource(st->dpy, "wireFrame", "Boolean"));
598 st->f->grains = (get_integer_resource(st->dpy, "sandGrains", "Integer"));
599 st->f->circle_percent = (get_integer_resource(st->dpy, "circlePercent", "Integer"));
600 st->f->seamless = (get_boolean_resource(st->dpy, "seamless", "Boolean"));
602 if (st->f->initial_cracks <= 2) {
603 fprintf(stderr, "%s: Initial cracks must be greater than 2\n", progname);
607 if (st->f->max_num <= 10) {
608 fprintf(stderr, "%s: Maximum number of cracks must be less than 10\n",
613 if (st->f->circle_percent < 0) {
614 fprintf(stderr, "%s: circle percent must be at least 0\n", progname);
618 if (st->f->circle_percent > 100) {
619 fprintf(stderr, "%s: circle percent must be less than 100\n", progname);
623 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
625 st->f->height = st->xgwa.height;
626 st->f->width = st->xgwa.width;
627 st->f->visdepth = st->xgwa.depth;
629 /* Count the colors in our map and assign them in a horrifically inefficient
630 * manner but it only happens once */
631 while (rgb_colormap[st->f->numcolors] != NULL) {
632 st->f->parsedcolors = (unsigned long *) xrealloc(st->f->parsedcolors,
633 sizeof(unsigned long) *
634 (st->f->numcolors + 1));
635 if (!XParseColor(st->dpy, st->xgwa.colormap, rgb_colormap[st->f->numcolors], &tmpcolor)) {
636 fprintf(stderr, "%s: couldn't parse color %s\n", progname,
637 rgb_colormap[st->f->numcolors]);
641 if (!XAllocColor(st->dpy, st->xgwa.colormap, &tmpcolor)) {
642 fprintf(stderr, "%s: couldn't allocate color %s\n", progname,
643 rgb_colormap[st->f->numcolors]);
647 st->f->parsedcolors[st->f->numcolors] = tmpcolor.pixel;
652 st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
653 "foreground", "Foreground");
654 st->gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
655 "background", "Background");
656 st->fgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv);
658 st->f->fgcolor = st->gcv.foreground;
659 st->f->bgcolor = st->gcv.background;
661 /* Initialize stuff */
662 build_img(st->dpy, st->window, st->xgwa, st->fgc, st->f);
663 build_substrate(st->f);
669 substrate_draw (Display *dpy, Window window, void *closure)
671 struct state *st = (struct state *) closure;
674 if ((st->f->cycles % 10) == 0) {
676 /* Restart if the window size changes */
677 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
679 if (st->f->height != st->xgwa.height || st->f->width != st->xgwa.width) {
680 st->f->height = st->xgwa.height;
681 st->f->width = st->xgwa.width;
682 st->f->visdepth = st->xgwa.depth;
684 build_substrate(st->f);
685 build_img(st->dpy, st->window, st->xgwa, st->fgc, st->f);
686 XSetForeground(st->dpy, st->fgc, st->gcv.background);
687 XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
688 XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
692 for (tempx = 0; tempx < st->f->num; tempx++) {
693 movedrawcrack(st, st->fgc, st->f, tempx);
698 if (st->f->cycles >= st->max_cycles && st->max_cycles != 0) {
699 build_substrate(st->f);
700 build_img(st->dpy, st->window, st->xgwa, st->fgc, st->f);
701 XSetForeground(st->dpy, st->fgc, st->gcv.background);
702 XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
703 XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
706 /* #### mi->recursion_depth = st->f->cycles; */
707 return st->growth_delay;
712 substrate_reshape (Display *dpy, Window window, void *closure,
713 unsigned int w, unsigned int h)
718 substrate_event (Display *dpy, Window window, void *closure, XEvent *event)
720 struct state *st = (struct state *) closure;
721 if (screenhack_event_helper (dpy, window, event))
723 st->f->cycles = st->max_cycles;
730 substrate_free (Display *dpy, Window window, void *closure)
732 struct state *st = (struct state *) closure;
736 static const char *substrate_defaults[] = {
737 ".background: white",
738 ".foreground: black",
743 "*growthDelay: 18000",
747 "*circlePercent: 33",
749 "*ignoreRotation: True",
754 static XrmOptionDescRec substrate_options[] = {
755 {"-background", ".background", XrmoptionSepArg, 0},
756 {"-foreground", ".foreground", XrmoptionSepArg, 0},
757 {"-wireframe", ".wireFrame", XrmoptionNoArg, "true"},
758 {"-seamless", ".seamless", XrmoptionNoArg, "true"},
759 {"-max-cycles", ".maxCycles", XrmoptionSepArg, 0},
760 {"-growth-delay", ".growthDelay", XrmoptionSepArg, 0},
761 {"-initial-cracks", ".initialCracks", XrmoptionSepArg, 0},
762 {"-max-cracks", ".maxCracks", XrmoptionSepArg, 0},
763 {"-sand-grains", ".sandGrains", XrmoptionSepArg, 0},
764 {"-circle-percent", ".circlePercent", XrmoptionSepArg, 0},
768 XSCREENSAVER_MODULE ("Substrate", substrate)