http://www.ibiblio.org/pub/historic-linux/ftp-archives/sunsite.unc.edu/Sep-29-1996...
[xscreensaver] / hacks / rorschach.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@netscape.com>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #include "screenhack.h"
13
14 static GC draw_gc, erase_gc;
15 static unsigned int default_fg_pixel;
16 static int iterations, offset;
17 static Bool xsym, ysym;
18
19 static void
20 init_rorschach (dpy, window)
21      Display *dpy;
22      Window window;
23 {
24   XGCValues gcv;
25   Colormap cmap;
26   XWindowAttributes xgwa;
27   XGetWindowAttributes (dpy, window, &xgwa);
28   cmap = xgwa.colormap;
29   gcv.foreground = default_fg_pixel =
30     get_pixel_resource ("foreground", "Foreground", dpy, cmap);
31   draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
32   gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
33   erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
34   iterations = get_integer_resource ("iterations", "Integer");
35   offset = get_integer_resource ("offset", "Integer");
36   if (offset <= 0) offset = 3;
37   if (iterations < 10) iterations = 10;
38   xsym = get_boolean_resource ("xsymmetry", "Symmetry");
39   ysym = get_boolean_resource ("ysymmetry", "Symmetry");
40 }
41
42 static void
43 hurm (dpy, window)
44      Display *dpy;
45      Window window;
46 {
47   Colormap cmap;
48   XWindowAttributes xgwa;
49   int xlim, ylim, x, y, i, got_color = 0;
50   XPoint points [4];
51   XColor color;
52   XClearWindow (dpy, window);
53   XGetWindowAttributes (dpy, window, &xgwa);
54   xlim = xgwa.width;
55   ylim = xgwa.height;
56   cmap = xgwa.colormap;
57
58   if (! mono_p)
59     hsv_to_rgb (random()%360, 1.0, 1.0, &color.red, &color.green, &color.blue);
60   if ((!mono_p) && (got_color = XAllocColor (dpy, cmap, &color)))
61     XSetForeground (dpy, draw_gc, color.pixel);
62   else
63     XSetForeground (dpy, draw_gc, default_fg_pixel);
64
65   x = xlim/2;
66   y = ylim/2;
67   for (i = 0; i < iterations; i++)
68     {
69       int j = 0;
70       x += ((random () % (1 + (offset << 1))) - offset);
71       y += ((random () % (1 + (offset << 1))) - offset);
72       points [j].x = x;
73       points [j].y = y;
74       j++;
75       if (xsym)
76         {
77           points [j].x = xlim - x;
78           points [j].y = y;
79           j++;
80         }
81       if (ysym)
82         {
83           points [j].x = x;
84           points [j].y = ylim - y;
85           j++;
86         }
87       if (xsym && ysym)
88         {
89           points [j].x = xlim - x;
90           points [j].y = ylim - y;
91           j++;
92         }
93       XDrawPoints (dpy, window, draw_gc, points, j, CoordModeOrigin);
94       XSync (dpy, True);
95     }
96   sleep (5);
97   for (i = 0; i < (ylim >> 1); i++)
98     {
99       int y = (random () % ylim);
100       XDrawLine (dpy, window, erase_gc, 0, y, xlim, y);
101       XFlush (dpy);
102       if ((i % 50) == 0)
103         usleep (10000);
104     }
105   XClearWindow (dpy, window);
106   if (got_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
107   XSync (dpy, True);
108   sleep (1);
109 }
110
111 \f
112 char *progclass = "Rorschach";
113
114 char *defaults [] = {
115   "Rorschach.background:        black",         /* to placate SGI */
116   "Rorschach.foreground:        white",
117   "*xsymmetry:  true",
118   "*ysymmetry:  false",
119   "*iterations: 4000",
120   "*offset:     4",
121   0
122 };
123
124 XrmOptionDescRec options [] = {
125   { "-iterations",      ".iterations",  XrmoptionSepArg, 0 },
126   { "-offset",          ".offset",      XrmoptionSepArg, 0 },
127   { "-xsymmetry",       ".xsymmetry",   XrmoptionNoArg, "true" },
128   { "-ysymmetry",       ".ysymmetry",   XrmoptionNoArg, "true" }
129 };
130 int options_size = (sizeof (options) / sizeof (options[0]));
131
132 void
133 screenhack (dpy, window)
134      Display *dpy;
135      Window window;
136 {
137   init_rorschach (dpy, window);
138   while (1)
139     hurm (dpy, window);
140 }