swarm.c revision 943
98N/A/*
98N/A * Copyright (c) 1988-91 by Patrick J. Naughton.
199N/A *
98N/A * Permission to use, copy, modify, and distribute this software and its
98N/A * documentation for any purpose and without fee is hereby granted,
98N/A * provided that the above copyright notice appear in all copies and that
98N/A * both that copyright notice and this permission notice appear in
98N/A * supporting documentation.
98N/A *
98N/A * This file is provided AS IS with no warranties of any kind. The author
98N/A * shall have no liability with respect to the infringement of copyrights,
98N/A * trade secrets or any patents by this file or any part thereof. In no
98N/A * event will the author be liable for any lost revenue or profits or
98N/A * other special, indirect and consequential damages.
98N/A */
98N/A
98N/A/*
98N/A * Copyright (c) 1990, 1994, Oracle and/or its affiliates. All rights reserved.
98N/A *
98N/A * Permission is hereby granted, free of charge, to any person obtaining a
98N/A * copy of this software and associated documentation files (the "Software"),
98N/A * to deal in the Software without restriction, including without limitation
98N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
98N/A * and/or sell copies of the Software, and to permit persons to whom the
98N/A * Software is furnished to do so, subject to the following conditions:
98N/A *
98N/A * The above copyright notice and this permission notice (including the next
98N/A * paragraph) shall be included in all copies or substantial portions of the
98N/A * Software.
98N/A *
98N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
98N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
199N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
98N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
98N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
199N/A * DEALINGS IN THE SOFTWARE.
98N/A */
98N/A/*-
199N/A * swarm.c - swarm of bees for xlock, the X Window System lockscreen.
98N/A *
98N/A * Copyright (c) 1991 by Patrick J. Naughton.
199N/A *
199N/A * Revision History:
98N/A * 31-Aug-90: Adapted from xswarm by Jeff Butterworth. (butterwo@ncsc.org)
98N/A */
98N/A
98N/A#include "xlock.h"
98N/A
98N/A#define TIMES 4 /* number of time positions recorded */
98N/A#define BEEACC 3 /* acceleration of bees */
98N/A#define WASPACC 5 /* maximum acceleration of wasp */
98N/A#define BEEVEL 11 /* maximum bee velocity */
98N/A#define WASPVEL 12 /* maximum wasp velocity */
98N/A#define BORDER 50 /* wasp won't go closer than this to the edge */
98N/A
98N/A/* Macros */
98N/A#define X(t,b) (sp->x[(t)*sp->beecount+(b)])
98N/A#define Y(t,b) (sp->y[(t)*sp->beecount+(b)])
98N/A#define RAND(v) ((random()%(v))-((v)/2)) /* random number around 0 */
98N/A
98N/Atypedef struct {
98N/A int pix;
199N/A long startTime;
199N/A int width;
199N/A int height;
98N/A int beecount; /* number of bees */
98N/A XSegment *segs; /* bee lines */
98N/A XSegment *old_segs; /* old bee lines */
199N/A short *x;
short *y; /* bee positions x[time][bee#] */
short *xv;
short *yv; /* bee velocities xv[bee#] */
short wx[3];
short wy[3];
short wxv;
short wyv;
} swarmstruct;
extern XColor ssblack[];
extern XColor sswhite[];
static swarmstruct swarms[MAXSCREENS];
void
initswarm(win)
Window win;
{
XWindowAttributes xgwa;
swarmstruct *sp = &swarms[screen];
int b;
sp->startTime = seconds();
sp->beecount = batchcount;
XGetWindowAttributes(dsp, win, &xgwa);
sp->width = xgwa.width;
sp->height = xgwa.height;
/* Clear the background. */
XSetForeground(dsp, Scr[screen].gc, ssblack[screen].pixel);
XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, sp->width, sp->height);
/* Allocate memory. */
if (!sp->segs) {
sp->segs = (XSegment *) malloc(sizeof(XSegment) * sp->beecount);
sp->old_segs = (XSegment *) malloc(sizeof(XSegment) * sp->beecount);
sp->x = (short *) malloc(sizeof(short) * sp->beecount * TIMES);
sp->y = (short *) malloc(sizeof(short) * sp->beecount * TIMES);
sp->xv = (short *) malloc(sizeof(short) * sp->beecount);
sp->yv = (short *) malloc(sizeof(short) * sp->beecount);
}
/* Initialize point positions, velocities, etc. */
/* wasp */
sp->wx[0] = BORDER + random() % (sp->width - 2 * BORDER);
sp->wy[0] = BORDER + random() % (sp->height - 2 * BORDER);
sp->wx[1] = sp->wx[0];
sp->wy[1] = sp->wy[0];
sp->wxv = 0;
sp->wyv = 0;
/* bees */
for (b = 0; b < sp->beecount; b++) {
X(0, b) = random() % sp->width;
X(1, b) = X(0, b);
Y(0, b) = random() % sp->height;
Y(1, b) = Y(0, b);
sp->xv[b] = RAND(7);
sp->yv[b] = RAND(7);
}
}
void
drawswarm(win)
Window win;
{
swarmstruct *sp = &swarms[screen];
int b;
/* <=- Wasp -=> */
/* Age the arrays. */
sp->wx[2] = sp->wx[1];
sp->wx[1] = sp->wx[0];
sp->wy[2] = sp->wy[1];
sp->wy[1] = sp->wy[0];
/* Accelerate */
sp->wxv += RAND(WASPACC);
sp->wyv += RAND(WASPACC);
/* Speed Limit Checks */
if (sp->wxv > WASPVEL)
sp->wxv = WASPVEL;
if (sp->wxv < -WASPVEL)
sp->wxv = -WASPVEL;
if (sp->wyv > WASPVEL)
sp->wyv = WASPVEL;
if (sp->wyv < -WASPVEL)
sp->wyv = -WASPVEL;
/* Move */
sp->wx[0] = sp->wx[1] + sp->wxv;
sp->wy[0] = sp->wy[1] + sp->wyv;
/* Bounce Checks */
if ((sp->wx[0] < BORDER) || (sp->wx[0] > sp->width - BORDER - 1)) {
sp->wxv = -sp->wxv;
sp->wx[0] += sp->wxv;
}
if ((sp->wy[0] < BORDER) || (sp->wy[0] > sp->height - BORDER - 1)) {
sp->wyv = -sp->wyv;
sp->wy[0] += sp->wyv;
}
/* Don't let things settle down. */
sp->xv[random() % sp->beecount] += RAND(3);
sp->yv[random() % sp->beecount] += RAND(3);
/* <=- Bees -=> */
for (b = 0; b < sp->beecount; b++) {
int distance,
dx,
dy;
/* Age the arrays. */
X(2, b) = X(1, b);
X(1, b) = X(0, b);
Y(2, b) = Y(1, b);
Y(1, b) = Y(0, b);
/* Accelerate */
dx = sp->wx[1] - X(1, b);
dy = sp->wy[1] - Y(1, b);
distance = abs(dx) + abs(dy); /* approximation */
if (distance == 0)
distance = 1;
sp->xv[b] += (dx * BEEACC) / distance;
sp->yv[b] += (dy * BEEACC) / distance;
/* Speed Limit Checks */
if (sp->xv[b] > BEEVEL)
sp->xv[b] = BEEVEL;
if (sp->xv[b] < -BEEVEL)
sp->xv[b] = -BEEVEL;
if (sp->yv[b] > BEEVEL)
sp->yv[b] = BEEVEL;
if (sp->yv[b] < -BEEVEL)
sp->yv[b] = -BEEVEL;
/* Move */
X(0, b) = X(1, b) + sp->xv[b];
Y(0, b) = Y(1, b) + sp->yv[b];
/* Fill the segment lists. */
sp->segs[b].x1 = X(0, b);
sp->segs[b].y1 = Y(0, b);
sp->segs[b].x2 = X(1, b);
sp->segs[b].y2 = Y(1, b);
sp->old_segs[b].x1 = X(1, b);
sp->old_segs[b].y1 = Y(1, b);
sp->old_segs[b].x2 = X(2, b);
sp->old_segs[b].y2 = Y(2, b);
}
XSetForeground(dsp, Scr[screen].gc, ssblack[screen].pixel);
XDrawLine(dsp, win, Scr[screen].gc,
sp->wx[1], sp->wy[1], sp->wx[2], sp->wy[2]);
XDrawSegments(dsp, win, Scr[screen].gc, sp->old_segs, sp->beecount);
XSetForeground(dsp, Scr[screen].gc, sswhite[screen].pixel);
XDrawLine(dsp, win, Scr[screen].gc,
sp->wx[0], sp->wy[0], sp->wx[1], sp->wy[1]);
if (!mono && Scr[screen].npixels > 2) {
XSetForeground(dsp, Scr[screen].gc, Scr[screen].pixels[sp->pix]);
if (++sp->pix >= Scr[screen].npixels)
sp->pix = 0;
}
XDrawSegments(dsp, win, Scr[screen].gc, sp->segs, sp->beecount);
}