began for maemo
[xscreensaver] / xscreensaver / hacks / worm.c
1
2 /* -*- Mode: C; tab-width: 4 -*- */
3 /* worm --- draw wiggly worms */
4
5 #if 0
6 static const char sccsid[] = "@(#)worm.c        4.04 97/07/28 xlockmore";
7 #endif
8
9 /*-
10  * Copyright (c) 1991 by Patrick J. Naughton.
11  *
12  * Permission to use, copy, modify, and distribute this software and its
13  * documentation for any purpose and without fee is hereby granted,
14  * provided that the above copyright notice appear in all copies and that
15  * both that copyright notice and this permission notice appear in
16  * supporting documentation.
17  *
18  * This file is provided AS IS with no warranties of any kind.  The author
19  * shall have no liability with respect to the infringement of copyrights,
20  * trade secrets or any patents by this file or any part thereof.  In no
21  * event will the author be liable for any lost revenue or profits or
22  * other special, indirect and consequential damages.
23  *
24  * Revision History:
25  * 10-May-97: Compatible with xscreensaver
26  * 03-Sep-96: fixed bug in allocation of space for worms, added 3d support
27  *            Henrik Theiling <theiling@coli.uni-sb.de>
28  * 27-Sep-95: put back malloc
29  * 23-Sep-93: got rid of "rint". (David Bagley)
30  * 27-Sep-91: got rid of all malloc calls since there were no calls to free().
31  * 25-Sep-91: Integrated into X11R5 contrib xlock.
32  *
33  * Adapted from a concept in the Dec 87 issue of Scientific American p. 142.
34  *
35  * SunView version: Brad Taylor <brad@sun.com>
36  * X11 version: Dave Lemke <lemke@ncd.com>
37  * xlock version: Boris Putanec <bp@cs.brown.edu>
38  */
39
40 #ifdef STANDALONE
41 # define DEFAULTS       "*delay:  17000 \n"     \
42                                         "*count:  -20 \n"               \
43                                         "*cycles:  10 \n"               \
44                                         "*size:   -3 \n"                \
45                                         "*ncolors: 150 \n"              \
46                                         "*use3d:   False \n"    \
47                                         "*delta3d: 1.5 \n"              \
48                                         "*right3d: red \n"              \
49                                         "*left3d:  blue \n"             \
50                                         "*both3d:  magenta \n"  \
51                                         "*none3d:  black \n"
52 # define SMOOTH_COLORS
53 # define reshape_worm 0
54 # define worm_handle_event 0
55 # include "xlockmore.h"         /* in xscreensaver distribution */
56 #else /* STANDALONE */
57 # include "xlock.h"             /* in xlockmore distribution */
58 #endif /* STANDALONE */
59
60 ENTRYPOINT ModeSpecOpt worm_opts =
61 {0, NULL, 0, NULL, NULL};
62
63 #define MINSIZE 1
64
65 #define SEGMENTS  36
66 #define MINWORMS 1
67
68 #define MAXZ      750
69 #define MINZ      100
70 #define SCREENZ   200
71 #define GETZDIFF(z) (MI_DELTA3D(mi)*20.0*(1.0-(SCREENZ)/((float)(z)+MINZ)))
72 #define IRINT(x) ((int)(((x)>0.0)?(x)+0.5:(x)-0.5))
73
74 /* How many segments to draw per cycle when redrawing */
75 #define REDRAWSTEP 3
76
77 typedef struct {
78         XPoint     *circ;
79         int        *diffcirc;
80         int         dir, dir2;
81         int         tail;
82         int         x, y, z;
83         int         redrawing, redrawpos;
84 } wormstuff;
85
86 typedef struct {
87         int         xsize, ysize, zsize;
88         int         wormlength;
89         int         nc;
90         int         nw;
91         int         circsize;
92         wormstuff  *worm;
93         XRectangle *rects;      /* [NUMCOLORS * batchcount/NUMCOLORS+1] */
94         int         maxsize;
95         int        *size;
96         unsigned int chromo;
97 } wormstruct;
98
99 static float sintab[SEGMENTS];
100 static float costab[SEGMENTS];
101 static int  init_table = 0;
102
103 static wormstruct *worms = NULL;
104
105 static void
106 worm_doit(ModeInfo * mi, int which, unsigned long color)
107 {
108         Display    *display = MI_DISPLAY(mi);
109         Window      window = MI_WINDOW(mi);
110         GC          gc = MI_GC(mi);
111         wormstruct *wp = &worms[MI_SCREEN(mi)];
112         wormstuff  *ws = &wp->worm[which];
113         int         x, y, z;
114         int         diff;
115
116         ws->tail++;
117         if (ws->tail == wp->wormlength)
118                 ws->tail = 0;
119
120         x = ws->circ[ws->tail].x;
121         y = ws->circ[ws->tail].y;
122
123         if (MI_WIN_IS_USE3D(mi)) {
124                 diff = ws->diffcirc[ws->tail];
125                 if (MI_WIN_IS_INSTALL(mi)) {
126                         XSetForeground(display, gc, MI_NONE_COLOR(mi));
127                         XFillRectangle(display, window, gc, x - diff, y,
128                                        wp->circsize, wp->circsize);
129                         XFillRectangle(display, window, gc, x + diff, y,
130                                        wp->circsize, wp->circsize);
131                 } else {
132                         XClearArea(display, window, x - diff, y,
133                                    wp->circsize, wp->circsize, False);
134                         XClearArea(display, window, x + diff, y,
135                                    wp->circsize, wp->circsize, False);
136                 }
137         } else
138                 XClearArea(display, window, x, y, wp->circsize, wp->circsize, False);
139
140         if (LRAND() & 1)
141                 ws->dir = (ws->dir + 1) % SEGMENTS;
142         else
143                 ws->dir = (ws->dir + SEGMENTS - 1) % SEGMENTS;
144
145         x = (ws->x + IRINT((float) wp->circsize * costab[ws->dir]) +
146              wp->xsize) % wp->xsize;
147         y = (ws->y + IRINT((float) wp->circsize * sintab[ws->dir]) +
148              wp->ysize) % wp->ysize;
149
150         ws->circ[ws->tail].x = x;
151         ws->circ[ws->tail].y = y;
152         ws->x = x;
153         ws->y = y;
154
155         if (MI_WIN_IS_USE3D(mi)) {
156                 if (LRAND() & 1)
157                         ws->dir2 = (ws->dir2 + 1) % SEGMENTS;
158                 else
159                         ws->dir2 = (ws->dir2 + SEGMENTS - 1) % SEGMENTS;
160                 /* for the z-axis the wrap-around looks bad, so worms should just turn around. */
161                 z = (int) (ws->z + wp->circsize * sintab[ws->dir2]);
162                 if (z < 0 || z >= wp->zsize)
163                         z = (int) (ws->z - wp->circsize * sintab[ws->dir2]);
164
165                 diff = (int) (GETZDIFF(z) + 0.5);       /* ROUND */
166                 ws->diffcirc[ws->tail] = diff;
167
168                 ws->z = z;
169
170                 /* right eye */
171                 color = 0;
172                 wp->rects[color * wp->maxsize + wp->size[color]].x = x + diff;
173                 wp->rects[color * wp->maxsize + wp->size[color]].y = y;
174                 wp->size[color]++;
175
176                 /* left eye */
177                 color = 1;
178                 wp->rects[color * wp->maxsize + wp->size[color]].x = x - diff;
179                 wp->rects[color * wp->maxsize + wp->size[color]].y = y;
180                 wp->size[color]++;
181
182 #if 0
183                 if (ws->redrawing) {    /* Too hard for now */
184                         int         j;
185
186                         for (j = 0; j < REDRAWSTEP; j++) {
187                                 int         k = (ws->tail - ws->redrawpos + wp->wormlength)
188                                 % wp->wormlength;
189
190                                 color = 0;
191                                 wp->rects[color * wp->maxsize + wp->size[color]].x =
192                                         ws->circ[k].x + ws->diffcirc[k];
193                                 wp->rects[color * wp->maxsize + wp->size[color]].y =
194                                         ws->circ[k].y;
195                                 wp->size[color]++;
196
197                                 color = 1;
198                                 wp->rects[color * wp->maxsize + wp->size[color]].x =
199                                         ws->circ[k].x - ws->diffcirc[k];
200                                 wp->rects[color * wp->maxsize + wp->size[color]].y =
201                                         ws->circ[k].y;
202                                 wp->size[color]++;
203
204                                 if (++(ws->redrawpos) >= wp->wormlength) {
205                                         ws->redrawing = 0;
206                                         break;
207                                 }
208                         }
209                 }
210 #endif
211
212         } else {
213
214                 wp->rects[color * wp->maxsize + wp->size[color]].x = x;
215                 wp->rects[color * wp->maxsize + wp->size[color]].y = y;
216                 wp->size[color]++;
217                 if (ws->redrawing) {
218                         int         j;
219
220                         ws->redrawpos++;
221                         /* Compensates for the changed ws->tail
222                            since the last callback. */
223
224                         for (j = 0; j < REDRAWSTEP; j++) {
225                                 int         k = (ws->tail - ws->redrawpos + wp->wormlength)
226                                 % wp->wormlength;
227
228                                 wp->rects[color * wp->maxsize + wp->size[color]].x = ws->circ[k].x;
229                                 wp->rects[color * wp->maxsize + wp->size[color]].y = ws->circ[k].y;
230                                 wp->size[color]++;
231
232                                 if (++(ws->redrawpos) >= wp->wormlength) {
233                                         ws->redrawing = 0;
234                                         break;
235                                 }
236                         }
237                 }
238         }
239 }
240
241 static void
242 free_worms(wormstruct * wp)
243 {
244         int         wn;
245
246         if (wp->worm) {
247                 for (wn = 0; wn < wp->nw; wn++) {
248                         if (wp->worm[wn].circ)
249                                 (void) free((void *) wp->worm[wn].circ);
250                         if (wp->worm[wn].diffcirc)
251                                 (void) free((void *) wp->worm[wn].diffcirc);
252                 }
253                 (void) free((void *) wp->worm);
254                 wp->worm = NULL;
255         }
256         if (wp->rects) {
257                 (void) free((void *) wp->rects);
258                 wp->rects = NULL;
259         }
260         if (wp->size) {
261                 (void) free((void *) wp->size);
262                 wp->size = NULL;
263         }
264 }
265
266 ENTRYPOINT void
267 init_worm (ModeInfo * mi)
268 {
269         wormstruct *wp;
270         int         size = MI_SIZE(mi);
271         int         i, j;
272
273         if (worms == NULL) {
274                 if ((worms = (wormstruct *) calloc(MI_NUM_SCREENS(mi),
275                                                sizeof (wormstruct))) == NULL)
276                         return;
277         }
278         wp = &worms[MI_SCREEN(mi)];
279         if (MI_NPIXELS(mi) <= 2 || MI_WIN_IS_USE3D(mi))
280                 wp->nc = 2;
281         else
282                 wp->nc = MI_NPIXELS(mi);
283         if (wp->nc > NUMCOLORS)
284                 wp->nc = NUMCOLORS;
285
286         free_worms(wp);
287         wp->nw = MI_BATCHCOUNT(mi);
288         if (wp->nw < -MINWORMS)
289                 wp->nw = NRAND(-wp->nw - MINWORMS + 1) + MINWORMS;
290         else if (wp->nw < MINWORMS)
291                 wp->nw = MINWORMS;
292         if (!wp->worm)
293                 wp->worm = (wormstuff *) malloc(wp->nw * sizeof (wormstuff));
294
295         if (!wp->size)
296                 wp->size = (int *) malloc(NUMCOLORS * sizeof (int));
297
298         wp->maxsize = (REDRAWSTEP + 1) * wp->nw;        /*  / wp->nc + 1; */
299         if (!wp->rects)
300                 wp->rects =
301                         (XRectangle *) malloc(wp->maxsize * NUMCOLORS * sizeof (XRectangle));
302
303
304         if (!init_table) {
305                 init_table = 1;
306                 for (i = 0; i < SEGMENTS; i++) {
307                         sintab[i] = SINF(i * 2.0 * M_PI / SEGMENTS);
308                         costab[i] = COSF(i * 2.0 * M_PI / SEGMENTS);
309                 }
310         }
311         wp->xsize = MI_WIN_WIDTH(mi);
312         wp->ysize = MI_WIN_HEIGHT(mi);
313         wp->zsize = MAXZ - MINZ + 1;
314         if (MI_NPIXELS(mi) > 2)
315                 wp->chromo = NRAND(MI_NPIXELS(mi));
316
317         if (size < -MINSIZE)
318                 wp->circsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
319         else if (size < MINSIZE)
320                 wp->circsize = MINSIZE;
321         else
322                 wp->circsize = size;
323
324         for (i = 0; i < wp->nc; i++) {
325                 for (j = 0; j < wp->maxsize; j++) {
326                         wp->rects[i * wp->maxsize + j].width = wp->circsize;
327                         wp->rects[i * wp->maxsize + j].height = wp->circsize;
328
329                 }
330         }
331         (void) memset((char *) wp->size, 0, wp->nc * sizeof (int));
332
333         wp->wormlength = (int) sqrt(wp->xsize + wp->ysize) *
334                 MI_CYCLES(mi) / 8;      /* Fudge this to something reasonable */
335         for (i = 0; i < wp->nw; i++) {
336                 wp->worm[i].circ = (XPoint *) malloc(wp->wormlength * sizeof (XPoint));
337                 wp->worm[i].diffcirc = (int *) malloc(wp->wormlength * sizeof (int));
338
339                 for (j = 0; j < wp->wormlength; j++) {
340                         wp->worm[i].circ[j].x = wp->xsize / 2;
341                         wp->worm[i].circ[j].y = wp->ysize / 2;
342                         if (MI_WIN_IS_USE3D(mi))
343                                 wp->worm[i].diffcirc[j] = 0;
344                 }
345                 wp->worm[i].dir = NRAND(SEGMENTS);
346                 wp->worm[i].dir2 = NRAND(SEGMENTS);
347                 wp->worm[i].tail = 0;
348                 wp->worm[i].x = wp->xsize / 2;
349                 wp->worm[i].y = wp->ysize / 2;
350                 wp->worm[i].z = SCREENZ - MINZ;
351                 wp->worm[i].redrawing = 0;
352         }
353
354         if (MI_WIN_IS_INSTALL(mi) && MI_WIN_IS_USE3D(mi)) {
355                 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_NONE_COLOR(mi));
356                 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
357                                0, 0, wp->xsize, wp->ysize);
358         } else
359                 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
360 }
361
362 ENTRYPOINT void
363 draw_worm (ModeInfo * mi)
364 {
365         Display    *display = MI_DISPLAY(mi);
366         Window      window = MI_WINDOW(mi);
367         GC          gc = MI_GC(mi);
368         wormstruct *wp = &worms[MI_SCREEN(mi)];
369         unsigned long wcolor;
370         int         i;
371
372         (void) memset((char *) wp->size, 0, wp->nc * sizeof (int));
373
374         for (i = 0; i < wp->nw; i++) {
375                 if (MI_NPIXELS(mi) > 2) {
376                         wcolor = (i + wp->chromo) % wp->nc;
377
378                         worm_doit(mi, i, wcolor);
379                 } else
380                         worm_doit(mi, i, (unsigned long) 0);
381         }
382
383         if (MI_WIN_IS_USE3D(mi)) {
384                 if (MI_WIN_IS_INSTALL(mi))
385                         XSetFunction(display, gc, GXor);
386                 XSetForeground(display, gc, MI_RIGHT_COLOR(mi));
387                 XFillRectangles(display, window, gc, &(wp->rects[0]), wp->size[0]);
388
389                 XSetForeground(display, gc, MI_LEFT_COLOR(mi));
390                 XFillRectangles(display, window, gc, &(wp->rects[wp->maxsize]), wp->size[1]);
391                 if (MI_WIN_IS_INSTALL(mi))
392                         XSetFunction(display, gc, GXcopy);
393         } else if (MI_NPIXELS(mi) > 2) {
394                 for (i = 0; i < wp->nc; i++) {
395                         XSetForeground(display, gc, MI_PIXEL(mi, i));
396                         XFillRectangles(display, window, gc, &(wp->rects[i * wp->maxsize]), wp->size[i]);
397                 }
398         } else {
399                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
400                 XFillRectangles(display, window, gc,
401                                 &(wp->rects[0]), wp->size[0]);
402         }
403
404         if (++wp->chromo == (unsigned long) wp->nc)
405                 wp->chromo = 0;
406 }
407
408 ENTRYPOINT void
409 release_worm(ModeInfo * mi)
410 {
411         if (worms != NULL) {
412                 int         screen;
413
414                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
415                         free_worms(&worms[screen]);
416                 (void) free((void *) worms);
417                 worms = NULL;
418         }
419 }
420
421 ENTRYPOINT void
422 refresh_worm (ModeInfo * mi)
423 {
424         if (MI_WIN_IS_USE3D(mi))
425                 /* The 3D code does drawing&clearing by XORing.  We do not
426                    want to go to too much trouble here to make it redraw
427                    correctly. */
428                 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
429         else if (worms != NULL) {
430                 wormstruct *wp = &worms[MI_SCREEN(mi)];
431                 int         i;
432
433                 for (i = 0; i < wp->nw; i++) {
434                         wp->worm[i].redrawing = 1;
435                         wp->worm[i].redrawpos = 0;
436                 }
437         }
438 }
439
440 XSCREENSAVER_MODULE ("Worm", worm)