2 /* -*- Mode: C; tab-width: 4 -*- */
3 /* worm --- draw wiggly worms */
6 static const char sccsid[] = "@(#)worm.c 4.04 97/07/28 xlockmore";
10 * Copyright (c) 1991 by Patrick J. Naughton.
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.
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.
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.
33 * Adapted from a concept in the Dec 87 issue of Scientific American p. 142.
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>
41 # define DEFAULTS "*delay: 17000 \n" \
50 "*both3d: magenta \n" \
52 "*fpsSolid: true \n" \
54 # define SMOOTH_COLORS
55 # define release_worm 0
56 # define reshape_worm 0
57 # define worm_handle_event 0
58 # include "xlockmore.h" /* in xscreensaver distribution */
59 #else /* STANDALONE */
60 # include "xlock.h" /* in xlockmore distribution */
61 #endif /* STANDALONE */
63 ENTRYPOINT ModeSpecOpt worm_opts =
64 {0, NULL, 0, NULL, NULL};
74 #define GETZDIFF(z) (MI_DELTA3D(mi)*20.0*(1.0-(SCREENZ)/((float)(z)+MINZ)))
75 #define IRINT(x) ((int)(((x)>0.0)?(x)+0.5:(x)-0.5))
77 /* How many segments to draw per cycle when redrawing */
86 int redrawing, redrawpos;
90 int xsize, ysize, zsize;
96 XRectangle *rects; /* [NUMCOLORS * batchcount/NUMCOLORS+1] */
102 static float sintab[SEGMENTS];
103 static float costab[SEGMENTS];
104 static int init_table = 0;
106 static wormstruct *worms = NULL;
109 worm_doit(ModeInfo * mi, int which, unsigned long color)
111 Display *display = MI_DISPLAY(mi);
112 Window window = MI_WINDOW(mi);
114 wormstruct *wp = &worms[MI_SCREEN(mi)];
115 wormstuff *ws = &wp->worm[which];
120 if (ws->tail == wp->wormlength)
123 x = ws->circ[ws->tail].x;
124 y = ws->circ[ws->tail].y;
126 if (MI_WIN_IS_USE3D(mi)) {
127 diff = ws->diffcirc[ws->tail];
128 if (MI_WIN_IS_INSTALL(mi)) {
129 XSetForeground(display, gc, MI_NONE_COLOR(mi));
130 XFillRectangle(display, window, gc, x - diff, y,
131 wp->circsize, wp->circsize);
132 XFillRectangle(display, window, gc, x + diff, y,
133 wp->circsize, wp->circsize);
135 XClearArea(display, window, x - diff, y,
136 wp->circsize, wp->circsize, False);
137 XClearArea(display, window, x + diff, y,
138 wp->circsize, wp->circsize, False);
141 XClearArea(display, window, x, y, wp->circsize, wp->circsize, False);
144 ws->dir = (ws->dir + 1) % SEGMENTS;
146 ws->dir = (ws->dir + SEGMENTS - 1) % SEGMENTS;
148 x = (ws->x + IRINT((float) wp->circsize * costab[ws->dir]) +
149 wp->xsize) % wp->xsize;
150 y = (ws->y + IRINT((float) wp->circsize * sintab[ws->dir]) +
151 wp->ysize) % wp->ysize;
153 ws->circ[ws->tail].x = x;
154 ws->circ[ws->tail].y = y;
158 if (MI_WIN_IS_USE3D(mi)) {
160 ws->dir2 = (ws->dir2 + 1) % SEGMENTS;
162 ws->dir2 = (ws->dir2 + SEGMENTS - 1) % SEGMENTS;
163 /* for the z-axis the wrap-around looks bad, so worms should just turn around. */
164 z = (int) (ws->z + wp->circsize * sintab[ws->dir2]);
165 if (z < 0 || z >= wp->zsize)
166 z = (int) (ws->z - wp->circsize * sintab[ws->dir2]);
168 diff = (int) (GETZDIFF(z) + 0.5); /* ROUND */
169 ws->diffcirc[ws->tail] = diff;
175 wp->rects[color * wp->maxsize + wp->size[color]].x = x + diff;
176 wp->rects[color * wp->maxsize + wp->size[color]].y = y;
181 wp->rects[color * wp->maxsize + wp->size[color]].x = x - diff;
182 wp->rects[color * wp->maxsize + wp->size[color]].y = y;
186 if (ws->redrawing) { /* Too hard for now */
189 for (j = 0; j < REDRAWSTEP; j++) {
190 int k = (ws->tail - ws->redrawpos + wp->wormlength)
194 wp->rects[color * wp->maxsize + wp->size[color]].x =
195 ws->circ[k].x + ws->diffcirc[k];
196 wp->rects[color * wp->maxsize + wp->size[color]].y =
201 wp->rects[color * wp->maxsize + wp->size[color]].x =
202 ws->circ[k].x - ws->diffcirc[k];
203 wp->rects[color * wp->maxsize + wp->size[color]].y =
207 if (++(ws->redrawpos) >= wp->wormlength) {
217 wp->rects[color * wp->maxsize + wp->size[color]].x = x;
218 wp->rects[color * wp->maxsize + wp->size[color]].y = y;
224 /* Compensates for the changed ws->tail
225 since the last callback. */
227 for (j = 0; j < REDRAWSTEP; j++) {
228 int k = (ws->tail - ws->redrawpos + wp->wormlength)
231 wp->rects[color * wp->maxsize + wp->size[color]].x = ws->circ[k].x;
232 wp->rects[color * wp->maxsize + wp->size[color]].y = ws->circ[k].y;
235 if (++(ws->redrawpos) >= wp->wormlength) {
245 free_worm(ModeInfo * mi)
252 wp = &worms[MI_SCREEN(mi)];
255 for (wn = 0; wn < wp->nw; wn++) {
256 if (wp->worm[wn].circ)
257 (void) free((void *) wp->worm[wn].circ);
258 if (wp->worm[wn].diffcirc)
259 (void) free((void *) wp->worm[wn].diffcirc);
261 (void) free((void *) wp->worm);
265 (void) free((void *) wp->rects);
269 (void) free((void *) wp->size);
275 init_worm (ModeInfo * mi)
278 int size = MI_SIZE(mi);
282 wp = &worms[MI_SCREEN(mi)];
283 if (MI_NPIXELS(mi) <= 2 || MI_WIN_IS_USE3D(mi))
286 wp->nc = MI_NPIXELS(mi);
287 if (wp->nc > NUMCOLORS)
291 wp->nw = MI_BATCHCOUNT(mi);
292 if (wp->nw < -MINWORMS)
293 wp->nw = NRAND(-wp->nw - MINWORMS + 1) + MINWORMS;
294 else if (wp->nw < MINWORMS)
297 wp->worm = (wormstuff *) malloc(wp->nw * sizeof (wormstuff));
300 wp->size = (int *) malloc(NUMCOLORS * sizeof (int));
302 wp->maxsize = (REDRAWSTEP + 1) * wp->nw; /* / wp->nc + 1; */
305 (XRectangle *) malloc(wp->maxsize * NUMCOLORS * sizeof (XRectangle));
310 for (i = 0; i < SEGMENTS; i++) {
311 sintab[i] = SINF(i * 2.0 * M_PI / SEGMENTS);
312 costab[i] = COSF(i * 2.0 * M_PI / SEGMENTS);
315 wp->xsize = MI_WIN_WIDTH(mi);
316 wp->ysize = MI_WIN_HEIGHT(mi);
317 wp->zsize = MAXZ - MINZ + 1;
318 if (MI_NPIXELS(mi) > 2)
319 wp->chromo = NRAND(MI_NPIXELS(mi));
322 wp->circsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
323 else if (size < MINSIZE)
324 wp->circsize = MINSIZE;
328 for (i = 0; i < wp->nc; i++) {
329 for (j = 0; j < wp->maxsize; j++) {
330 wp->rects[i * wp->maxsize + j].width = wp->circsize;
331 wp->rects[i * wp->maxsize + j].height = wp->circsize;
335 (void) memset((char *) wp->size, 0, wp->nc * sizeof (int));
337 wp->wormlength = (int) sqrt(wp->xsize + wp->ysize) *
338 MI_CYCLES(mi) / 8; /* Fudge this to something reasonable */
339 for (i = 0; i < wp->nw; i++) {
340 wp->worm[i].circ = (XPoint *) malloc(wp->wormlength * sizeof (XPoint));
341 wp->worm[i].diffcirc = (int *) malloc(wp->wormlength * sizeof (int));
343 for (j = 0; j < wp->wormlength; j++) {
344 wp->worm[i].circ[j].x = wp->xsize / 2;
345 wp->worm[i].circ[j].y = wp->ysize / 2;
346 if (MI_WIN_IS_USE3D(mi))
347 wp->worm[i].diffcirc[j] = 0;
349 wp->worm[i].dir = NRAND(SEGMENTS);
350 wp->worm[i].dir2 = NRAND(SEGMENTS);
351 wp->worm[i].tail = 0;
352 wp->worm[i].x = wp->xsize / 2;
353 wp->worm[i].y = wp->ysize / 2;
354 wp->worm[i].z = SCREENZ - MINZ;
355 wp->worm[i].redrawing = 0;
358 if (MI_WIN_IS_INSTALL(mi) && MI_WIN_IS_USE3D(mi)) {
359 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_NONE_COLOR(mi));
360 XFillRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
361 0, 0, wp->xsize, wp->ysize);
363 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
367 draw_worm (ModeInfo * mi)
369 Display *display = MI_DISPLAY(mi);
370 Window window = MI_WINDOW(mi);
372 wormstruct *wp = &worms[MI_SCREEN(mi)];
373 unsigned long wcolor;
376 (void) memset((char *) wp->size, 0, wp->nc * sizeof (int));
378 for (i = 0; i < wp->nw; i++) {
379 if (MI_NPIXELS(mi) > 2) {
380 wcolor = (i + wp->chromo) % wp->nc;
382 worm_doit(mi, i, wcolor);
384 worm_doit(mi, i, (unsigned long) 0);
387 if (MI_WIN_IS_USE3D(mi)) {
388 if (MI_WIN_IS_INSTALL(mi))
389 XSetFunction(display, gc, GXor);
390 XSetForeground(display, gc, MI_RIGHT_COLOR(mi));
391 XFillRectangles(display, window, gc, &(wp->rects[0]), wp->size[0]);
393 XSetForeground(display, gc, MI_LEFT_COLOR(mi));
394 XFillRectangles(display, window, gc, &(wp->rects[wp->maxsize]), wp->size[1]);
395 if (MI_WIN_IS_INSTALL(mi))
396 XSetFunction(display, gc, GXcopy);
397 } else if (MI_NPIXELS(mi) > 2) {
398 for (i = 0; i < wp->nc; i++) {
399 XSetForeground(display, gc, MI_PIXEL(mi, i));
400 XFillRectangles(display, window, gc, &(wp->rects[i * wp->maxsize]), wp->size[i]);
403 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
404 XFillRectangles(display, window, gc,
405 &(wp->rects[0]), wp->size[0]);
408 if (++wp->chromo == (unsigned long) wp->nc)
414 refresh_worm (ModeInfo * mi)
416 if (MI_WIN_IS_USE3D(mi))
417 /* The 3D code does drawing&clearing by XORing. We do not
418 want to go to too much trouble here to make it redraw
420 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
421 else if (worms != NULL) {
422 wormstruct *wp = &worms[MI_SCREEN(mi)];
425 for (i = 0; i < wp->nw; i++) {
426 wp->worm[i].redrawing = 1;
427 wp->worm[i].redrawpos = 0;
433 XSCREENSAVER_MODULE ("Worm", worm)