X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=utils%2Ferase.c;h=05b213e2aec540a4fac553b57f2e4b0367b560a3;hb=50be9bb40dc60130c99ffa568e6677779904ff70;hp=bd7a345d1d74cec162d9431286d73d17cf615778;hpb=3c58fb6311db49c46f1670922933b27c6ea0c065;p=xscreensaver diff --git a/utils/erase.c b/utils/erase.c index bd7a345d..05b213e2 100644 --- a/utils/erase.c +++ b/utils/erase.c @@ -1,5 +1,5 @@ /* erase.c: Erase the screen in various more or less interesting ways. - * Copyright (c) 1997-2001 Jamie Zawinski + * Copyright (c) 1997-2008 Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -19,1034 +19,627 @@ #include "yarandom.h" #include "usleep.h" #include "resources.h" +#include "erase.h" +#include /* for gettimeofday() */ extern char *progname; #undef countof #define countof(x) (sizeof(x)/sizeof(*(x))) -#define LITTLE_NAP 5000 /* 1/200th sec */ +typedef void (*Eraser) (eraser_state *); -typedef void (*Eraser) (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs); +struct eraser_state { + Display *dpy; + Window window; + GC fg_gc, bg_gc; + int width, height; + Eraser fn; + double start_time, stop_time; + double ratio, prev_ratio; -static unsigned long -millitime (void) + /* data for random_lines, venetian, random_squares */ + Bool horiz_p; + Bool flip_p; + int nlines, *lines; + + /* data for triple_wipe, quad_wipe */ + Bool flip_x, flip_y; + + /* data for circle_wipe, three_circle_wipe */ + int start; + + /* data for random_squares */ + int cols; + +}; + + +static double +double_time (void) { - struct timeval tt; + struct timeval now; # ifdef GETTIMEOFDAY_TWO_ARGS - struct timezone tz; - gettimeofday (&tt, &tz); + struct timezone tzp; + gettimeofday(&now, &tzp); # else - gettimeofday (&tt); + gettimeofday(&now); # endif - return (tt.tv_sec * 1000) + (tt.tv_usec / 1000); + + return (now.tv_sec + ((double) now.tv_usec * 0.000001)); } static void -random_lines (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +random_lines (eraser_state *st) { - int granularity = 50; - - Bool horiz_p = (random() & 1); - int max = (horiz_p ? height : width); - int *lines = (int *) calloc(max, sizeof(*lines)); - int oi = -1; int i; - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - for (i = 0; i < max; i++) - lines[i] = i; - - for (i = 0; i < max; i++) + if (! st->lines) /* first time */ { - int t, r; - t = lines[i]; - r = random() % max; - lines[i] = lines[r]; - lines[r] = t; - } + st->horiz_p = (random() & 1); + st->nlines = (st->horiz_p ? st->height : st->width); + st->lines = (int *) calloc (st->nlines, sizeof(*st->lines)); - while (tick < end_tick) - { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); + for (i = 0; i < st->nlines; i++) /* every line */ + st->lines[i] = i; - i /= granularity; - - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else + for (i = 0; i < st->nlines; i++) /* shuffle */ { - int j; - for (j = 0; j < granularity; j++) - { - int ii = i * granularity + j; - if (horiz_p) - XDrawLine (dpy, window, gc, 0, lines[ii], width, lines[ii]); - else - XDrawLine (dpy, window, gc, lines[ii], 0, lines[ii], height); - hits++; - } - XSync (dpy, False); + int t, r; + t = st->lines[i]; + r = random() % st->nlines; + st->lines[i] = st->lines[r]; + st->lines[r] = t; } + } - oi = i; - tick = millitime(); + for (i = st->nlines * st->prev_ratio; + i < st->nlines * st->ratio; + i++) + { + if (st->horiz_p) + XDrawLine (st->dpy, st->window, st->bg_gc, + 0, st->lines[i], st->width, st->lines[i]); + else + XDrawLine (st->dpy, st->window, st->bg_gc, + st->lines[i], 0, st->lines[i], st->height); } - free(lines); + if (st->ratio >= 1.0) + { + free (st->lines); + st->lines = 0; + } } static void -venetian (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +venetian (eraser_state *st) { - Bool horiz_p = (random() & 1); - Bool flip_p = (random() & 1); - int max = (horiz_p ? height : width); - int *lines = (int *) calloc(max, sizeof(*lines)); - int i, j; - int oi = -1; - - unsigned long start_tick = millitime(); - unsigned long end_tick = (start_tick + - (1.5 * total_msecs)); /* this one needs more */ - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - - j = 0; - for (i = 0; i < max*2; i++) - { - int line = ((i / 16) * 16) - ((i % 16) * 15); - if (line >= 0 && line < max) - lines[j++] = (flip_p ? max - line : line); - } - - while (tick < end_tick) + int i; + if (st->ratio == 0.0) { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); + int j = 0; + st->horiz_p = (random() & 1); + st->flip_p = (random() & 1); + st->nlines = (st->horiz_p ? st->height : st->width); + st->lines = (int *) calloc (st->nlines, sizeof(*st->lines)); - if (i == oi) + for (i = 0; i < st->nlines * 2; i++) { - usleep (LITTLE_NAP); - nonhits++; + int line = ((i / 16) * 16) - ((i % 16) * 15); + if (line >= 0 && line < st->nlines) + st->lines[j++] = (st->flip_p ? st->nlines - line : line); } + } + + + for (i = st->nlines * st->prev_ratio; + i < st->nlines * st->ratio; + i++) + { + if (st->horiz_p) + XDrawLine (st->dpy, st->window, st->bg_gc, + 0, st->lines[i], st->width, st->lines[i]); else - { - int k; - for (k = oi; k <= i; k++) - { - if (horiz_p) - XDrawLine(dpy,window, gc, 0, lines[k], width, lines[k]); - else - XDrawLine(dpy,window, gc, lines[k], 0, lines[k], height); - hits++; - } - XSync (dpy, False); - } + XDrawLine (st->dpy, st->window, st->bg_gc, + st->lines[i], 0, st->lines[i], st->height); + } - oi = i; - tick = millitime(); + if (st->ratio >= 1.0) + { + free (st->lines); + st->lines = 0; } - free(lines); } static void -triple_wipe (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +triple_wipe (eraser_state *st) { - Bool flip_x = random() & 1; - Bool flip_y = random() & 1; - int max = width + (height / 2); - int *lines = (int *)calloc(max, sizeof(int)); int i; - int oi = -1; - - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - - for(i = 0; i < width/2; i++) - lines[i] = i*2+height; - for(i = 0; i < height/2; i++) - lines[i+width/2] = i*2; - for(i = 0; i < width/2; i++) - lines[i+width/2+height/2] = width-i*2-(width%2?0:1)+height; - - while (tick < end_tick) + if (st->ratio == 0.0) + { + st->flip_x = random() & 1; + st->flip_y = random() & 1; + st->nlines = st->width + (st->height / 2); + st->lines = (int *) calloc (st->nlines, sizeof(*st->lines)); + + for (i = 0; i < st->width / 2; i++) + st->lines[i] = i * 2 + st->height; + for (i = 0; i < st->height / 2; i++) + st->lines[i + st->width / 2] = i*2; + for (i = 0; i < st->width / 2; i++) + st->lines[i + st->width / 2 + st->height / 2] = + st->width - i * 2 - (st->width % 2 ? 0 : 1) + st->height; + } + + for (i = st->nlines * st->prev_ratio; + i < st->nlines * st->ratio; + i++) { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); int x, y, x2, y2; - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } + if (st->lines[i] < st->height) + x = 0, y = st->lines[i], x2 = st->width, y2 = y; else - { - int k; - for (k = oi; k <= i; k++) - { - if (lines[k] < height) - x = 0, y = lines[k], x2 = width, y2 = y; - else - x = lines[k]-height, y = 0, x2 = x, y2 = height; - - if (flip_x) - x = width-x, x2 = width-x2; - if (flip_y) - y = height-y, y2 = height-y2; - - XDrawLine (dpy, window, gc, x, y, x2, y2); - hits++; - } - XSync (dpy, False); - } + x = st->lines[i]-st->height, y = 0, x2 = x, y2 = st->height; + + if (st->flip_x) + x = st->width - x, x2 = st->width - x2; + if (st->flip_y) + y = st->height - y, y2 = st->height - y2; - oi = i; - tick = millitime(); + XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2); + } + + if (st->ratio >= 1.0) + { + free (st->lines); + st->lines = 0; } - free(lines); } static void -quad_wipe (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +quad_wipe (eraser_state *st) { - Bool flip_x = random() & 1; - Bool flip_y = random() & 1; - int max = width + height; - int *lines = (int *)calloc(max, sizeof(int)); int i; - int oi = -1; - - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - - for (i = 0; i < max/4; i++) + if (st->ratio == 0.0) { - lines[i*4] = i*2; - lines[i*4+1] = height-i*2-(height%2?0:1); - lines[i*4+2] = height+i*2; - lines[i*4+3] = height+width-i*2-(width%2?0:1); - } - - while (tick < end_tick) - { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); + st->flip_x = random() & 1; + st->flip_y = random() & 1; + st->nlines = st->width + st->height; + st->lines = (int *) calloc (st->nlines, sizeof(*st->lines)); - if (i == oi) + for (i = 0; i < st->nlines/4; i++) { - usleep (LITTLE_NAP); - nonhits++; + st->lines[i*4] = i*2; + st->lines[i*4+1] = st->height - i*2 - (st->height % 2 ? 0 : 1); + st->lines[i*4+2] = st->height + i*2; + st->lines[i*4+3] = st->height + st->width - i*2 + - (st->width % 2 ? 0 : 1); } + } + + for (i = st->nlines * st->prev_ratio; + i < st->nlines * st->ratio; + i++) + { + int x, y, x2, y2; + if (st->lines[i] < st->height) + x = 0, y = st->lines[i], x2 = st->width, y2 = y; else - { - int k; - for (k = oi; k <= i; k++) - { - int x, y, x2, y2; - if (lines[k] < height) - x = 0, y = lines[k], x2 = width, y2 = y; - else - x = lines[k]-height, y = 0, x2 = x, y2 = height; - - if (flip_x) - x = width-x, x2 = width-x2; - if (flip_y) - y = height-y, y2 = height-y2; - - XDrawLine (dpy, window, gc, x, y, x2, y2); - hits++; - } - XSync (dpy, False); - } + x = st->lines[i] - st->height, y = 0, x2 = x, y2 = st->height; - oi = i; - tick = millitime(); + if (st->flip_x) + x = st->width-x, x2 = st->width-x2; + if (st->flip_y) + y = st->height-y, y2 = st->height-y2; + + XDrawLine (st->dpy, st->window, st->bg_gc, x, y, x2, y2); } - free(lines); + if (st->ratio >= 1.0) + { + free (st->lines); + st->lines = 0; + } } - static void -circle_wipe (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +circle_wipe (eraser_state *st) { + int rad = (st->width > st->height ? st->width : st->height); int max = 360 * 64; - int start = random() % max; - int rad = (width > height ? width : height); - int flip_p = random() & 1; - int oth; - - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - - int hits = 0; - int nonhits = 0; + int th, oth; - oth = (flip_p ? max : 0); - while (tick < end_tick) + if (st->ratio == 0.0) { - int th = (max * (tick - start_tick)) / (end_tick - start_tick); - if (flip_p) - th = (360 * 64) - th; - - if (th == oth) - { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - (start+oth)%(360*64), - (th-oth)); - hits++; - XSync (dpy, False); - } + st->flip_p = random() & 1; + st->start = random() % max; + } - oth = th; - tick = millitime(); + th = max * st->ratio; + oth = max * st->prev_ratio; + if (st->flip_p) + { + th = max - th; + oth = max - oth; } + XFillArc (st->dpy, st->window, st->bg_gc, + (st->width / 2) - rad, + (st->height / 2) - rad, + rad*2, rad*2, + (st->start + oth) % max, + th-oth); } static void -three_circle_wipe (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +three_circle_wipe (eraser_state *st) { - int max = (360 * 64) / 6; - int start = random() % max; - int rad = (width > height ? width : height); - int oth; + int rad = (st->width > st->height ? st->width : st->height); + int max = 360 * 64; + int th, oth; + int i; - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; + if (st->ratio == 0.0) + st->start = random() % max; - int hits = 0; - int nonhits = 0; + th = max/6 * st->ratio; + oth = max/6 * st->prev_ratio; - oth = 0; - while (tick < end_tick) + for (i = 0; i < 3; i++) { - int th = (max * (tick - start_tick)) / (end_tick - start_tick); + int off = i * max / 3; + XFillArc (st->dpy, st->window, st->bg_gc, + (st->width / 2) - rad, + (st->height / 2) - rad, + rad*2, rad*2, + (st->start + off + oth) % max, + th-oth); + XFillArc (st->dpy, st->window, st->bg_gc, + (st->width / 2) - rad, + (st->height / 2) - rad, + rad*2, rad*2, + (st->start + off - oth) % max, + oth-th); + } +} - if (th == oth) - { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - int off = 0; - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - (start+off+oth)%(360*64), - (th-oth)); - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - ((start+off-oth))%(360*64), - -(th-oth)); - - off += max + max; - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - (start+off+oth)%(360*64), - (th-oth)); - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - ((start+off-oth))%(360*64), - -(th-oth)); - - off += max + max; - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - (start+off+oth)%(360*64), - (th-oth)); - XFillArc(dpy, window, gc, - (width/2)-rad, (height/2)-rad, rad*2, rad*2, - ((start+off-oth))%(360*64), - -(th-oth)); - - hits++; - XSync (dpy, False); - } - oth = th; - tick = millitime(); - } +static void +squaretate (eraser_state *st) +{ + int max = ((st->width > st->height ? st->width : st->height) * 2); + XPoint points [3]; + int i = max * st->ratio; + + if (st->ratio == 0.0) + st->flip_p = random() & 1; + +# define DRAW() \ + if (st->flip_p) { \ + points[0].x = st->width - points[0].x; \ + points[1].x = st->width - points[1].x; \ + points[2].x = st->width - points[2].x; \ + } \ + XFillPolygon (st->dpy, st->window, st->bg_gc, \ + points, 3, Convex, CoordModeOrigin) + + points[0].x = 0; + points[0].y = 0; + points[1].x = st->width; + points[1].y = 0; + points[2].x = 0; + points[2].y = points[0].y + ((i * st->height) / max); + DRAW(); + + points[0].x = 0; + points[0].y = 0; + points[1].x = 0; + points[1].y = st->height; + points[2].x = ((i * st->width) / max); + points[2].y = st->height; + DRAW(); + + points[0].x = st->width; + points[0].y = st->height; + points[1].x = 0; + points[1].y = st->height; + points[2].x = st->width; + points[2].y = st->height - ((i * st->height) / max); + DRAW(); + + points[0].x = st->width; + points[0].y = st->height; + points[1].x = st->width; + points[1].y = 0; + points[2].x = st->width - ((i * st->width) / max); + points[2].y = 0; + DRAW(); +# undef DRAW } static void -squaretate (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +fizzle (eraser_state *st) { - int max = ((width > height ? width : width) * 2); - int oi = -1; - Bool flip = random() & 1; - - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - -#define DRAW() \ - if (flip) { \ - points[0].x = width-points[0].x; \ - points[1].x = width-points[1].x; \ - points[2].x = width-points[2].x; } \ - XFillPolygon (dpy, window, gc, points, 3, Convex, CoordModeOrigin) - - while (tick < end_tick) - { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); + XPoint *points; + int chunk = 20000; + int npoints = st->width * st->height * 4; + npoints *= (st->ratio - st->prev_ratio); - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else + points = (XPoint *) malloc (chunk * sizeof(*points)); + if (! points) return; + + while (npoints > 0) + { + int remain = (chunk > npoints ? npoints : chunk); + int i; + for (i = 0; i < remain; i++) { - XPoint points [3]; - points[0].x = 0; - points[0].y = 0; - points[1].x = width; - points[1].y = 0; - points[2].x = 0; - points[2].y = points[0].y + ((i * height) / max); - DRAW(); - - points[0].x = 0; - points[0].y = 0; - points[1].x = 0; - points[1].y = height; - points[2].x = ((i * width) / max); - points[2].y = height; - DRAW(); - - points[0].x = width; - points[0].y = height; - points[1].x = 0; - points[1].y = height; - points[2].x = width; - points[2].y = height - ((i * height) / max); - DRAW(); - - points[0].x = width; - points[0].y = height; - points[1].x = width; - points[1].y = 0; - points[2].x = width - ((i * width) / max); - points[2].y = 0; - DRAW(); - hits++; - XSync (dpy, True); + int r = random(); + points[i].x = r % st->width; + points[i].y = (r >> 16) % st->height; } - - oi = i; - tick = millitime(); + XDrawPoints (st->dpy, st->window, st->bg_gc, + points, remain, CoordModeOrigin); + npoints -= remain; } -#undef DRAW + free (points); } -/* from Frederick Roeber */ static void -fizzle (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +spiral (eraser_state *st) { - /* These dimensions must be prime numbers. They should be roughly the - square root of the width and height. */ -# define BX 41 -# define BY 31 -# define SIZE (BX*BY) - - int array[SIZE]; - int i, j; - int oi = -1; - XPoint *skews; - XPoint points[250]; - int npoints = 0; - int nx, ny; - - unsigned long start_tick = millitime(); - unsigned long end_tick = (start_tick + - (2.5 * total_msecs)); /* this one needs more */ - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - - /* Distribute the numbers [0,SIZE) randomly in the array */ - { - int indices[SIZE]; - - for( i = 0; i < SIZE; i++ ) { - array[i] = -1; - indices[i] = i; - } - - for( i = 0; i < SIZE; i++ ) { - j = random()%(SIZE-i); - array[indices[j]] = i; - indices[j] = indices[SIZE-i-1]; - } - } - - /* nx, ny are the number of cells across and down, rounded up */ - nx = width / BX + (0 == (width %BX) ? 0 : 1); - ny = height / BY + (0 == (height%BY) ? 0 : 1); - skews = (XPoint *)malloc(sizeof(XPoint) * (nx*ny)); - if( (XPoint *)0 != skews ) { - for( i = 0; i < nx; i++ ) { - for( j = 0; j < ny; j++ ) { - skews[j * nx + i].x = random()%BX; - skews[j * nx + i].y = random()%BY; - } + int max_radius = (st->width > st->height ? st->width : st->height) * 0.7; + int loops = 10; + float max_th = M_PI * 2 * loops; + int i; + int steps = 360 * loops / 4; + float off; + + if (st->ratio == 0.0) + { + st->flip_p = random() & 1; + st->start = random() % 360; } - } -# define SKEWX(cx, cy) (((XPoint *)0 == skews)?0:skews[cy*nx + cx].x) -# define SKEWY(cx, cy) (((XPoint *)0 == skews)?0:skews[cy*nx + cx].y) + off = st->start * M_PI / 180; - while (tick < end_tick) + for (i = steps * st->prev_ratio; + i < steps * st->ratio; + i++) { - int i = (SIZE * (tick - start_tick)) / (end_tick - start_tick); + float th1 = i * max_th / steps; + float th2 = (i+1) * max_th / steps; + int r1 = i * max_radius / steps; + int r2 = (i+1) * max_radius / steps; + XPoint points[3]; - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else + if (st->flip_p) { - int j; - for (j = oi; j < i; j++) - { - int x = array[j] % BX; - int y = array[j] / BX; - int iy, cy; - for (iy = 0, cy = 0; iy < height; iy += BY, cy++) - { - int ix, cx; - for( ix = 0, cx = 0; ix < width; ix += BX, cx++ ) { - int xx = ix + (SKEWX(cx, cy) + x*((cx%(BX-1))+1))%BX; - int yy = iy + (SKEWY(cx, cy) + y*((cy%(BY-1))+1))%BY; - if (xx < width && yy < height) - { - points[npoints].x = xx; - points[npoints].y = yy; - if (++npoints == countof(points)) - { - XDrawPoints(dpy, window, gc, points, npoints, - CoordModeOrigin); - XSync (dpy, False); - npoints = 0; - } - } - } - } - } - hits++; + th1 = max_th - th1; + th2 = max_th - th2; } - oi = i; - tick = millitime(); - } - - if (npoints > 100) - { - XDrawPoints(dpy, window, gc, points, npoints, CoordModeOrigin); - XSync (dpy, False); - usleep (10000); + points[0].x = st->width / 2; + points[0].y = st->height / 2; + points[1].x = points[0].x + r1 * cos (off + th1); + points[1].y = points[0].y + r1 * sin (off + th1); + points[2].x = points[0].x + r2 * cos (off + th2); + points[2].y = points[0].y + r2 * sin (off + th2); +/* XFillRectangle(st->dpy, st->window, st->fg_gc,0,0,st->width, st->height);*/ + XFillPolygon (st->dpy, st->window, st->bg_gc, + points, 3, Convex, CoordModeOrigin); } - -# undef SKEWX -# undef SKEWY -# undef BX -# undef BY -# undef SIZE - - if (skews) free(skews); } -/* from Rick Campbell */ static void -spiral (Display *dpy, Window window, GC context, - int width, int height, int total_msecs) +random_squares (eraser_state *st) { - int granularity = 1; /* #### */ - - double pi2 = (M_PI + M_PI); - int loop_count = 10; - int angle_step = 1000 / 8; /* disc granularity is 8 degrees */ - int max = pi2 * angle_step; - double angle; - int arc_limit; - int arc_max_limit; - int length_step; - XPoint points [3]; + int i, size, rows; - total_msecs *= 2.5; /* this one needs more */ - - angle = 0.0; - arc_limit = 1; - arc_max_limit = (ceil (sqrt ((width * width) + (height * height))) / 2.0); - length_step = ((arc_max_limit + loop_count - 1) / loop_count); - arc_max_limit += length_step; - points [0].x = width / 2; - points [0].y = height / 2; - points [1].x = points [0].x + length_step; - points [1].y = points [0].y; - points [2].x = points [1].x; - points [2].y = points [1].y; - - for (arc_limit = length_step; - arc_limit < arc_max_limit; - arc_limit += length_step) + if (st->ratio == 0.0) { - int arc_length = length_step; - int length_base = arc_limit; - - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + (total_msecs / - (arc_max_limit / length_step)); - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - int i = 0; - int oi = -1; - -#if 0 - int max2 = max / granularity; - while (i < max2) -#else - while (tick < end_tick) -#endif + st->cols = 10 + random() % 30; + size = st->width / st->cols; + rows = (size ? (st->height / size) : 0) + 1; + st->nlines = st->cols * rows; + st->lines = (int *) calloc (st->nlines, sizeof(*st->lines)); + + for (i = 0; i < st->nlines; i++) /* every square */ + st->lines[i] = i; + + for (i = 0; i < st->nlines; i++) /* shuffle */ { - i = (max * (tick - start_tick)) / (end_tick - start_tick); - if (i > max) i = max; - - i /= granularity; - - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - int j, k; -#if 0 - for (k = oi; k <= i; k++) -#else - k = i; -#endif - { - for (j = 0; j < granularity; j++) - { - int ii = k * granularity + j; - angle = ii / (double) angle_step; - arc_length = length_base + ((length_step * angle) / pi2); - points [1].x = points [2].x; - points [1].y = points [2].y; - points [2].x = points [0].x + - (int)(cos(angle) * arc_length); - points [2].y = points [0].y + - (int)(sin(angle) * arc_length); - XFillPolygon (dpy, window, context, points, 3, Convex, - CoordModeOrigin); - hits++; - } - } - XSync (dpy, False); - } - - oi = i; - tick = millitime(); + int t, r; + t = st->lines[i]; + r = random() % st->nlines; + st->lines[i] = st->lines[r]; + st->lines[r] = t; } } -} - -#undef MAX -#undef MIN -#define MAX(a,b) ((a)>(b)?(a):(b)) -#define MIN(a,b) ((a)<(b)?(a):(b)) - -/* from David Bagley */ -static void -random_squares(Display * dpy, Window window, GC gc, - int width, int height, int total_msecs) -{ - int granularity = 20; + size = st->width / st->cols; + rows = (size ? (st->height / size) : 0) + 1; - int randsize = MAX(1, MIN(width, height) / (16 + (random() % 32))); - int max = (height / randsize + 1) * (width / randsize + 1); - int *squares = (int *) calloc(max, sizeof (*squares)); - int i; - int columns = width / randsize + 1; /* Add an extra for roundoff */ - - int oi = -1; - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - - for (i = 0; i < max; i++) - squares[i] = i; - - for (i = 0; i < max; i++) + for (i = st->nlines * st->prev_ratio; + i < st->nlines * st->ratio; + i++) { - int t, r; - t = squares[i]; - r = random() % max; - squares[i] = squares[r]; - squares[r] = t; + int x = st->lines[i] % st->cols; + int y = st->lines[i] / st->cols; + XFillRectangle (st->dpy, st->window, st->bg_gc, + st->width * x / st->cols, + st->height * y / rows, + size+1, size+1); } - while (tick < end_tick) + if (st->ratio >= 1.0) { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); - - i /= granularity; - - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - int j; - for (j = 0; j < granularity; j++) - { - int ii = i * granularity + j; - - XFillRectangle(dpy, window, gc, - (squares[ii] % columns) * randsize, - (squares[ii] / columns) * randsize, - randsize, randsize); - hits++; - } - } - XSync (dpy, False); - - oi = i; - tick = millitime(); + free (st->lines); + st->lines = 0; } - free(squares); } + /* I first saw something like this, albeit in reverse, in an early Tetris implementation for the Mac. -- Torbjörn Andersson */ static void -slide_lines (Display *dpy, Window window, GC gc, - int width, int height, int total_msecs) +slide_lines (eraser_state *st) { - int max = width; - int dy = MAX (10, height/40); + int max = st->width * 1.1; + int nlines = 40; + int h = st->height / nlines; + int y, step; + int tick = 0; - int oi = 0; - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + total_msecs; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; + if (h < 10) + h = 10; - while (tick < end_tick) - { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); + step = (max * st->ratio) - (max * st->prev_ratio); + if (step <= 0) + step = 1; - if (i == oi) + for (y = 0; y < st->height; y += h) + { + if (st->width <= step) + ; + else if (tick & 1) { - usleep (LITTLE_NAP); - nonhits++; + XCopyArea (st->dpy, st->window, st->window, st->fg_gc, + 0, y, st->width-step, h, step, y); + XFillRectangle (st->dpy, st->window, st->bg_gc, + 0, y, step, h); } else { - int y; - int tick = 0; - int from1 = oi; - int to1 = i; - int w = width-to1; - int from2 = width - oi - w; - int to2 = width - i - w; - - for (y = 0; y < height; y += dy) - { - if (++tick & 1) - { - XCopyArea (dpy, window, window, gc, from1, y, w, dy, to1, y); - XFillRectangle (dpy, window, gc, from1, y, to1-from1, dy); - } - else - { - XCopyArea (dpy, window, window, gc, from2, y, w, dy, to2, y); - XFillRectangle (dpy, window, gc, from2+w, y, to2-from2, dy); - } - } - - hits++; - XSync (dpy, False); + XCopyArea (st->dpy, st->window, st->window, st->fg_gc, + step, y, st->width-step, h, 0, y); + XFillRectangle (st->dpy, st->window, st->bg_gc, + st->width-step, y, step, h); } - oi = i; - tick = millitime(); + tick++; } } /* from Frederick Roeber */ static void -losira (Display * dpy, Window window, GC gc, - int width, int height, int total_msecs) +losira (eraser_state *st) { - XGCValues gcv; - XWindowAttributes wa; - XColor white; - GC white_gc; - XArc arc[2][8]; - double xx[8], yy[8], dx[8], dy[8]; + double mode1 = 0.55; + double mode2 = mode1 + 0.30; + double mode3 = 1.0; + int radius = 10; - int i; - int oi = 0; - - int max = width/2; - int max_off = MAX(1, max / 12); - - int msecs1 = (0.55 * total_msecs); - int msecs2 = (0.30 * total_msecs); - int msecs3 = (0.15 * total_msecs); - - unsigned long start_tick = millitime(); - unsigned long end_tick = start_tick + msecs1; - unsigned long tick = start_tick; - int hits = 0; - int nonhits = 0; - - XGetWindowAttributes(dpy, window, &wa); - white.flags = DoRed|DoGreen|DoBlue; - white.red = white.green = white.blue = 65535; - XAllocColor(dpy, wa.colormap, &white); - gcv.foreground = white.pixel; - white_gc = XCreateGC(dpy, window, GCForeground, &gcv); - - /* Squeeze in from the sides */ - while (tick < end_tick) + if (st->ratio < mode1) /* squeeze from the sides */ { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); - - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - int off = (max_off * (tick - start_tick)) / (end_tick - start_tick); - - int from1 = oi; - int to1 = i; - int w = max - to1 - off/2 + 1; - int from2 = max+(to1-from1)+off/2; - int to2 = max+off/2; - - if (w < 0) - break; - - XCopyArea (dpy, window, window, gc, from1, 0, w, height, to1, 0); - XCopyArea (dpy, window, window, gc, from2, 0, w, height, to2, 0); - XFillRectangle (dpy, window, gc, from1, 0, (to1-from1), height); - XFillRectangle (dpy, window, gc, to2+w, 0, from2+w, height); - XFillRectangle (dpy, window, white_gc, max-off/2, 0, off, height); - hits++; - XSync(dpy, False); - } - - oi = i; - tick = millitime(); + double ratio = st->ratio / mode1; + double prev_ratio = st->prev_ratio / mode1; + int max = st->width / 2; + int step = (max * ratio) - (max * prev_ratio); + + if (step <= 0) + step = 1; + + /* pull from left */ + XCopyArea (st->dpy, st->window, st->window, st->fg_gc, + 0, 0, max - step, st->height, step, 0); + XFillRectangle (st->dpy, st->window, st->bg_gc, + 0, 0, max * ratio, st->height); + + /* pull from right */ + XCopyArea (st->dpy, st->window, st->window, st->fg_gc, + max+step, 0, max - step, st->height, max, 0); + XFillRectangle (st->dpy, st->window, st->bg_gc, + max + max*(1-ratio), 0, max, st->height); + + /* expand white from center */ + XFillRectangle (st->dpy, st->window, st->fg_gc, + max - (radius * ratio), 0, + radius * ratio * 2, st->height); } - - - XFillRectangle(dpy, window, white_gc, max-max_off/2, 0, max_off, height); - - /* Cap the top and bottom of the line */ - XFillRectangle(dpy, window, gc, max-max_off/2, 0, max_off, max_off/2); - XFillRectangle(dpy, window, gc, max-max_off/2, height-max_off/2, - max_off, max_off/2); - XFillArc(dpy, window, white_gc, max-max_off/2-1, 0, - max_off-1, max_off-1, 0, 180*64); - XFillArc(dpy, window, white_gc, max-max_off/2-1, height-max_off, - max_off-1, max_off-1, - 180*64, 360*64); - - XFillRectangle(dpy, window, gc, 0, 0, max-max_off/2, height); - XFillRectangle(dpy, window, gc, max+max_off/2-1, 0, max-max_off/2, height); - XSync(dpy, False); - - /* Collapse vertically */ - start_tick = millitime(); - end_tick = start_tick + msecs2; - tick = start_tick; - - max = height/2; - oi = 0; - while (tick < end_tick) + else if (st->ratio < mode2) /* squeeze from the top/bottom */ { - int i = (max * (tick - start_tick)) / (end_tick - start_tick); - int x = (width-max_off)/2; - int w = max_off; - - if (i == oi) - { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - int off = (max_off * (tick - start_tick)) / (end_tick - start_tick); - - int from1 = oi; - int to1 = i; - int h = max - to1 - off/2; - int from2 = max+(to1-from1)+off/2; - int to2 = max+off/2; - - if (h < max_off/2) - break; - - XCopyArea (dpy, window, window, gc, x, from1, w, h, x, to1); - XCopyArea (dpy, window, window, gc, x, from2, w, h, x, to2); - XFillRectangle(dpy, window, gc, x, from1, w, (to1 - from1)); - XFillRectangle(dpy, window, gc, x, to2+h, w, (to2 - from2)); - hits++; - XSync(dpy, False); - } - - oi = i; - tick = millitime(); + double ratio = (st->ratio - mode1) / (mode2 - mode1); + int max = st->height / 2; + + /* fill middle */ + XFillRectangle (st->dpy, st->window, st->fg_gc, + st->width/2 - radius, + max * ratio, + radius*2, st->height * (1 - ratio)); + + /* fill left and right */ + XFillRectangle (st->dpy, st->window, st->bg_gc, + 0, 0, st->width/2 - radius, st->height); + XFillRectangle (st->dpy, st->window, st->bg_gc, + st->width/2 + radius, 0, st->width/2, st->height); + + /* fill top and bottom */ + XFillRectangle (st->dpy, st->window, st->bg_gc, + 0, 0, st->width, max * ratio); + XFillRectangle (st->dpy, st->window, st->bg_gc, + 0, st->height - (max * ratio), + st->width, max); + + /* cap top */ + XFillArc (st->dpy, st->window, st->fg_gc, + st->width/2 - radius, + max * ratio - radius, + radius*2, radius*2, + 0, 180*64); + + /* cap bottom */ + XFillArc (st->dpy, st->window, st->fg_gc, + st->width/2 - radius, + st->height - (max * ratio + radius), + radius*2, radius*2, + 180*64, 180*64); } - - /* "This is Sci-Fi" */ - for( i = 0; i < 8; i++ ) { - arc[0][i].width = arc[0][i].height = max_off; - arc[1][i].width = arc[1][i].height = max_off; - arc[0][i].x = arc[1][i].x = width/2; - arc[0][i].y = arc[1][i].y = height/2; - xx[i] = (double)(width/2) - max_off/2; - yy[i] = (double)(height/2) - max_off/2; - } - - arc[0][0].angle1 = arc[1][0].angle1 = 0*64; arc[0][0].angle2 = arc[1][0].angle2 = 45*64; - arc[0][1].angle1 = arc[1][1].angle1 = 45*64; arc[0][1].angle2 = arc[1][1].angle2 = 45*64; - arc[0][2].angle1 = arc[1][2].angle1 = 90*64; arc[0][2].angle2 = arc[1][2].angle2 = 45*64; - arc[0][3].angle1 = arc[1][3].angle1 = 135*64; arc[0][3].angle2 = arc[1][3].angle2 = 45*64; - arc[0][4].angle1 = arc[1][4].angle1 = 180*64; arc[0][4].angle2 = arc[1][4].angle2 = 45*64; - arc[0][5].angle1 = arc[1][5].angle1 = 225*64; arc[0][5].angle2 = arc[1][5].angle2 = 45*64; - arc[0][6].angle1 = arc[1][6].angle1 = 270*64; arc[0][6].angle2 = arc[1][6].angle2 = 45*64; - arc[0][7].angle1 = arc[1][7].angle1 = 315*64; arc[0][7].angle2 = arc[1][7].angle2 = 45*64; - - for( i = 0; i < 8; i++ ) { - dx[i] = cos((i*45 + 22.5)/360 * 2*M_PI); - dy[i] = -sin((i*45 + 22.5)/360 * 2*M_PI); - } - - gcv.line_width = 3; - XChangeGC(dpy, gc, GCLineWidth, &gcv); - - XClearWindow (dpy, window); - XFillArc(dpy, window, white_gc, - width/2-max_off/2-1, height/2-max_off/2-1, - max_off-1, max_off-1, - 0, 360*64); - XDrawLine(dpy, window, gc, 0, height/2-1, width, height/2-1); - XDrawLine(dpy, window, gc, width/2-1, 0, width/2-1, height); - XDrawLine(dpy, window, gc, width/2-1-max_off, height/2-1-max_off, - width/2+max_off, height/2+max_off); - XDrawLine(dpy, window, gc, width/2+max_off, height/2-1-max_off, - width/2-1-max_off, height/2+max_off); - - XSync(dpy, False); - - - /* Fan out */ - start_tick = millitime(); - end_tick = start_tick + msecs3; - tick = start_tick; - oi = 0; - while (tick < end_tick) + else /* starburst */ { - int i = (max_off * (tick - start_tick)) / (end_tick - start_tick); + double ratio = (st->ratio - mode2) / (mode3 - mode2); + double r2 = ratio * radius * 4; + XArc arc[9]; + int i; + int angle = 360*64/countof(arc); - if (i == oi) + for (i = 0; i < countof(arc); i++) { - usleep (LITTLE_NAP); - nonhits++; - } - else - { - int j; - for (j = 0; j < 8; j++) - { - xx[j] += 2*dx[j]; - yy[j] += 2*dy[j]; - arc[(i+1)%2][j].x = xx[j]; - arc[(i+1)%2][j].y = yy[j]; - } - - XFillRectangle (dpy, window, gc, - (width-max_off*5)/2, (height-max_off*5)/2, - max_off*5, max_off*5); - XFillArcs(dpy, window, white_gc, arc[(i+1)%2], 8); - XSync(dpy, False); - hits++; + double th; + arc[i].angle1 = angle * i; + arc[i].angle2 = angle; + arc[i].width = radius*2 * (1 + ratio); + arc[i].height = radius*2 * (1 + ratio); + arc[i].x = st->width / 2 - radius; + arc[i].y = st->height / 2 - radius; + + th = ((arc[i].angle1 + (arc[i].angle2 / 2)) / 64.0 / 180 * M_PI); + + arc[i].x += r2 * cos (th); + arc[i].y -= r2 * sin (th); } - oi = i; - tick = millitime(); + XFillRectangle (st->dpy, st->window, st->bg_gc, + 0, 0, st->width, st->height); + XFillArcs (st->dpy, st->window, st->fg_gc, arc, countof(arc)); } - - XSync (dpy, False); - - /*XFreeColors(dpy, wa.colormap, &white.pixel, 1, 0);*/ - XFreeGC(dpy, white_gc); } - static Eraser erasers[] = { random_lines, venetian, @@ -1063,64 +656,103 @@ static Eraser erasers[] = { }; -static void -erase_window (Display *dpy, Window window, GC gc, - int width, int height, int mode, int total_msecs) +static eraser_state * +eraser_init (Display *dpy, Window window) { - Bool verbose_p = False; - unsigned long start = millitime(); + eraser_state *st = (eraser_state *) calloc (1, sizeof(*st)); + XWindowAttributes xgwa; + XGCValues gcv; + unsigned long fg, bg; + double duration; + int which; + char *s; - if (mode < 0 || mode >= countof(erasers)) - mode = random() % countof(erasers); + st->dpy = dpy; + st->window = window; - (*(erasers[mode])) (dpy, window, gc, width, height, total_msecs); + XGetWindowAttributes (dpy, window, &xgwa); + st->width = xgwa.width; + st->height = xgwa.height; + + bg = get_pixel_resource (dpy, xgwa.colormap, "background", "Background"); + fg = get_pixel_resource (dpy, xgwa.colormap, "foreground", "Foreground"); + + gcv.foreground = fg; + gcv.background = bg; + st->fg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv); + gcv.foreground = bg; + gcv.background = fg; + st->bg_gc = XCreateGC (dpy, window, GCForeground|GCBackground, &gcv); + +# ifdef HAVE_COCOA + /* Pretty much all of these leave turds if AA is on. */ + jwxyz_XSetAntiAliasing (st->dpy, st->fg_gc, False); + jwxyz_XSetAntiAliasing (st->dpy, st->bg_gc, False); +# endif + + s = get_string_resource (dpy, "eraseMode", "Integer"); + if (!s || !*s) + which = -1; + else + which = get_integer_resource(dpy, "eraseMode", "Integer"); + + if (which < 0 || which >= countof(erasers)) + which = random() % countof(erasers); + st->fn = erasers[which]; + + duration = get_float_resource (dpy, "eraseSeconds", "Float"); + if (duration < 0.1 || duration > 10) + duration = 1; + + st->start_time = double_time(); + st->stop_time = st->start_time + duration; - if (verbose_p) - fprintf(stderr, "%s: eraser %d time: %4.2f sec\n", - progname, mode, (millitime() - start) / 1000.0); + XSync (st->dpy, False); - XClearWindow (dpy, window); - XSync(dpy, False); - usleep (333333); /* 1/3 sec */ + return st; } -void -erase_full_window(Display *dpy, Window window) +static Bool +eraser_draw (eraser_state *st, Bool first_p) { - XWindowAttributes xgwa; - XGCValues gcv; - GC erase_gc; - XColor black; - int erase_msecs, erase_mode; - char *s; + double now = (first_p ? st->start_time : double_time()); + double duration = st->stop_time - st->start_time; - s = get_string_resource("eraseSeconds", "Integer"); - if (s && *s) - erase_msecs = 1000 * get_float_resource("eraseSeconds", "Float"); - else - erase_msecs = 1000; + st->prev_ratio = st->ratio; + st->ratio = (now - st->start_time) / duration; - if (erase_msecs < 10 || erase_msecs > 10000) - erase_msecs = 1000; + if (st->ratio > 1.0) + st->ratio = 1.0; - if (s) free(s); + st->fn (st); + XSync (st->dpy, False); - s = get_string_resource("eraseMode", "Integer"); - if (s && *s) - erase_mode = get_integer_resource("eraseMode", "Integer"); - else - erase_mode = -1; - if (s) free(s); + return (st->ratio < 1.0); +} - XGetWindowAttributes (dpy, window, &xgwa); - black.flags = DoRed|DoGreen|DoBlue; - black.red = black.green = black.blue = 0; - XAllocColor(dpy, xgwa.colormap, &black); - gcv.foreground = black.pixel; - erase_gc = XCreateGC (dpy, window, GCForeground, &gcv); - erase_window (dpy, window, erase_gc, xgwa.width, xgwa.height, - erase_mode, erase_msecs); - XFreeColors(dpy, xgwa.colormap, &black.pixel, 1, 0); - XFreeGC(dpy, erase_gc); +static void +eraser_free (eraser_state *st) +{ + XClearWindow (st->dpy, st->window); + XFreeGC (st->dpy, st->fg_gc); + XFreeGC (st->dpy, st->bg_gc); + free (st); +} + +eraser_state * +erase_window (Display *dpy, Window window, eraser_state *st) +{ + Bool first_p = False; + if (! st) + { + first_p = True; + st = eraser_init (dpy, window); + } + if (! eraser_draw (st, first_p)) + { + eraser_free (st); + st = 0; + } + return st; }