1 /* ripples, Copyright (c) 1999 Ian McConnell <ian@emit.demon.co.uk>
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
13 * "Water" ripples that can cross and interfere with each other.
15 * I can't remember where I got this idea from, but it's been around for a
16 * while in various demos. Some inspiration from
17 * water.txt by Tom Hammersley,tomh@globalnet.co.uk
20 * -delay usleep every iteration
21 * -rate Add one drop every "rate" iterations
22 * -box Add big square splash every "box" iters (not very good)
23 * -water Ripples on a grabbed background image
24 * -foreground Interpolate ripples between these two colors
26 * -oily Psychedelic colours like man
27 * -stir Add a regular pattern of drops
28 * -fluidity Between 0 and 16. 16 = big drops
29 * -light Hack to add lighting effect
31 * Code mainly hacked from xflame and decayscreen.
35 * 13 Oct 1999: Initial hack
36 * 30 Oct 1999: Speeded up graphics with dirty buffer. Returned to using
37 * putpixel for greater portability
38 * Added a variety of methods for splashing screen.
39 * 31 Oct 1999: Added in lighting hack
40 * 13 Nov 1999: Speed up tweaks
41 * Adjust "light" for different bits per colour (-water only)
46 #include "screenhack.h"
48 typedef enum {ripple_drop, ripple_blob, ripple_box, ripple_stir} ripple_mode;
50 #ifdef HAVE_XSHM_EXTENSION
52 #endif /* HAVE_XSHM_EXTENSION */
62 XImage *orig_map, *buffer_map;
68 int width, height; /* ripple size */
69 int bigwidth, bigheight; /* screen size */
72 short *bufferA, *bufferB, *temp;
75 double cos_tab[TABLE];
79 unsigned long rmask; /* This builds on the warp effect by adding */
80 unsigned long gmask; /* in a lighting effect: brighten pixels by an */
81 unsigned long bmask; /* amount corresponding to the vertical gradient */
92 int iterations, delay, rate, box, oily, stir, fluidity;
96 void (*draw_transparent) (struct state *st, short *src);
98 async_load_state *img_loader;
100 #ifdef HAVE_XSHM_EXTENSION
102 XShmSegmentInfo shm_info;
103 #endif /* HAVE_XSHM_EXTENSION */
107 /* Distribution of drops: many little ones and a few big ones. */
108 static const double drop_dist[] =
109 {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.6};
112 /* How hard to hit the water */
115 #define MIN(x, y) ((x) < (y) ? (x) : (y))
117 #define MAX(x, y) ((x) > (y) ? (x) : (y))
119 #define DIRTY 3 /* dirty >= 2, 1 = restore original pixel, 0 = leave alone */
121 /* From fortune(6) */
122 /* -- really weird C code to count the number of bits in a word */
123 #define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
124 #define BX_(x) ((x) - (((x)>>1)&0x77777777) \
125 - (((x)>>2)&0x33333333) \
126 - (((x)>>3)&0x11111111))
129 static unsigned long grayscale(struct state *st, unsigned long color);
131 /* ------------------------------------------- */
135 map_color(struct state *st, int grey)
138 grey = st->ncolors * abs(grey) / (SPLASH/4);
139 if (grey > st->ncolors)
143 return st->ctab[grey];
148 draw_ripple(struct state *st, short *src)
151 char *dirty = st->dirty_buffer;
153 for (down = 0; down < st->height - 1; down++, src += 1, dirty += 1)
154 for (across = 0; across < st->width - 1; across++, src++, dirty++) {
157 v2 = (int)*(src + 1);
158 v3 = (int)*(src + st->width);
159 v4 = (int)*(src + st->width + 1);
160 if ((v1 == 0 && v2 == 0 && v3 == 0 && v4 == 0)) {
169 dx = ((v3 - v1) + (v4 - v2)) << st->light; /* light from top */
172 XPutPixel(st->buffer_map,(across<<1), (down<<1), map_color(st, dx + v1));
173 XPutPixel(st->buffer_map,(across<<1)+1,(down<<1), map_color(st, dx + ((v1 + v2) >> 1)));
174 XPutPixel(st->buffer_map,(across<<1), (down<<1)+1,map_color(st, dx + ((v1 + v3) >> 1)));
175 XPutPixel(st->buffer_map,(across<<1)+1,(down<<1)+1,map_color(st, dx + ((v1 + v4) >> 1)));
181 /* ------------------------------------------- */
184 /* Uses the horizontal gradient as an offset to create a warp effect */
186 draw_transparent_vanilla(struct state *st, short *src)
188 int across, down, pixel;
189 char *dirty = st->dirty_buffer;
192 for (down = 0; down < st->height - 2; down++, pixel += 2)
193 for (across = 0; across < st->width-2; across++, pixel++) {
194 int gradx, grady, gradx1, grady1;
195 int x0, x1, x2, y1, y2;
200 y1 = src[pixel + st->width];
201 y2 = src[pixel + 2*st->width];
207 gradx1 = 1 + (gradx + gradx1) / 2;
208 grady1 = 1 + (grady + grady1) / 2;
210 if ((2*across+MIN(gradx,gradx1) < 0) ||
211 (2*across+MAX(gradx,gradx1) >= st->bigwidth)) {
215 if ((2*down+MIN(grady,grady1) < 0) ||
216 (2*down+MAX(grady,grady1) >= st->bigheight)) {
221 if ((gradx == 0 && gradx1 == 1 && grady == 0 && grady1 == 1)) {
222 if (dirty[pixel] > 0)
225 dirty[pixel] = DIRTY;
227 if (dirty[pixel] > 0) {
228 XPutPixel(st->buffer_map, (across<<1), (down<<1),
229 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx, (down<<1) + grady)));
230 XPutPixel(st->buffer_map, (across<<1)+1,(down<<1),
231 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx1,(down<<1) + grady)));
232 XPutPixel(st->buffer_map, (across<<1), (down<<1)+1,
233 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx, (down<<1) + grady1)));
234 XPutPixel(st->buffer_map, (across<<1)+1,(down<<1)+1,
235 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx1,(down<<1) + grady1)));
241 /* ------------------------------------------- */
245 set_mask(unsigned long color, unsigned long *mask, int *shift)
248 while (color != 0 && (color & 1) == 0) {
257 cadd(unsigned long color, int dx, unsigned long mask, int shift)
264 else if (x > (int)mask) x = mask;
266 return color << shift;
271 bright(struct state *st, int dx, unsigned long color)
273 return (cadd(color, dx, st->rmask, st->rshift) |
274 cadd(color, dx, st->gmask, st->gshift) |
275 cadd(color, dx, st->bmask, st->bshift));
280 grayscale(struct state *st, unsigned long color)
290 if (!st->grayscale_p)
292 if (!st->transparent)
294 if ((st->rmask == 0) || (st->gmask == 0) || (st->bmask == 0))
297 red = ((color >> st->rshift) & st->rmask);
298 green = ((color >> st->gshift) & st->gmask);
299 blue = ((color >> st->bshift) & st->bmask);
300 total = red * st->gmask * st->bmask + green * st->rmask * st->bmask + blue * st->rmask * st->gmask;
302 gray_r = total / (3 * st->gmask * st->bmask);
305 if (gray_r > st->rmask)
308 gray_g = total / (3 * st->rmask * st->bmask);
311 if (gray_g > st->gmask)
314 gray_b = total / (3 * st->rmask * st->gmask);
317 if (gray_b > st->bmask)
320 return ((unsigned long)
321 ((gray_r << st->rshift) | (gray_g << st->gshift) | (gray_b << st->bshift)));
326 draw_transparent_light(struct state *st, short *src)
328 int across, down, pixel;
329 char *dirty = st->dirty_buffer;
332 for (down = 0; down < st->height - 2; down++, pixel += 2)
333 for (across = 0; across < st->width-2; across++, pixel++) {
334 int gradx, grady, gradx1, grady1;
335 int x0, x1, x2, y1, y2;
340 y1 = src[pixel + st->width];
341 y2 = src[pixel + 2*st->width];
347 gradx1 = 1 + (gradx + gradx1) / 2;
348 grady1 = 1 + (grady + grady1) / 2;
350 if ((2*across+MIN(gradx,gradx1) < 0) ||
351 (2*across+MAX(gradx,gradx1) >= st->bigwidth)) {
355 if ((2*down+MIN(grady,grady1) < 0) ||
356 (2*down+MAX(grady,grady1) >= st->bigheight)) {
361 if ((gradx == 0 && gradx1 == 1 && grady == 0 && grady1 == 1)) {
362 if (dirty[pixel] > 0)
365 dirty[pixel] = DIRTY;
367 if (dirty[pixel] > 0) {
371 if (4-st->light >= 0)
372 dx = (grady + (src[pixel+st->width+1]-x1)) >> (4-st->light);
374 dx = (grady + (src[pixel+st->width+1]-x1)) << (st->light-4);
377 XPutPixel(st->buffer_map, (across<<1), (down<<1),
378 bright(st, dx, grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx, (down<<1) + grady))));
379 XPutPixel(st->buffer_map, (across<<1)+1,(down<<1),
380 bright(st, dx, grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx1,(down<<1) + grady))));
381 XPutPixel(st->buffer_map, (across<<1), (down<<1)+1,
382 bright(st, dx, grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx, (down<<1) + grady1))));
383 XPutPixel(st->buffer_map, (across<<1)+1,(down<<1)+1,
384 bright(st, dx, grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx1,(down<<1) + grady1))));
386 /* Could use XCopyArea, but XPutPixel is faster */
387 XPutPixel(st->buffer_map, (across<<1), (down<<1),
388 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx, (down<<1) + grady)));
389 XPutPixel(st->buffer_map, (across<<1)+1,(down<<1),
390 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx1,(down<<1) + grady)));
391 XPutPixel(st->buffer_map, (across<<1), (down<<1)+1,
392 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx, (down<<1) + grady1)));
393 XPutPixel(st->buffer_map, (across<<1)+1,(down<<1)+1,
394 grayscale(st, XGetPixel(st->orig_map, (across<<1) + gradx1,(down<<1) + grady1)));
401 /* ------------------------------------------- */
405 /* Doesn't go any faster and doesn't work at all colour depths */
407 draw_transparent16l(short *src)
409 int across, down, bigpix, pixel;
410 char *dirty = st->dirty_buffer;
411 unsigned short *buffer, *orig;
413 buffer = (unsigned short *) st->buffer_map->data;
414 orig = (unsigned short *) st->orig_map->data;
416 for (pixel = bigpix = down = 0;
417 down < st->height - 2;
418 down++, pixel += 2, bigpix += st->bigwidth+4)
419 for (across = 0; across < st->width-2; across++, pixel++, bigpix+=2) {
420 int gradx, grady, gradx1, grady1;
421 int x0, x1, x2, y1, y2;
426 y1 = src[pixel + st->width];
427 y2 = src[pixel + 2*st->width];
433 gradx1 = 1 + (gradx + gradx1) / 2;
434 grady1 = 1 + (grady + grady1) / 2;
436 if ((2*across+MIN(gradx,gradx1) < 0) ||
437 (2*across+MAX(gradx,gradx1) >= st->bigwidth)) {
441 if ((2*down+MIN(grady,grady1) < 0) ||
442 (2*down+MAX(grady,grady1) >= st->bigheight)) {
447 if ((gradx == 0 && gradx1 == 1 && grady == 0 && grady1 == 1)) {
448 if (dirty[pixel] > 0)
451 dirty[pixel] = DIRTY;
453 if (dirty[pixel] > 0) {
454 unsigned short *dest = buffer + bigpix;
455 unsigned short *image = orig + bigpix;
459 if (4-st->light >= 0)
460 dx = (grady + (src[pixel+st->width+1]-x1)) >> (4-st->light);
462 dx = (grady + (src[pixel+st->width+1]-x1)) << (st->light-4);
464 grady *= st->bigwidth;
465 grady1*= st->bigwidth;
468 *dest++ = dobright(dx, *(image + gradx + grady));
469 *dest = dobright(dx, *(image + gradx1 + grady));
470 dest += st->bigwidth - 1;
471 *dest++ = dobright(dx, *(image + gradx + grady1));
472 *dest = dobright(dx, *(image + gradx1 + grady1));
474 *dest++ = *(image + gradx + grady);
475 *dest = *(image + gradx1 + grady);
476 dest += st->bigwidth - 1;
477 *dest++ = *(image + gradx + grady1);
478 *dest = *(image + gradx1 + grady1);
486 /* ------------------------------------------- */
490 setup_X(struct state *st)
492 XWindowAttributes xgwa;
495 XGetWindowAttributes(st->dpy, st->window, &xgwa);
497 st->colormap = xgwa.colormap;
498 st->bigwidth = xgwa.width;
499 st->bigheight = xgwa.height;
500 st->visual = xgwa.visual;
503 /* This causes buffer_map to be 1 pixel taller and wider than orig_map,
504 which can cause the two XImages to have different bytes-per-line,
505 which causes stair-stepping. So this better not be necessary...
508 #if 0 /* I'm not entirely sure if I need this */
509 if (st->bigwidth % 2)
511 if (st->bigheight % 2)
516 st->width = st->bigwidth / 2;
517 st->height = st->bigheight / 2;
519 if (st->transparent) {
523 gcv.function = GXcopy;
524 gcv.subwindow_mode = IncludeInferiors;
526 gcflags = GCFunction;
527 if (use_subwindow_mode_p(xgwa.screen, st->window)) /* see grabscreen.c */
528 gcflags |= GCSubwindowMode;
530 st->gc = XCreateGC(st->dpy, st->window, gcflags, &gcv);
532 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
534 st->start_time = time ((time_t) 0);
538 st->gc = XCreateGC(st->dpy, st->window, 0, &gcv);
543 fprintf(stderr, "XCreateGC failed\n");
549 #ifdef HAVE_XSHM_EXTENSION
551 st->buffer_map = create_xshm_image(st->dpy, xgwa.visual, depth,
552 ZPixmap, 0, &st->shm_info, st->bigwidth, st->bigheight);
553 if (!st->buffer_map) {
555 fprintf(stderr, "create_xshm_image failed\n");
558 #endif /* HAVE_XSHM_EXTENSION */
560 if (!st->buffer_map) {
561 st->buffer_map = XCreateImage(st->dpy, xgwa.visual,
562 depth, ZPixmap, 0, 0,
563 st->bigwidth, st->bigheight, 8, 0);
564 st->buffer_map->data = (char *)
565 calloc(st->buffer_map->height, st->buffer_map->bytes_per_line);
571 DisplayImage(struct state *st)
573 #ifdef HAVE_XSHM_EXTENSION
575 XShmPutImage(st->dpy, st->window, st->gc, st->buffer_map, 0, 0, 0, 0,
576 st->bigwidth, st->bigheight, False);
578 #endif /* HAVE_XSHM_EXTENSION */
579 XPutImage(st->dpy, st->window, st->gc, st->buffer_map, 0, 0, 0, 0,
580 st->bigwidth, st->bigheight);
584 /* ------------------------------------------- */
588 cinterp(double a, int bg, int fg)
591 result = (int)((1-a) * bg + a * fg + 0.5);
592 if (result < 0) result = 0;
593 if (result > 255) result = 255;
598 /* Interpolate the ripple colours between the background colour and
601 init_linear_colors(struct state *st)
603 int i, j, red, green, blue, bred, bgreen, bblue;
606 if (st->ncolors < 2 || mono_p)
608 if (st->ncolors <= 2)
611 /* Make it possible to set the color of the ripples,
612 Based on work by Raymond Medeiros <ray@stommel.marine.usf.edu> and jwz.
614 fg.pixel = get_pixel_resource(st->dpy, st->colormap, "foreground", "Foreground");
615 XQueryColor(st->dpy, st->colormap, &fg);
617 green = (fg.green >> 8);
618 blue = (fg.blue >> 8);
620 bg.pixel = get_pixel_resource(st->dpy, st->colormap, "background", "Background");
621 XQueryColor(st->dpy, st->colormap, &bg);
622 bred = (bg.red >> 8);
623 bgreen = (bg.green >> 8);
624 bblue = (bg.blue >> 8);
627 for (i = 0; i < st->ncolors+1; i++) {
629 double a = (double)i / st->ncolors;
630 int r = cinterp(a, bred, red);
631 int g = cinterp(a, bgreen, green);
632 int b = cinterp(a, bblue, blue);
634 xcl.red = (unsigned short) ((r << 8) | r);
635 xcl.green = (unsigned short) ((g << 8) | g);
636 xcl.blue = (unsigned short) ((b << 8) | b);
637 xcl.flags = DoRed | DoGreen | DoBlue;
639 XAllocColor(st->dpy, st->colormap, &xcl);
641 st->ctab[j++] = (int) xcl.pixel;
647 init_oily_colors(struct state *st)
649 XColor *colors = NULL;
651 if (st->ncolors < 2 || mono_p)
653 if (st->ncolors <= 2)
658 colors = (XColor *)malloc(sizeof(*colors) * (st->ncolors+1));
659 make_smooth_colormap(st->dpy, st->visual, st->colormap, colors, &st->ncolors,
661 False, /* not writable */
662 True); /* verbose (complain about failure) */
663 if (st->ncolors <= 2) {
672 for (i = 0; i < st->ncolors+1; i++) {
673 XAllocColor(st->dpy, st->colormap, colors+i);
674 st->ctab[j++] = (int) colors[i].pixel;
679 st->ctab[1] = get_pixel_resource(st->dpy, st->colormap, "foreground", "Foreground");
680 st->ctab[0] = get_pixel_resource(st->dpy, st->colormap, "background", "Background");
685 /* ------------------------------------------- */
689 init_cos_tab(struct state *st)
692 for (i = 0; i < TABLE; i++)
693 st->cos_tab[i] = cos(i * M_PI/2 / TABLE);
697 /* Shape of drop to add */
699 sinc(struct state *st, double x)
704 i = (int)(x * TABLE + 0.5);
705 if (i >= TABLE) i = (TABLE-1) - (i-(TABLE-1));
706 return st->cos_tab[i];
708 return cos(x * M_PI/2);
719 add_circle_drop(struct state *st, int x, int y, int radius, int dheight)
722 short *buf = (random()&1) ? st->bufferA : st->bufferB;
724 r2 = radius * radius;
727 for (cy = -radius; cy <= radius; cy++)
728 for (cx = -radius; cx <= radius; cx++) {
731 if (xx < 0 || yy < 0 || xx >= st->width || yy >= st->height) {break;}
734 buf[xx + yy*st->width] =
735 (short)(dheight * sinc(st, sqrt(r)/radius));
741 add_drop(struct state *st, ripple_mode mode, int drop)
743 int newx, newy, dheight;
744 int radius = MIN(st->width, st->height) / 50;
745 /* Don't put drops too near the edge of the screen or they get stuck */
753 dheight = 1 + (random() % drop);
754 newx = border + (random() % (st->width - 2*border));
755 newy = border + (random() % (st->height - 2*border));
756 x = newy * st->width + newx;
757 st->bufferA[x + 1] = st->bufferA[x] = st->bufferA[x + st->width] = st->bufferA[x + st->width + 1] =
758 st->bufferB[x + 1] = st->bufferB[x] = st->bufferB[x + st->width] = st->bufferB[x + st->width + 1] =
766 power = drop_dist[random() % (sizeof(drop_dist)/sizeof(drop_dist[0]))]; /* clumsy */
767 dheight = (int)(drop * (power + 0.01));
768 tmp_i = (int)(st->width - 2*border - 2*radius*power);
769 tmp_j = (int)(st->height - 2*border - 2*radius*power);
770 newx = radius + border + ((tmp_i > 0) ? random() % tmp_i : 0);
771 newy = radius + border + ((tmp_j > 0) ? random() % tmp_j : 0);
772 add_circle_drop(st, newx, newy, radius, dheight);
775 /* Adding too many boxes too quickly (-box 1) doesn't give the waves time
776 to disperse and the waves build up (and overflow) */
780 short *buf = (random()&1) ? st->bufferA : st->bufferB;
783 radius = (1 + (random() % 5)) * (1 + (random() % 5));
784 dheight = drop / 128;
785 if (random() & 1) dheight = -dheight;
786 tmp_i = st->width - 2*border - 2*radius;
787 tmp_j = st->height - 2*border - 2*radius;
788 newx = radius + border + ((tmp_i > 0) ? random() % tmp_i : 0);
789 newy = radius + border + ((tmp_j > 0) ? random() % tmp_j : 0);
790 x = newy * st->width + newx;
791 for (cy = -radius; cy <= radius; cy++)
792 for (cx = -radius; cx <= radius; cx++)
793 buf[x + cx + cy*st->width] = (short)(dheight);
798 newx = border + (int)((st->width-2*border) * (1+cos(3*st->stir_ang)) / 2);
799 newy = border + (int)((st->height-2*border) * (1+sin(2*st->stir_ang)) / 2);
800 add_circle_drop(st, newx, newy, radius, drop / 10);
801 st->stir_ang += 0.02;
802 if (st->stir_ang > 12*M_PI) st->stir_ang = 0;
810 init_ripples(struct state *st, int ndrops, int splash)
814 st->bufferA = (short *)calloc(st->width * st->height, sizeof(*st->bufferA));
815 st->bufferB = (short *)calloc(st->width * st->height, sizeof(*st->bufferB));
816 st->temp = (short *)calloc(st->width * st->height, sizeof(*st->temp));
818 st->dirty_buffer = (char *)calloc(st->width * st->height, sizeof(*st->dirty_buffer));
820 for (i = 0; i < ndrops; i++)
821 add_drop(st, ripple_blob, splash);
823 if (st->transparent) {
827 for (down = 0; down < st->bigheight; down++)
828 for (across = 0; across < st->bigwidth; across++)
829 XPutPixel(st->buffer_map, across, down,
830 grayscale(st, XGetPixel(st->orig_map, across, down)));
834 /* There's got to be a better way of doing this XCopyArea? */
835 memcpy(st->buffer_map->data, st->orig_map->data,
836 st->bigheight * st->buffer_map->bytes_per_line);
839 int across, down, color;
841 color = map_color(st, 0); /* background colour */
842 for (down = 0; down < st->bigheight; down++)
843 for (across = 0; across < st->bigwidth; across++)
844 XPutPixel(st->buffer_map,across, down, color);
852 Explanation from hq_water.zip (Arturo R Montesinos (ARM) arami@fi.upm.es)
854 Differential equation is: u = a ( u + u )
857 Where a = tension * gravity / surface_density.
859 Approximating second derivatives by central differences:
861 [ u(t+1)-2u(t)+u(t-1) ] / dt = a [ u(x+1)+u(x-1)+u(y+1)+u(y-1)-4u ] / h
863 where dt = time step squared, h = dx*dy = mesh resolution squared.
865 From where u(t+1) may be calculated as:
868 u(t+1) = a -- | 1 0 1 |u - u(t-1) + (2-4a --)u
871 When a*dt/h = 1/2 last term vanishes, giving:
874 u(t+1) = - | 1 0 1 |u - u(t-1)
877 (note that u(t-1,x,y) is only used to calculate u(t+1,x,y) so
878 we can use the same array for both t-1 and t+1, needing only
879 two arrays, U[0] and U[1])
881 Dampening is simulated by subtracting 1/2^n of result.
882 n=4 gives best-looking result
883 n<4 (eg 2 or 3) thicker consistency, waves die out immediately
884 n>4 (eg 8 or 12) more fluid, waves die out slowly
888 ripple(struct state *st)
890 int across, down, pixel;
893 if (st->draw_toggle == 0) {
903 switch (st->draw_count) {
905 pixel = 1 * st->width + 1;
906 for (down = 1; down < st->height - 1; down++, pixel += 2 * 1)
907 for (across = 1; across < st->width - 1; across++, pixel++) {
909 (((src[pixel - 1] + src[pixel + 1] +
910 src[pixel - st->width] + src[pixel + st->width]) / 2)) - dest[pixel];
913 /* Smooth the output */
914 pixel = 1 * st->width + 1;
915 for (down = 1; down < st->height - 1; down++, pixel += 2 * 1)
916 for (across = 1; across < st->width - 1; across++, pixel++) {
917 if (st->temp[pixel] != 0) { /* Close enough for government work */
919 (st->temp[pixel - 1] + st->temp[pixel + 1] +
920 st->temp[pixel - st->width] + st->temp[pixel + st->width] +
921 st->temp[pixel - st->width - 1] + st->temp[pixel - st->width + 1] +
922 st->temp[pixel + st->width - 1] + st->temp[pixel + st->width + 1] +
923 st->temp[pixel]) / 9;
924 dest[pixel] = damp - (damp >> st->fluidity);
930 pixel = 1 * st->width + 1;
931 for (down = 1; down < st->height - 1; down++, pixel += 2 * 1)
932 for (across = 1; across < st->width - 1; across++, pixel++) {
934 (((src[pixel - 1] + src[pixel + 1] +
935 src[pixel - st->width] + src[pixel + st->width]) / 2)) - dest[pixel];
936 dest[pixel] = damp - (damp >> st->fluidity);
940 if (++st->draw_count > 3) st->draw_count = 0;
943 st->draw_transparent(st, dest);
945 draw_ripple(st, dest);
949 /* ------------------------------------------- */
952 ripples_init (Display *disp, Window win)
954 struct state *st = (struct state *) calloc (1, sizeof(*st));
958 st->delay = get_integer_resource(disp, "delay", "Integer");
959 st->duration = get_integer_resource (st->dpy, "duration", "Seconds");
960 st->rate = get_integer_resource(disp, "rate", "Integer");
961 st->box = get_integer_resource(disp, "box", "Integer");
962 st->oily = get_boolean_resource(disp, "oily", "Boolean");
963 st->stir = get_boolean_resource(disp, "stir", "Boolean");
964 st->fluidity = get_integer_resource(disp, "fluidity", "Integer");
965 st->transparent = get_boolean_resource(disp, "water", "Boolean");
966 st->grayscale_p = get_boolean_resource(disp, "grayscale", "Boolean");
967 #ifdef HAVE_XSHM_EXTENSION
968 st->use_shm = get_boolean_resource(disp, "useSHM", "Boolean");
969 #endif /* HAVE_XSHM_EXTENSION */
970 st->light = get_integer_resource(disp, "light", "Integer");
972 if (st->delay < 0) st->delay = 0;
973 if (st->duration < 1) st->duration = 1;
974 if (st->fluidity <= 1) st->fluidity = 1;
975 if (st->fluidity > 16) st->fluidity = 16; /* 16 = sizeof(short) */
976 if (st->light < 0) st->light = 0;
981 st->ncolors = get_integer_resource (disp, "colors", "Colors");
982 if (0 == st->ncolors) /* English spelling? */
983 st->ncolors = get_integer_resource (disp, "colours", "Colors");
985 if (st->ncolors > sizeof(st->ctab)/sizeof(*st->ctab))
986 st->ncolors = sizeof(st->ctab)/sizeof(*st->ctab);
989 init_oily_colors(st);
991 init_linear_colors(st);
993 if (st->transparent && st->light > 0) {
995 st->draw_transparent = draw_transparent_light;
996 set_mask(st->visual->red_mask, &st->rmask, &st->rshift);
997 set_mask(st->visual->green_mask, &st->gmask, &st->gshift);
998 set_mask(st->visual->blue_mask, &st->bmask, &st->bshift);
999 if (st->rmask == 0) st->draw_transparent = draw_transparent_vanilla;
1001 /* Adjust the shift value "light" when we don't have 8 bits per colour */
1002 maxbits = MIN(MIN(BITCOUNT(st->rmask), BITCOUNT(st->gmask)), BITCOUNT(st->bmask));
1003 st->light -= 8-maxbits;
1004 if (st->light < 0) st->light = 0;
1006 if (st->grayscale_p)
1008 set_mask(st->visual->red_mask, &st->rmask, &st->rshift);
1009 set_mask(st->visual->green_mask, &st->gmask, &st->gshift);
1010 set_mask(st->visual->blue_mask, &st->bmask, &st->bshift);
1012 st->draw_transparent = draw_transparent_vanilla;
1015 if (!st->transparent)
1016 init_ripples(st, 0, -SPLASH); /* Start off without any drops */
1021 static unsigned long
1022 ripples_draw (Display *dpy, Window window, void *closure)
1024 struct state *st = (struct state *) closure;
1026 if (st->img_loader) /* still loading */
1028 st->img_loader = load_image_async_simple (st->img_loader, 0, 0, 0, 0, 0);
1029 if (! st->img_loader) { /* just finished */
1030 XWindowAttributes xgwa;
1031 XGetWindowAttributes(st->dpy, st->window, &xgwa);
1032 st->start_time = time ((time_t) 0);
1033 st->orig_map = XGetImage (st->dpy, st->window, 0, 0,
1034 xgwa.width, xgwa.height,
1036 init_ripples(st, 0, -SPLASH); /* Start off without any drops */
1041 if (!st->img_loader &&
1042 st->start_time + st->duration < time ((time_t) 0)) {
1043 XWindowAttributes xgwa;
1044 XGetWindowAttributes(st->dpy, st->window, &xgwa);
1045 st->img_loader = load_image_async_simple (0, xgwa.screen, st->window,
1047 st->start_time = time ((time_t) 0);
1051 if (st->rate > 0 && (st->iterations % st->rate) == 0)
1052 add_drop(st, ripple_blob, -SPLASH);
1054 add_drop(st, ripple_stir, -SPLASH);
1055 if (st->box > 0 && (random() % st->box) == 0)
1056 add_drop(st, ripple_box, -SPLASH);
1068 ripples_reshape (Display *dpy, Window window, void *closure,
1069 unsigned int w, unsigned int h)
1074 ripples_event (Display *dpy, Window window, void *closure, XEvent *event)
1080 ripples_free (Display *dpy, Window window, void *closure)
1082 struct state *st = (struct state *) closure;
1086 static const char *ripples_defaults[] =
1088 ".background: black",
1089 ".foreground: #FFAF5F",
1091 "*dontClearRoot: True",
1101 "*grayscale: False",
1102 #ifdef HAVE_XSHM_EXTENSION
1110 static XrmOptionDescRec ripples_options[] =
1112 { "-colors", ".colors", XrmoptionSepArg, 0},
1113 { "-colours", ".colors", XrmoptionSepArg, 0},
1114 {"-delay", ".delay", XrmoptionSepArg, 0},
1115 {"-duration", ".duration", XrmoptionSepArg, 0 },
1116 {"-rate", ".rate", XrmoptionSepArg, 0},
1117 {"-box", ".box", XrmoptionSepArg, 0},
1118 {"-water", ".water", XrmoptionNoArg, "True"},
1119 {"-oily", ".oily", XrmoptionNoArg, "True"},
1120 {"-stir", ".stir", XrmoptionNoArg, "True"},
1121 {"-fluidity", ".fluidity", XrmoptionSepArg, 0},
1122 {"-light", ".light", XrmoptionSepArg, 0},
1123 {"-grayscale", ".grayscale", XrmoptionNoArg, "True"},
1124 {"-shm", ".useSHM", XrmoptionNoArg, "True"},
1125 {"-no-shm", ".useSHM", XrmoptionNoArg, "False"},
1130 XSCREENSAVER_MODULE ("Ripples", ripples)