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->dpy,
214 st->mycmap, &st->mycolors[1], &ncolors, True, True, 0, True);
215 if (ncolors < tailmax - 1)
218 for (c = 1; c < tailmax; c++)
219 st->mycolors[c].pixel = WhitePixel (st->dpy, DefaultScreen (st->dpy));
224 gridupdate (struct state *st, Bool interruptible)
228 for (x = 0; x <= xmax && !(wasakeypressed (st) && interruptible); x += st->boxw)
229 for (y = 0; y <= ymax; y += st->boxh)
231 if (random1 (15) < st->gridden)
233 #define lesser(A,B) ( ((A)<(B)) ? (A) : (B) )
234 int max = lesser (x + st->boxw, xmax);
236 for (xc = x; xc <= max; xc++)
239 if (random1 (15) < st->gridden)
241 int max = lesser (y + st->boxh, ymax);
243 for (yc = y; yc <= max; yc++)
250 bordupdate (struct state *st)
254 if (st->bordcorn == 0 || st->bordcorn == 1)
258 if (st->bordcorn == 0 || st->bordcorn == 3)
264 for (x = xmin; x <= xmax; x++)
265 sp (st, x, ybord, st->bordcol);
266 for (y = ymin; y <= ymax; y++)
267 sp (st, ybord, y, st->bordcol);
272 inbank (struct state *st, unsigned char thr)
276 for (c = 1; c <= st->bnkt; c++)
277 if (st->bank[c - 1] == thr)
283 pickbank (struct state *st)
285 unsigned char thr = 1;
290 while (inbank (st, thr))
291 thr = thr % st->threads + 1;
293 palupdate (st, False);
294 st->ch = readkey (st);
295 palupdate (st, False);
306 wraparound (thr, 1, st->threads + 1);
308 while (inbank (st, thr));
311 st->bank[++st->bnkt - 1] = thr;
313 case '1': case '2': case '3':
314 case '4': case '5': case '6':
315 case '7': case '8': case '9':
317 st->bank[++st->bnkt - 1] = st->ch - '0';
318 if (st->bank[st->bnkt - 1] > st->threads)
326 for (c = 1; c <= st->threads; c++)
328 tbank[++tbankt - 1] = c;
330 arrcpy (st->bank, tbank);
334 st->ch = readkey (st);
337 case '1': case '2': case '3':
338 case '4': case '5': case '6':
339 case '7': case '8': case '9':
343 for (c = 1; c <= st->threads; c++)
344 if (st->thread[c - 1].tmode == st->ch - '0')
345 st->bank[++st->bnkt - 1] = c;
351 for (st->bnkt = 1; st->bnkt <= st->threads; st->bnkt++)
352 st->bank[st->bnkt - 1] = st->bnkt;
353 st->bnkt = st->threads;
356 for (st->bnkt = 1; st->bnkt <= thrmax; st->bnkt++)
357 st->bank[st->bnkt - 1] = st->bnkt;
362 while (!(st->bnkt >= st->threads || st->ch == 'N' || st->ch == '\15' || st->ch == '#'));
363 if (st->bnkt == 0 && st->ch != 'N')
368 palupdate (st, False);
372 bankmod (char boolop, Bool * Bool_)
389 newonscreen (struct state *st, unsigned char thr)
391 linedata *LP = &st->thread[thr - 1];
394 LP->reclen = (LP->little) ?
395 random1 (10) + 5 : random1 (rlmax - 30) + 30;
396 LP->deg = random1 (degs);
397 LP->y = random1 (st->hei);
398 LP->x = random1 (st->wid);
401 LP->turnsize = random1 (4) + 2;
405 firstinit (struct state *st, unsigned char thr)
407 linedata *LP = &st->thread[thr - 1];
411 LP->slice = degs / 3;
414 LP->selfbounce = False;
415 LP->realbounce = False;
416 LP->vhfollow = False;
417 LP->tailfollow = False;
418 LP->killwalls = False;
420 LP->ctinc = random1 (2) * 2 - 1;
421 LP->circturn = ((thr % 2) * 2 - 1) * ((thr - 1) % 7 + 1);
430 LP->tclim = (unsigned char) (((real) degs) / 2 / 12);
434 maininit (struct state *st)
438 int n = random1 (sizeof (sampleStrings) / sizeof (sampleStrings[0]));
439 st->instring = sampleStrings[n].str;
440 st->speed = sampleStrings[n].speed;
458 for (thr = 1; thr <= thrmax; thr++)
461 newonscreen (st, thr);
466 for (d = degs - 1; d >= 0; d--)
468 st->sinof[d] = sin (d * dtor);
469 st->cosof[d] = cos (d * dtor);
471 st->tanof[d] = st->tanof[d + 1];
473 st->tanof[d] = tan (d * dtor);
480 move (struct state *st, unsigned char thr)
482 linedata *LP = &st->thread[thr - 1];
489 LP->deg += random1 (2 * LP->turnsize + 1) - LP->turnsize;
492 if (LP->slice == degs || LP->slice == degs2 || LP->slice == degs4)
494 if (LP->orichar == 'D')
496 if (LP->deg % degs4 != degs8)
497 LP->deg = degs4 * random1 (4) + degs8;
499 else if (LP->orichar == 'V')
500 if (LP->deg % degs4 != 0)
501 LP->deg = degs4 * random1 (4);
503 if (random1 (100) == 0)
506 LP->deg = LP->deg - degs4 + random1 (degs2);
508 LP->deg += (random1 (2) * 2 - 1) * LP->slice;
512 LP->deg += LP->circturn;
515 if (abs (LP->spiturn) > 11)
518 LP->deg += LP->spiturn;
519 if (random1 (15 - abs (LP->spiturn)) == 0)
521 LP->spiturn += LP->ctinc;
522 if (abs (LP->spiturn) > 10)
527 LP->turnco = abs (LP->turnco) - 1;
530 LP->turnco = st->curviness + random1 (10);
533 LP->deg += LP->circturn;
536 if (abs (LP->turnco) == 1)
537 LP->turnco *= -1 * (random1 (degs2 / abs (LP->circturn)) + 5);
538 else if (LP->turnco == 0)
540 else if (LP->turnco > 0)
543 LP->deg += LP->circturn;
550 if (LP->turnco > LP->tclim)
553 LP->tsc = (LP->tsc % LP->tslen) + 1;
555 LP->deg += LP->turnseq[LP->tsc - 1];
562 if (LP->tailfollow || LP->prey == thr)
564 dx = st->thread[LP->prey - 1].xrec[st->thread[LP->prey - 1].recpos] - LP->x;
565 dy = st->thread[LP->prey - 1].yrec[st->thread[LP->prey - 1].recpos] - LP->y;
569 dx = st->thread[LP->prey - 1].x - LP->x;
570 dy = st->thread[LP->prey - 1].y - LP->y;
574 ((fabs (dx) > fabs (dy)) ?
587 1 * degs8 : 7 * degs8) : ((dy > 0) ? 3 * degs8 : 5 * degs8));
588 if (desdeg - desdeg % degs4 != LP->deg - LP->deg % degs4
593 /* Using atan2 here doesn't seem to slow things down: */
594 desdeg = atan2 (dy, dx) / dtor;
595 wraparound (desdeg, 0, degs);
597 if (abs (desdeg - LP->deg) <= abs (LP->circturn))
602 ((desdeg - LP->deg > degs2) ?
603 -abs (LP->circturn) : abs (LP->circturn))
604 : ((LP->deg - desdeg > degs2) ?
605 abs (LP->circturn) : -abs (LP->circturn));
609 (st->tanof[LP->deg] >
610 dy / dx) ? -abs (LP->circturn) : abs (LP->circturn);
613 wraparound (LP->deg, 0, degs);
615 unsigned char oldcol;
616 real oldy = LP->y, oldx = LP->x;
617 LP->x += st->cosof[LP->deg];
618 wraparound (LP->x, xmin, xmax + 1);
619 LP->y += st->sinof[LP->deg];
620 wraparound (LP->y, ymin, ymax + 1);
621 #define xi ((int) LP->x)
622 #define yi ((int) LP->y)
624 oldcol = gp (st, xi, yi);
627 Bool vertwall = False, horiwall = False;
628 if (oldcol == 1 && ((LP->killwalls && st->gridden > 0) || LP->realbounce))
630 vertwall = (gp (st, xi, (int) oldy) == 1);
631 horiwall = (gp (st, (int) oldx, yi) == 1);
633 if (oldcol == 1 && LP->realbounce && (vertwall || horiwall))
636 LP->deg = -LP->deg + degs2;
642 if ((oldcol != LP->col && LP->realbounce)
643 || (oldcol == LP->col && LP->selfbounce))
644 LP->deg += degs4 * (random1 (2) * 2 - 1);
645 else if (oldcol != LP->col)
648 if (LP->killwalls && st->gridden > 0 && oldcol == 1)
650 if (vertwall && xi + 1 <= xmax)
653 for (yy = yi - yi % st->boxh;
654 yy <= yi - yi % st->boxh + st->boxh && yy <= ymax; yy++)
655 if (gp (st, xi + 1, yy) != 1 || yy == ymax)
658 if (horiwall && yi + 1 <= ymax)
661 for (xx = xi - xi % st->boxw;
662 xx <= xi - xi % st->boxw + st->boxw && xx <= xmax; xx++)
663 if (gp (st, xx, yi + 1) != 1 || xx == xmax)
667 if (oldcol != LP->col || LP->selfbounce)
672 wraparound (LP->deg, 0, degs);
676 sp (st, xi, yi, LP->col);
680 sp (st, LP->xrec[LP->recpos], LP->yrec[LP->recpos], 0);
682 sp (st, LP->xrec[LP->recpos], LP->yrec[LP->recpos], LP->col + thrmax);
684 LP->yrec[LP->recpos] = yi;
685 LP->xrec[LP->recpos] = xi;
686 if (LP->recpos == LP->reclen - 1)
688 if (LP->filled && !st->erasing)
695 wraparound (nextco, 0, LP->reclen);
696 if (LP->yrec[co] != LP->yrec[nextco]
697 || LP->xrec[co] != LP->xrec[nextco])
701 while (!(!LP->dead || co == LP->recpos));
704 wraparound (LP->recpos, 0, LP->reclen);
709 vermiculate_draw (Display *dpy, Window window, void *closure)
711 struct state *st = (struct state *) closure;
712 int had_instring = (st->instring != 0);
726 for (thr = 1; thr <= st->threads; thr++)
727 newonscreen (st, thr);
732 palupdate (st, False);
735 gridupdate (st, False);
741 for (thr = 1; thr <= st->threads; thr++)
744 if (alltrap) /* all threads are trapped */
746 if (st->speed != SPEEDMAX)
747 this_delay = waitabit (st);
750 if (tick++ > st->max_ticks && !had_instring)
759 if (this_delay == 0 && loop++ < 1000)
766 vermiculate_init (Display *d, Window w)
768 struct state *st = (struct state *) calloc (1, sizeof(*st));
772 st->instring = get_string_resource (st->dpy, "instring", "Instring");
773 if (st->instring && !*st->instring)
776 st->max_ticks = get_integer_resource (st->dpy, "ticks", "Integer");
778 int temp = get_integer_resource (st->dpy, "speed", "Speed");
783 st->mycolors[0].pixel = BlackPixel (st->dpy, DefaultScreen (st->dpy));
785 XSetWindowBackground (st->dpy, st->window,
786 BlackPixel (st->dpy, DefaultScreen (st->dpy)));
788 XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
789 st->wid = st->xgwa.width;
790 st->hei = st->xgwa.height;
791 st->mycmap = st->xgwa.colormap;
795 st->mygc = XCreateGC (st->dpy, st->window, 0, &mygcv);
798 st->point = (unsigned char *) calloc (1, st->wid * st->hei);
800 palupdate (st, True);
801 consume_instring(st);
807 vermiculate_reshape (Display *dpy, Window window, void *closure,
808 unsigned int w, unsigned int h)
813 vermiculate_event (Display *dpy, Window window, void *closure, XEvent *event)
819 consume_instring(struct state *st)
823 while (wasakeypressed (st))
825 st->ch = readkey (st);
829 st->ch = readkey (st);
835 unsigned char othreads = st->threads;
840 st->ch = readkey (st);
843 case '1': case '2': case '3':
844 case '4': case '5': case '6':
845 case '7': case '8': case '9':
846 st->thread[++st->threads - 1].tmode = st->ch - '0';
849 st->thread[++st->threads - 1].tmode =
850 random1 (tmodes - '0') + 1;
854 while (!(st->ch == '\15' || st->ch == '#'
855 || st->threads == thrmax));
856 if (st->threads == 0)
857 st->threads = othreads;
867 st->ch = readkey (st);
871 st->ch = readkey (st);
874 case '1': case '2': case '3':
875 case '4': case '5': case '6':
876 case '7': case '8': case '9':
877 /* Careful! The following macro needs to be at the beginning of any
878 block in which it's invoked, since it declares variables: */
879 #define forallinbank(LDP) linedata *LDP; int bankc; \
881 (LDP = &st->thread[st->bank[bankc - 1] - 1], \
882 bankc <= st->bnkt); bankc++)
884 forallinbank (L) L->slice = degs / (st->ch - '0');
889 forallinbank (L) L->slice = 0;
898 L->otslen = L->tslen;
905 st->ch = readkey (st);
912 case '1': case '2': case '3':
913 case '4': case '5': case '6':
914 case '7': case '8': case '9':
916 L->turnseq[L->tslen - 1] = st->ch - '0';
918 L->turnseq[L->tslen - 1] *= -1;
920 L->turnseq[L->tslen - 1] *= -1;
926 while (!(st->ch == '\15' || st->ch == '#'
927 || st->thread[st->bank[0] - 1].tslen == 50));
934 L->tslen = L->otslen;
935 for (c = 1; c <= L->tslen; c++)
936 seqSum += L->turnseq[c - 1];
941 (int) (((real) degs2) / abs (seqSum));
942 L->tsc = random1 (L->tslen) + 1;
948 st->ch = readkey (st);
954 case '1': case '2': case '3':
955 case '4': case '5': case '6':
956 case '7': case '8': case '9':
957 L->tmode = st->ch - '0';
960 L->tmode = random1 (tmodes - '0') + 1;
968 st->ch = readkey (st);
970 forallinbank (L) L->orichar = st->ch;
976 arrcpy (fbank, st->bank);
978 int fbnkt = st->bnkt;
981 for (bankc = 1; bankc <= fbnkt; bankc++)
983 linedata *L = &st->thread[fbank[bankc - 1] - 1];
987 L->prey = st->bank[0 + (bankc - 1) % st->bnkt];
994 forallinbank (L) L->prey = st->bank[bankc % st->bnkt];
998 st->ch = readkey (st);
1000 forallinbank (L) switch (st->ch)
1002 case '1': case '2': case '3':
1003 case '4': case '5': case '6':
1004 case '7': case '8': case '9':
1005 L->circturn = 10 - (st->ch - '0');
1008 L->circturn = random1 (7) + 1;
1023 st->ch = readkey (st);
1030 bankmod (boolop, &L->selfbounce);
1033 bankmod (boolop, &L->vhfollow);
1036 bankmod (boolop, &L->realbounce);
1039 bankmod (boolop, &L->little);
1043 bankmod (boolop, &L->tailfollow);
1046 bankmod (boolop, &L->killwalls);
1054 if (st->bordcol == 1)
1058 st->bordcorn = (st->bordcorn + 1) % 4;
1063 case '1': case '2': case '3':
1064 case '4': case '5': case '6':
1065 case '7': case '8': case '9':
1068 for (c = 1; c <= thrmax; c++)
1069 st->thread[c - 1].tmode = st->ch - '0';
1076 st->erasing = !st->erasing;
1080 palupdate (st, True);
1085 Bool gridchanged = True;
1086 if (st->gridden == 0)
1087 st->gridden = st->ogd;
1094 gridupdate (st, True);
1096 st->ch = readkey (st);
1107 if (st->gridden < 15)
1111 if (st->gridden > 0)
1115 st->ogd = st->gridden;
1119 st->boxw = st->boxh;
1126 gridchanged = False;
1128 if (dimch == 'W' || dimch == 'B')
1130 if (dimch == 'H' || dimch == 'B')
1137 while (!(st->ch == '\15' || st->ch == '#' || st->ch == 'O'));
1142 st->autopal = !st->autopal;
1145 st->bordcol = 1 - st->bordcol;
1149 st->speed -= SPEEDINC;
1154 st->speed += SPEEDINC;
1155 if (st->speed > SPEEDMAX)
1156 st->speed = SPEEDMAX;
1159 if (st->curviness > 5)
1163 if (st->curviness < 50)
1167 if (st->threads < thrmax)
1168 newonscreen (st, ++st->threads);
1171 if (st->threads > 1)
1173 linedata *L = &st->thread[st->threads - 1];
1174 int lastpos = (L->filled) ? L->reclen - 1 : L->recpos;
1176 for (c = 0; c <= lastpos; c++)
1177 sp (st, L->xrec[c], L->yrec[c], 0);
1186 vermiculate_free (Display *dpy, Window window, void *closure)
1188 struct state *st = (struct state *) closure;
1198 static const char *vermiculate_defaults[] = {
1199 ".background: Black",
1207 static XrmOptionDescRec vermiculate_options[] = {
1208 {"-speed", ".speed", XrmoptionSepArg, 0},
1209 {"-instring", ".instring", XrmoptionSepArg, 0},
1214 XSCREENSAVER_MODULE ("Vermiculate", vermiculate)