3 * @(#) Copyright (C) 2001 Tyler Pierce (tyler@alumni.brown.edu)
4 * The full program, with documentation, is available at:
5 * http://freshmeat.net/projects/fdm
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation. No representations are made about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
22 #include "screenhack.h"
25 #define degs2 (degs/2)
26 #define degs4 (degs/4)
27 #define degs8 (degs/8)
28 #define dtor 0.0174532925 /* pi / degs2; */
30 #define tailmax (thrmax * 2 + 1)
32 #define ymax (st->hei - 1)
34 #define xmax (st->wid - 1)
39 #define wraparound(VAL,LOWER,UPPER) { \
41 VAL -= UPPER - LOWER; \
42 else if (VAL < LOWER) \
43 VAL += UPPER - LOWER; }
44 #define arrcpy(DEST,SRC) memcpy (DEST, SRC, sizeof(DEST))
47 typedef unsigned char banktype[thrmax];
49 typedef struct linedata
51 int deg, spiturn, turnco, turnsize;
57 int tmode, tsc, tslen, tclim, otslen, ctinc, reclen, recpos, circturn, prey,
59 int xrec[rlmax + 1], yrec[rlmax + 1];
61 Bool filled, killwalls, vhfollow,
62 selfbounce, tailfollow, realbounce, little;
66 static const struct stringAndSpeed
73 /* { "]]]]]]]]7ces123400-9-8#c123456#s9998880004#ma3#car9ma6#c-#r1", 600} ,
74 { "bmarrrr#c1234#lc5678#lyet]", 600} , */
75 { "AEBMN222222223#CAR9CAD4CAOV", 150} ,
76 { "mn333#c23#f1#]]]]]]]]]]]3bc9#r9#c78#f9#ma4#", 600} ,
77 { "AEBMN22222#CAD4CAORc1#f2#c1#r6", 100} ,
78 /* { "mn6666666#c1i#f1#y2#sy2#vyety1#ry13i#l", 40} , */
79 { "aebmnrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr#", 500} ,
80 /* { "bg+++++++++++++++++++++++#mnrrrrrrrrrrrrrrrrrrrrrrr#y1#k", 500}, */
81 /* { "BMN22222223#CAD4CAOVYAS", 150}, */
82 /* { "aebmnrrrrrrrrrrrrrrrr#yaryakg--#", 100} , */
83 { "mn6rrrrrrrrrrrrrrr#by1i#lcalc1#fnyav" , 200 } ,
84 { "mn1rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr#by1i#lcalc1#fn", 2000 },
85 { "baeMn333333333333333333333333#CerrYerCal", 800 },
86 { "baeMn1111111111111111111111111111111111111111111111111111111111#Cer9YesYevYerCal", 1200 },
87 { "baMn111111222222333333444444555555#Ct1#lCt2#lCt3#lCt4#lCt5#lCerrYerYet", 1400 },
88 { "baMn111111222222333333444444555555#Ct1#lCt2#lCt3#lCt4#lCt5#lCerrYerYetYt1i#lYt1i#sYt1#v", 1400 }
97 XWindowAttributes xgwa;
98 XColor mycolors[tailmax];
101 Bool erasing, cleared, autopal;
105 real sinof[degs], cosof[degs], tanof[degs];
106 unsigned char *point;
108 linedata thread[thrmax];
111 int boxw, boxh, curviness, gridden, ogd, bordcorn;
112 unsigned char bordcol, threads;
122 consume_instring(struct state *st);
125 wasakeypressed (struct state *st)
127 if (*st->instring != 0)
134 readkey (struct state *st)
137 if (*st->instring == 0)
139 readkey_result = '#';
143 readkey_result = *st->instring;
146 return toupper (readkey_result);
150 random1 (unsigned int i)
152 return (ya_random () % i);
156 waitabit (struct state *st)
159 st->cyc += st->threads;
160 while (st->cyc > st->speed)
163 st->cyc -= st->speed;
169 clearscreen (struct state *st)
171 XClearWindow (st->dpy, st->window);
172 memset (st->point, 0, st->wid * st->hei);
176 sp (struct state *st, int x, int y, int c)
178 XSetForeground (st->dpy, st->mygc, st->mycolors[c].pixel);
179 XDrawPoint (st->dpy, st->window, st->mygc, x, y);
180 st->point[(st->wid * y) + x] = c;
184 gp (struct state *st, int x, int y)
186 return st->point[(st->wid * y) + x];
190 redraw (struct state *st, int x, int y, int width, int height)
193 for (xc = x; xc <= x + width - 1; xc++)
194 for (yc = y; yc <= y + height - 1; yc++)
195 if (st->point[st->wid * yc + xc] != 0)
196 sp (st, xc, yc, st->point[st->wid * yc + xc]);
200 palupdate (struct state *st, Bool forceUpdate)
202 if (forceUpdate || *st->instring == 0)
204 redraw (st, xmin, ymin, st->wid, st->hei);
209 randpal (struct state *st)
211 int ncolors = tailmax - 1;
212 make_random_colormap (st->xgwa.screen, st->xgwa.visual, st->mycmap,
213 &st->mycolors[1], &ncolors, True, True, 0, True);
214 if (ncolors < tailmax - 1)
217 for (c = 1; c < tailmax; c++)
218 st->mycolors[c].pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
223 gridupdate (struct state *st, Bool interruptible)
227 for (x = 0; x <= xmax && !(wasakeypressed (st) && interruptible); x += st->boxw)
228 for (y = 0; y <= ymax; y += st->boxh)
230 if (random1 (15) < st->gridden)
232 #define lesser(A,B) ( ((A)<(B)) ? (A) : (B) )
233 int max = lesser (x + st->boxw, xmax);
235 for (xc = x; xc <= max; xc++)
238 if (random1 (15) < st->gridden)
240 int max = lesser (y + st->boxh, ymax);
242 for (yc = y; yc <= max; yc++)
249 bordupdate (struct state *st)
253 if (st->bordcorn == 0 || st->bordcorn == 1)
257 if (st->bordcorn == 0 || st->bordcorn == 3)
263 for (x = xmin; x <= xmax; x++)
264 sp (st, x, ybord, st->bordcol);
265 for (y = ymin; y <= ymax; y++)
266 sp (st, xbord, y, st->bordcol);
271 inbank (struct state *st, unsigned char thr)
275 for (c = 1; c <= st->bnkt; c++)
276 if (st->bank[c - 1] == thr)
282 pickbank (struct state *st)
284 unsigned char thr = 1;
289 while (inbank (st, thr))
290 thr = thr % st->threads + 1;
292 palupdate (st, False);
293 st->ch = readkey (st);
294 palupdate (st, False);
305 wraparound (thr, 1, st->threads + 1);
307 while (inbank (st, thr));
310 st->bank[++st->bnkt - 1] = thr;
312 case '1': case '2': case '3':
313 case '4': case '5': case '6':
314 case '7': case '8': case '9':
316 st->bank[++st->bnkt - 1] = st->ch - '0';
317 if (st->bank[st->bnkt - 1] > st->threads)
325 for (c = 1; c <= st->threads; c++)
327 tbank[++tbankt - 1] = c;
329 arrcpy (st->bank, tbank);
333 st->ch = readkey (st);
336 case '1': case '2': case '3':
337 case '4': case '5': case '6':
338 case '7': case '8': case '9':
342 for (c = 1; c <= st->threads; c++)
343 if (st->thread[c - 1].tmode == st->ch - '0')
344 st->bank[++st->bnkt - 1] = c;
350 for (st->bnkt = 1; st->bnkt <= st->threads; st->bnkt++)
351 st->bank[st->bnkt - 1] = st->bnkt;
352 st->bnkt = st->threads;
355 for (st->bnkt = 1; st->bnkt <= thrmax; st->bnkt++)
356 st->bank[st->bnkt - 1] = st->bnkt;
361 while (!(st->bnkt >= st->threads || st->ch == 'N' || st->ch == '\15' || st->ch == '#'));
362 if (st->bnkt == 0 && st->ch != 'N')
367 palupdate (st, False);
371 bankmod (char boolop, Bool * Bool_)
388 newonscreen (struct state *st, unsigned char thr)
390 linedata *LP = &st->thread[thr - 1];
393 LP->reclen = (LP->little) ?
394 random1 (10) + 5 : random1 (rlmax - 30) + 30;
395 LP->deg = random1 (degs);
396 LP->y = random1 (st->hei);
397 LP->x = random1 (st->wid);
400 LP->turnsize = random1 (4) + 2;
404 firstinit (struct state *st, unsigned char thr)
406 linedata *LP = &st->thread[thr - 1];
410 LP->slice = degs / 3;
413 LP->selfbounce = False;
414 LP->realbounce = False;
415 LP->vhfollow = False;
416 LP->tailfollow = False;
417 LP->killwalls = False;
419 LP->ctinc = random1 (2) * 2 - 1;
420 LP->circturn = ((thr % 2) * 2 - 1) * ((thr - 1) % 7 + 1);
429 LP->tclim = (unsigned char) (((real) degs) / 2 / 12);
433 maininit (struct state *st)
437 int n = random1 (sizeof (sampleStrings) / sizeof (sampleStrings[0]));
438 st->instring = sampleStrings[n].str;
439 st->speed = sampleStrings[n].speed;
457 for (thr = 1; thr <= thrmax; thr++)
460 newonscreen (st, thr);
465 for (d = degs - 1; d >= 0; d--)
467 st->sinof[d] = sin (d * dtor);
468 st->cosof[d] = cos (d * dtor);
470 st->tanof[d] = st->tanof[d + 1];
472 st->tanof[d] = tan (d * dtor);
479 move (struct state *st, unsigned char thr)
481 linedata *LP = &st->thread[thr - 1];
488 LP->deg += random1 (2 * LP->turnsize + 1) - LP->turnsize;
491 if (LP->slice == degs || LP->slice == degs2 || LP->slice == degs4)
493 if (LP->orichar == 'D')
495 if (LP->deg % degs4 != degs8)
496 LP->deg = degs4 * random1 (4) + degs8;
498 else if (LP->orichar == 'V')
499 if (LP->deg % degs4 != 0)
500 LP->deg = degs4 * random1 (4);
502 if (random1 (100) == 0)
505 LP->deg = LP->deg - degs4 + random1 (degs2);
507 LP->deg += (random1 (2) * 2 - 1) * LP->slice;
511 LP->deg += LP->circturn;
514 if (abs (LP->spiturn) > 11)
517 LP->deg += LP->spiturn;
518 if (random1 (15 - abs (LP->spiturn)) == 0)
520 LP->spiturn += LP->ctinc;
521 if (abs (LP->spiturn) > 10)
526 LP->turnco = abs (LP->turnco) - 1;
529 LP->turnco = st->curviness + random1 (10);
532 LP->deg += LP->circturn;
535 if (abs (LP->turnco) == 1)
536 LP->turnco *= -1 * (random1 (degs2 / abs (LP->circturn)) + 5);
537 else if (LP->turnco == 0)
539 else if (LP->turnco > 0)
542 LP->deg += LP->circturn;
549 if (LP->turnco > LP->tclim)
552 LP->tsc = (LP->tsc % LP->tslen) + 1;
554 LP->deg += LP->turnseq[LP->tsc - 1];
561 if (LP->tailfollow || LP->prey == thr)
563 dx = st->thread[LP->prey - 1].xrec[st->thread[LP->prey - 1].recpos] - LP->x;
564 dy = st->thread[LP->prey - 1].yrec[st->thread[LP->prey - 1].recpos] - LP->y;
568 dx = st->thread[LP->prey - 1].x - LP->x;
569 dy = st->thread[LP->prey - 1].y - LP->y;
573 ((fabs (dx) > fabs (dy)) ?
586 1 * degs8 : 7 * degs8) : ((dy > 0) ? 3 * degs8 : 5 * degs8));
587 if (desdeg - desdeg % degs4 != LP->deg - LP->deg % degs4
592 /* Using atan2 here doesn't seem to slow things down: */
593 desdeg = atan2 (dy, dx) / dtor;
594 wraparound (desdeg, 0, degs);
596 if (abs (desdeg - LP->deg) <= abs (LP->circturn))
601 ((desdeg - LP->deg > degs2) ?
602 -abs (LP->circturn) : abs (LP->circturn))
603 : ((LP->deg - desdeg > degs2) ?
604 abs (LP->circturn) : -abs (LP->circturn));
608 (st->tanof[LP->deg] >
609 dy / dx) ? -abs (LP->circturn) : abs (LP->circturn);
612 wraparound (LP->deg, 0, degs);
614 unsigned char oldcol;
615 real oldy = LP->y, oldx = LP->x;
616 LP->x += st->cosof[LP->deg];
617 wraparound (LP->x, xmin, xmax + 1);
618 LP->y += st->sinof[LP->deg];
619 wraparound (LP->y, ymin, ymax + 1);
620 #define xi ((int) LP->x)
621 #define yi ((int) LP->y)
623 oldcol = gp (st, xi, yi);
626 Bool vertwall = False, horiwall = False;
627 if (oldcol == 1 && ((LP->killwalls && st->gridden > 0) || LP->realbounce))
629 vertwall = (gp (st, xi, (int) oldy) == 1);
630 horiwall = (gp (st, (int) oldx, yi) == 1);
632 if (oldcol == 1 && LP->realbounce && (vertwall || horiwall))
635 LP->deg = -LP->deg + degs2;
641 if ((oldcol != LP->col && LP->realbounce)
642 || (oldcol == LP->col && LP->selfbounce))
643 LP->deg += degs4 * (random1 (2) * 2 - 1);
644 else if (oldcol != LP->col)
647 if (LP->killwalls && st->gridden > 0 && oldcol == 1)
649 if (vertwall && xi + 1 <= xmax)
652 for (yy = yi - yi % st->boxh;
653 yy <= yi - yi % st->boxh + st->boxh && yy <= ymax; yy++)
654 if (gp (st, xi + 1, yy) != 1 || yy == ymax)
657 if (horiwall && yi + 1 <= ymax)
660 for (xx = xi - xi % st->boxw;
661 xx <= xi - xi % st->boxw + st->boxw && xx <= xmax; xx++)
662 if (gp (st, xx, yi + 1) != 1 || xx == xmax)
666 if (oldcol != LP->col || LP->selfbounce)
671 wraparound (LP->deg, 0, degs);
675 sp (st, xi, yi, LP->col);
679 sp (st, LP->xrec[LP->recpos], LP->yrec[LP->recpos], 0);
681 sp (st, LP->xrec[LP->recpos], LP->yrec[LP->recpos], LP->col + thrmax);
683 LP->yrec[LP->recpos] = yi;
684 LP->xrec[LP->recpos] = xi;
685 if (LP->recpos == LP->reclen - 1)
687 if (LP->filled && !st->erasing)
694 wraparound (nextco, 0, LP->reclen);
695 if (LP->yrec[co] != LP->yrec[nextco]
696 || LP->xrec[co] != LP->xrec[nextco])
700 while (!(!LP->dead || co == LP->recpos));
703 wraparound (LP->recpos, 0, LP->reclen);
708 vermiculate_draw (Display *dpy, Window window, void *closure)
710 struct state *st = (struct state *) closure;
711 int had_instring = (st->instring != 0);
725 for (thr = 1; thr <= st->threads; thr++)
726 newonscreen (st, thr);
731 palupdate (st, False);
734 gridupdate (st, False);
740 for (thr = 1; thr <= st->threads; thr++)
743 if (alltrap) /* all threads are trapped */
745 if (st->speed != SPEEDMAX)
746 this_delay = waitabit (st);
749 if (tick++ > st->max_ticks && !had_instring)
758 if (this_delay == 0 && loop++ < 1000)
765 vermiculate_init (Display *d, Window w)
767 struct state *st = (struct state *) calloc (1, sizeof(*st));
771 st->instring = get_string_resource (st->dpy, "instring", "Instring");
772 if (st->instring && !*st->instring)
775 st->max_ticks = get_integer_resource (st->dpy, "ticks", "Integer");
777 int temp = get_integer_resource (st->dpy, "speed", "Speed");
782 st->mycolors[0].pixel = BlackPixel (st->dpy, DefaultScreen (st->dpy));
784 XSetWindowBackground (st->dpy, st->window,
785 BlackPixel (st->dpy, DefaultScreen (st->dpy)));
787 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
788 st->wid = st->xgwa.width;
789 st->hei = st->xgwa.height;
790 st->mycmap = st->xgwa.colormap;
794 st->mygc = XCreateGC (st->dpy, st->window, 0, &mygcv);
797 st->point = (unsigned char *) calloc (1, st->wid * st->hei);
799 palupdate (st, True);
800 consume_instring(st);
806 vermiculate_reshape (Display *dpy, Window window, void *closure,
807 unsigned int w, unsigned int h)
809 struct state *st = (struct state *) closure;
813 st->point = (unsigned char *) calloc (1, st->wid * st->hei);
817 vermiculate_event (Display *dpy, Window window, void *closure, XEvent *event)
823 consume_instring(struct state *st)
827 while (wasakeypressed (st))
829 st->ch = readkey (st);
833 st->ch = readkey (st);
839 unsigned char othreads = st->threads;
844 st->ch = readkey (st);
847 case '1': case '2': case '3':
848 case '4': case '5': case '6':
849 case '7': case '8': case '9':
850 st->thread[++st->threads - 1].tmode = st->ch - '0';
853 st->thread[++st->threads - 1].tmode =
854 random1 (tmodes - '0') + 1;
858 while (!(st->ch == '\15' || st->ch == '#'
859 || st->threads == thrmax));
860 if (st->threads == 0)
861 st->threads = othreads;
871 st->ch = readkey (st);
875 st->ch = readkey (st);
878 case '1': case '2': case '3':
879 case '4': case '5': case '6':
880 case '7': case '8': case '9':
881 /* Careful! The following macro needs to be at the beginning of any
882 block in which it's invoked, since it declares variables: */
883 #define forallinbank(LDP) linedata *LDP; int bankc; \
885 (LDP = &st->thread[st->bank[bankc - 1] - 1], \
886 bankc <= st->bnkt); bankc++)
888 forallinbank (L) L->slice = degs / (st->ch - '0');
893 forallinbank (L) L->slice = 0;
902 L->otslen = L->tslen;
909 st->ch = readkey (st);
916 case '1': case '2': case '3':
917 case '4': case '5': case '6':
918 case '7': case '8': case '9':
920 L->turnseq[L->tslen - 1] = st->ch - '0';
922 L->turnseq[L->tslen - 1] *= -1;
924 L->turnseq[L->tslen - 1] *= -1;
930 while (!(st->ch == '\15' || st->ch == '#'
931 || st->thread[st->bank[0] - 1].tslen == 50));
938 L->tslen = L->otslen;
939 for (c = 1; c <= L->tslen; c++)
940 seqSum += L->turnseq[c - 1];
945 (int) (((real) degs2) / abs (seqSum));
946 L->tsc = random1 (L->tslen) + 1;
952 st->ch = readkey (st);
958 case '1': case '2': case '3':
959 case '4': case '5': case '6':
960 case '7': case '8': case '9':
961 L->tmode = st->ch - '0';
964 L->tmode = random1 (tmodes - '0') + 1;
972 st->ch = readkey (st);
974 forallinbank (L) L->orichar = st->ch;
980 arrcpy (fbank, st->bank);
982 int fbnkt = st->bnkt;
985 for (bankc = 1; bankc <= fbnkt; bankc++)
987 linedata *L = &st->thread[fbank[bankc - 1] - 1];
991 L->prey = st->bank[0 + (bankc - 1) % st->bnkt];
998 forallinbank (L) L->prey = st->bank[bankc % st->bnkt];
1002 st->ch = readkey (st);
1004 forallinbank (L) switch (st->ch)
1006 case '1': case '2': case '3':
1007 case '4': case '5': case '6':
1008 case '7': case '8': case '9':
1009 L->circturn = 10 - (st->ch - '0');
1012 L->circturn = random1 (7) + 1;
1027 st->ch = readkey (st);
1034 bankmod (boolop, &L->selfbounce);
1037 bankmod (boolop, &L->vhfollow);
1040 bankmod (boolop, &L->realbounce);
1043 bankmod (boolop, &L->little);
1047 bankmod (boolop, &L->tailfollow);
1050 bankmod (boolop, &L->killwalls);
1058 if (st->bordcol == 1)
1062 st->bordcorn = (st->bordcorn + 1) % 4;
1067 case '1': case '2': case '3':
1068 case '4': case '5': case '6':
1069 case '7': case '8': case '9':
1072 for (c = 1; c <= thrmax; c++)
1073 st->thread[c - 1].tmode = st->ch - '0';
1080 st->erasing = !st->erasing;
1084 palupdate (st, True);
1089 Bool gridchanged = True;
1090 if (st->gridden == 0)
1091 st->gridden = st->ogd;
1098 gridupdate (st, True);
1100 st->ch = readkey (st);
1111 if (st->gridden < 15)
1115 if (st->gridden > 0)
1119 st->ogd = st->gridden;
1123 st->boxw = st->boxh;
1130 gridchanged = False;
1132 if (dimch == 'W' || dimch == 'B')
1134 if (dimch == 'H' || dimch == 'B')
1141 while (!(st->ch == '\15' || st->ch == '#' || st->ch == 'O'));
1146 st->autopal = !st->autopal;
1149 st->bordcol = 1 - st->bordcol;
1153 st->speed -= SPEEDINC;
1158 st->speed += SPEEDINC;
1159 if (st->speed > SPEEDMAX)
1160 st->speed = SPEEDMAX;
1163 if (st->curviness > 5)
1167 if (st->curviness < 50)
1171 if (st->threads < thrmax)
1172 newonscreen (st, ++st->threads);
1175 if (st->threads > 1)
1177 linedata *L = &st->thread[st->threads - 1];
1178 int lastpos = (L->filled) ? L->reclen - 1 : L->recpos;
1180 for (c = 0; c <= lastpos; c++)
1181 sp (st, L->xrec[c], L->yrec[c], 0);
1190 vermiculate_free (Display *dpy, Window window, void *closure)
1192 struct state *st = (struct state *) closure;
1202 static const char *vermiculate_defaults[] = {
1203 ".background: Black",
1209 "*ignoreRotation: True",
1214 static XrmOptionDescRec vermiculate_options[] = {
1215 {"-speed", ".speed", XrmoptionSepArg, 0},
1216 {"-instring", ".instring", XrmoptionSepArg, 0},
1221 XSCREENSAVER_MODULE ("Vermiculate", vermiculate)