http://ftp.x.org/contrib/applications/xscreensaver-3.10.tar.gz
[xscreensaver] / hacks / slip.c
1 /* -*- Mode: C; tab-width: 4 -*-
2  * slip --- lots of blits.
3  */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)slip.c        4.00 97/01/01 xlockmore";
6 #endif
7
8 /* Copyright (c) 1992 by Scott Draves (spot@cs.cmu.edu)
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * Revision History:
23  * 12-May-97: jwz@jwz.org: turned into a standalone program.
24  * 01-Dec-95: Patched for VMS <joukj@alpha.chem.uva.nl>.
25  */
26
27 #ifdef STANDALONE
28 # define PROGCLASS                                      "Slip"
29 # define HACK_INIT                                      init_slip
30 # define HACK_DRAW                                      draw_slip
31 # define slip_opts                                      xlockmore_opts
32 # define DEFAULTS       "*count:                35    \n"                       \
33                                         "*cycles:               50    \n"                       \
34                                         "*delay:                50000 \n"                       \
35                                         "*ncolors:              200   \n"
36 # include "xlockmore.h"                         /* from the xscreensaver distribution */
37 #else  /* !STANDALONE */
38 # include "xlock.h"                                     /* from the xlockmore distribution */
39 #endif /* !STANDALONE */
40
41 ModeSpecOpt slip_opts = {
42   0, NULL, 0, NULL, NULL };
43
44 typedef struct {
45         int         width, height;
46         int         nblits_remaining;
47         int         blit_width, blit_height;
48         int         mode;
49         int         first_time;
50     int         backwards;
51 } slipstruct;
52 static slipstruct *slips = NULL;
53
54 static short
55 halfrandom(int mv)
56 {
57         static short lasthalf = 0;
58         unsigned long r;
59
60         if (lasthalf) {
61                 r = lasthalf;
62                 lasthalf = 0;
63         } else {
64                 r = LRAND();
65                 lasthalf = r >> 16;
66         }
67         return r % mv;
68 }
69
70 static int
71 erandom(int mv)
72 {
73         static int  stage = 0;
74         static unsigned long r;
75         int         res;
76
77         if (0 == stage) {
78                 r = LRAND();
79                 stage = 7;
80         }
81         res = r & 0xf;
82         r = r >> 4;
83         stage--;
84         if (res & 8)
85                 return res & mv;
86         else
87                 return -(res & mv);
88 }
89
90 static void
91 prepare_screen(ModeInfo * mi, slipstruct * s)
92 {
93
94         Display    *display = MI_DISPLAY(mi);
95         GC          gc = MI_GC(mi);
96         int         i, n, w = s->width / 20;
97         int         not_solid = halfrandom(10);
98
99 #ifdef STANDALONE                         /* jwz -- sometimes hack the desktop image! */
100         if (halfrandom(2) == 0)
101           {
102                 grab_screen_image(DefaultScreenOfDisplay (MI_DISPLAY(mi)),
103                                                   MI_WINDOW(mi));
104                 return;
105           }
106 #endif
107
108         s->backwards = LRAND() & 1;             /* jwz: go the other way sometimes */
109
110         if (s->first_time || (0 == halfrandom(10))) {
111                 XClearWindow(display, MI_WINDOW(mi));
112                 n = 300;
113         } else {
114                 if (halfrandom(5))
115                         return;
116                 if (halfrandom(5))
117                         n = 100;
118                 else
119                         n = 2000;
120         }
121
122         if (MI_NPIXELS(mi) > 2)
123                 XSetForeground(display, gc, MI_PIXEL(mi, halfrandom(MI_NPIXELS(mi))));
124         else if (halfrandom(2))
125                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
126         else
127                 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
128
129         for (i = 0; i < n; i++) {
130           int ww = ((w/2) + halfrandom(w));
131                 if (not_solid)
132                         if (MI_NPIXELS(mi) > 2)
133                                 XSetForeground(display, gc, MI_PIXEL(mi, halfrandom(MI_NPIXELS(mi))));
134                         else if (halfrandom(2))
135                                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
136                         else
137                                 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
138                 XFillRectangle(display, MI_WINDOW(mi), gc,
139                                halfrandom(s->width - ww),
140                                halfrandom(s->height - ww),
141                                ww, ww);
142         }
143         s->first_time = 0;
144 }
145
146 static int
147 quantize(double d)
148 {
149         int         i = (int) floor(d);
150         double      f = d - i;
151
152         if ((LRAND() & 0xff) < f * 0xff)
153                 i++;
154         return i;
155 }
156
157 void
158 init_slip(ModeInfo * mi)
159 {
160         slipstruct *sp;
161
162         if (slips == NULL) {
163                 if ((slips = (slipstruct *) calloc(MI_NUM_SCREENS(mi),
164                                                sizeof (slipstruct))) == NULL)
165                         return;
166         }
167         sp = &slips[MI_SCREEN(mi)];
168
169         sp->width = MI_WIN_WIDTH(mi);
170         sp->height = MI_WIN_HEIGHT(mi);
171
172         sp->blit_width = sp->width / 25;
173         sp->blit_height = sp->height / 25;
174         sp->nblits_remaining = 0;
175         sp->mode = 0;
176         sp->first_time = 1;
177
178         /* no "NoExpose" events from XCopyArea wanted */
179         XSetGraphicsExposures(MI_DISPLAY(mi), MI_GC(mi), False);
180 }
181
182 void
183 draw_slip(ModeInfo * mi)
184 {
185         Display    *display = MI_DISPLAY(mi);
186         Window      window = MI_WINDOW(mi);
187         GC          gc = MI_GC(mi);
188         slipstruct *s = &slips[MI_SCREEN(mi)];
189         int         timer;
190
191         timer = MI_BATCHCOUNT(mi) * MI_CYCLES(mi);
192
193         while (timer--) {
194                 int         xi = halfrandom(s->width - s->blit_width);
195                 int         yi = halfrandom(s->height - s->blit_height);
196                 double      x, y, dx = 0, dy = 0, t, s1, s2;
197
198                 if (0 == s->nblits_remaining--) {
199                         static int lut[] =
200                         {0, 0, 0, 1, 1, 1, 2};
201
202                         prepare_screen(mi, s);
203                         s->nblits_remaining = MI_BATCHCOUNT(mi) *
204                                 (2000 + halfrandom(1000) + halfrandom(1000));
205                         if (s->mode == 2)
206                                 s->mode = halfrandom(2);
207                         else
208                                 s->mode = lut[halfrandom(7)];
209                 }
210                 x = (2 * xi + s->blit_width) / (double) s->width - 1;
211                 y = (2 * yi + s->blit_height) / (double) s->height - 1;
212
213                 /* (x,y) is in biunit square */
214                 switch (s->mode) {
215                         case 0:                                                         /* rotor */
216                                 dx = x;
217                                 dy = y;
218
219                                 if (dy < 0) {
220                                         dy += 0.04;
221                                         if (dy > 0)
222                                                 dy = 0.00;
223                                 }
224                                 if (dy > 0) {
225                                         dy -= 0.04;
226                                         if (dy < 0)
227                                                 dy = 0.00;
228                                 }
229                                 t = dx * dx + dy * dy + 1e-10;
230                                 s1 = 2 * dx * dx / t - 1;
231                                 s2 = 2 * dx * dy / t;
232                                 dx = s1 * 5;
233                                 dy = s2 * 5;
234                                 if (s->backwards) {     /* jwz: go the other way sometimes */
235                                         dx = -dx;
236                                         dy = -dy;
237                                 }
238                                 break;
239                         case 1:                                                         /* shuffle */
240                                 dx = erandom(3);
241                                 dy = erandom(3);
242                                 break;
243                         case 2:                                                         /* explode */
244                                 dx = x * 3;
245                                 dy = y * 3;
246                                 break;
247                 }
248                 {
249                         int         qx = xi + quantize(dx), qy = yi + quantize(dy);
250                         int         wrap;
251
252                         if (qx < 0 || qy < 0 ||
253                             qx >= s->width - s->blit_width ||
254                             qy >= s->height - s->blit_height)
255                                 continue;
256
257                         XCopyArea(display, window, window, gc, xi, yi,
258                                   s->blit_width, s->blit_height,
259                                   qx, qy);
260
261                         switch (s->mode) {
262                                 case 0:
263                                         /* wrap */
264                                         wrap = s->width - (2 * s->blit_width);
265                                         if (qx > wrap)
266                                                 XCopyArea(display, window, window, gc, qx, qy,
267                                                 s->blit_width, s->blit_height,
268                                                           qx - wrap, qy);
269
270                                         if (qx < 2 * s->blit_width)
271                                                 XCopyArea(display, window, window, gc, qx, qy,
272                                                 s->blit_width, s->blit_height,
273                                                           qx + wrap, qy);
274
275                                         wrap = s->height - (2 * s->blit_height);
276                                         if (qy > wrap)
277                                                 XCopyArea(display, window, window, gc, qx, qy,
278                                                 s->blit_width, s->blit_height,
279                                                           qx, qy - wrap);
280
281                                         if (qy < 2 * s->blit_height)
282                                                 XCopyArea(display, window, window, gc, qx, qy,
283                                                 s->blit_width, s->blit_height,
284                                                           qx, qy + wrap);
285                                         break;
286                                 case 1:
287                                 case 2:
288                                         break;
289                         }
290                 }
291         }
292 }
293
294 void
295 release_slip(ModeInfo * mi)
296 {
297         if (slips != NULL) {
298                 (void) free((void *) slips);
299                 slips = NULL;
300         }
301 }