1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
/* -*- Mode: C; tab-width: 4 -*- */
/* bounce --- bouncing footballs */
#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)bounce.c 4.07 97/11/24 xlockmore";
#endif
/*-
* Copyright (c) 1988 by Sun Microsystems
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
*
* Revision History:
* 10-May-97: Compatible with xscreensaver
* 01-Apr-97: Curtis Larsen <larsen@rtp3.med.utah.edu>
* The modification is only for the inroot mode. It causes the balls to see
* children of the root window and bounce off of the sides of them. New
* windows are only recognized after every init_bounce, because fvwm did not
* like xlock setting SubstructureNotifyMask on root. I did not fix the
* initial placement of balls yet, so they can start out underneath windows.
* 18-Sep-95: tinkered with it to look like bat.c .
* 15-Jul-94: cleaned up in time for the final match.
* 4-Apr-94: spinning multiple ball version
* (I got a lot of help from with the physics of ball to ball
* collision looking at the source of xpool from Ismail ARIT
* <iarit@tara.mines.colorado.edu>
* 22-Mar-94: got rid of flashing problem by only erasing parts of the image
* that will not be in the next image.
* 2-Sep-93: xlock version David Bagley <bagleyd@bigfoot.com>
* 1986: Sun Microsystems
*/
/*-
* original copyright
* **************************************************************************
* Copyright 1988 by Sun Microsystems, Inc. Mountain View, CA.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the names of Sun or MIT not be used in advertising
* or publicity pertaining to distribution of the software without specific
* prior written permission. Sun and M.I.T. make no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without any express or implied warranty.
*
* SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* IN NO EVENT SHALL SUN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
* ***************************************************************************
*/
/*-
* Open for improvement:
* include different balls (size and mass)
* how about be real crazy and put in an American/Australian football?
* should only have 1 bitmap for ball, the others should be generated
* as 90 degree rotations.
* multiscreen interaction
*/
#ifdef STANDALONE
#define PROGCLASS "Bounce"
#define HACK_INIT init_bounce
#define HACK_DRAW draw_bounce
#define bounce_opts xlockmore_opts
#define DEFAULTS "*delay: 5000 \n" \
"*count: -10 \n" \
"*size: 0 \n" \
"*ncolors: 200 \n"
#include "xlockmore.h" /* in xscreensaver distribution */
#else /* STANDALONE */
#include "xlock.h" /* in xlockmore distribution */
#endif /* STANDALONE */
ModeSpecOpt bounce_opts =
{0, NULL, 0, NULL, NULL};
#ifdef USE_MODULES
ModStruct bounce_description =
{"bounce", "init_bounce", "draw_bounce", "release_bounce",
"refresh_bounce", "init_bounce", NULL, &bounce_opts,
5000, -10, 1, 0, 64, 1.0, "",
"Shows bouncing footballs", 0, NULL};
#endif
#include "bitmaps/bounce-0.xbm"
#include "bitmaps/bounce-1.xbm"
#include "bitmaps/bounce-2.xbm"
#include "bitmaps/bounce-3.xbm"
#include "bitmaps/bounce-mask.xbm"
#define BALLBITS(n,w,h)\
bp->pixmaps[bp->init_orients++]=\
XCreateBitmapFromData(display,window,(char *)n,w,h)
#define MAX_STRENGTH 24
#define FRICTION 24
#define PENETRATION 0.3
#define SLIPAGE 4
#define TIME 32
#define MINBALLS 1
#define MINSIZE 1
#define MINGRIDSIZE 6
#define ORIENTS 4
#define ORIENTCYCLE 16
#define CCW 1
#define CW (ORIENTS-1)
#define DIR(x) (((x)>=0)?CCW:CW)
#define SIGN(x) (((x)>=0)?1:-1)
typedef struct {
int x, y;
int width, height;
} ballwindow;
typedef struct {
int x, y, xlast, ylast, orientlast;
int spincount, spindelay, spindir, orient;
int vx, vy, vang;
unsigned long color;
} ballstruct;
typedef struct {
int width, height;
int nballs;
int xs, ys, avgsize;
int restartnum;
int pixelmode;
ballstruct *balls;
GC stippledGC;
Pixmap pixmaps[ORIENTS + 1];
int init_orients;
int nwindow;
ballwindow *windows;
} bouncestruct;
static bouncestruct *bounces = NULL;
static void
checkCollision(bouncestruct * bp, int aball)
{
int i, amount, spin, d, size;
double x, y;
for (i = 0; i < bp->nballs; i++) {
if (i != aball) {
x = (double) (bp->balls[i].x - bp->balls[aball].x);
y = (double) (bp->balls[i].y - bp->balls[aball].y);
d = (int) sqrt(x * x + y * y);
size = bp->avgsize;
if (d > 0 && d < size) {
amount = size - d;
if (amount > PENETRATION * size)
amount = (int) (PENETRATION * size);
bp->balls[i].vx += (int) ((double) amount * x / d);
bp->balls[i].vy += (int) ((double) amount * y / d);
bp->balls[i].vx -= bp->balls[i].vx / FRICTION;
bp->balls[i].vy -= bp->balls[i].vy / FRICTION;
bp->balls[aball].vx -= (int) ((double) amount * x / d);
bp->balls[aball].vy -= (int) ((double) amount * y / d);
bp->balls[aball].vx -= bp->balls[aball].vx / FRICTION;
bp->balls[aball].vy -= bp->balls[aball].vy / FRICTION;
spin = (bp->balls[i].vang - bp->balls[aball].vang) /
(2 * size * SLIPAGE);
bp->balls[i].vang -= spin;
bp->balls[aball].vang += spin;
bp->balls[i].spindir = DIR(bp->balls[i].vang);
bp->balls[aball].spindir = DIR(bp->balls[aball].vang);
if (!bp->balls[i].vang) {
bp->balls[i].spindelay = 1;
bp->balls[i].spindir = 0;
} else
bp->balls[i].spindelay = (int) ((double) M_PI *
bp->avgsize / (ABS(bp->balls[i].vang))) + 1;
if (!bp->balls[aball].vang) {
bp->balls[aball].spindelay = 1;
bp->balls[aball].spindir = 0;
} else
bp->balls[aball].spindelay = (int) ((double) M_PI *
bp->avgsize / (ABS(bp->balls[aball].vang))) + 1;
return;
}
}
}
}
static void
drawball(ModeInfo * mi, ballstruct * ball)
{
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
if (bp->pixelmode) {
if (ball->xlast != -1) {
XSetForeground(display, MI_GC(mi), MI_BLACK_PIXEL(mi));
XFillRectangle(display, window, MI_GC(mi),
ball->xlast, ball->ylast, bp->xs, bp->ys);
}
XSetForeground(display, MI_GC(mi), ball->color);
XFillRectangle(display, window, MI_GC(mi),
ball->x, ball->y, bp->xs, bp->ys);
} else {
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
if (ball->xlast != -1) {
XSetForeground(display, bp->stippledGC, MI_BLACK_PIXEL(mi));
XSetStipple(display, bp->stippledGC, bp->pixmaps[ORIENTS]);
XSetFillStyle(display, bp->stippledGC, FillStippled);
XSetTSOrigin(display, bp->stippledGC, ball->xlast, ball->ylast);
#ifdef FLASH
XFillRectangle(display, window, bp->stippledGC,
ball->xlast, ball->ylast, bp->xs, bp->ys);
#else
ERASE_IMAGE(display, window, bp->stippledGC, ball->x, ball->y,
ball->xlast, ball->ylast, bp->xs, bp->ys);
#endif
}
XSetTSOrigin(display, bp->stippledGC, ball->x, ball->y);
XSetForeground(display, bp->stippledGC, ball->color);
XSetStipple(display, bp->stippledGC, bp->pixmaps[ball->orient]);
#ifdef FLASH
XSetFillStyle(display, bp->stippledGC, FillStippled);
#else
XSetFillStyle(display, bp->stippledGC, FillOpaqueStippled);
#endif
XFillRectangle(display, window, bp->stippledGC,
ball->x, ball->y, bp->xs, bp->ys);
XFlush(display);
}
}
static void
spinball(ballstruct * ball, int dir, int *vel, int avgsize)
{
*vel -= (int) ((*vel + SIGN(*vel * dir) *
ball->spindelay * ORIENTCYCLE / (M_PI * avgsize)) / SLIPAGE);
if (*vel) {
ball->spindir = DIR(*vel * dir);
ball->vang = *vel * ORIENTCYCLE;
ball->spindelay = (int) ((double) M_PI * avgsize / (ABS(ball->vang))) + 1;
} else
ball->spindir = 0;
}
#define BETWEEN(x, xmin, xmax) (((x) >= (xmin)) && ((x) <= (xmax)))
static void
hit_left_wall(ModeInfo * mi, ballstruct * ball,
int ytop, int height, int x, int side)
{
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
if ((ball->x <= x) && ((ball->xlast >= x) || side) &&
BETWEEN(ball->y, ytop - bp->ys, ytop + height)) {
/* Bounce off the wall to the left of the ball */
ball->x = 2 * x - ball->x;
ball->vx = (ball->vx - (ball->vx * FRICTION)) / FRICTION;
spinball(ball, -1, &ball->vy, bp->avgsize);
}
}
static void
hit_right_wall(ModeInfo * mi, ballstruct * ball,
int ytop, int height, int x, int side)
{
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
x -= bp->xs; /* account for ball width */
if ((ball->x >= x) && ((ball->xlast <= x) || side) &&
BETWEEN(ball->y, ytop - bp->ys, ytop + height)) {
/* Bounce off the wall to the right of the ball */
ball->x = 2 * x - ball->x;
ball->vx = (ball->vx - (ball->vx * FRICTION)) / FRICTION;
spinball(ball, 1, &ball->vy, bp->avgsize);
}
}
static void
hit_top_wall(bouncestruct * bp, ballstruct * ball, int xleft, int width, int y, int side)
{
if ((ball->y <= y) && ((ball->ylast >= y) || side) &&
BETWEEN(ball->x, xleft - bp->xs, xleft + width)) { /* Bounce off the wall to the top of the ball */
ball->y = 2 * y - ball->y;
/* HACK to make it look better for iconified mode */
if (y == 0) {
ball->vy = 0;
} else {
ball->vy = (ball->vy - (FRICTION * ball->vy)) / FRICTION;
}
spinball(ball, 1, &ball->vx, bp->avgsize);
}
}
static void
hit_bottom_wall(bouncestruct * bp, ballstruct * ball, int xleft, int width, int y, int side)
{
y -= bp->ys; /* account for ball height */
if ((ball->y >= y) && ((ball->ylast <= y) || side) &&
BETWEEN(ball->x, xleft - bp->xs, xleft + width)) { /* Bounce off the wall to the bottom of the ball */
ball->y = y;
ball->vy = (ball->vy - (FRICTION * ball->vy)) / FRICTION;
spinball(ball, -1, &ball->vx, bp->avgsize);
}
}
static void
moveball(ModeInfo * mi, ballstruct * ball)
{
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
int i;
ballwindow *win;
ball->xlast = ball->x;
ball->ylast = ball->y;
ball->orientlast = ball->orient;
ball->x += ball->vx;
for (i = 0; i < bp->nwindow; i++) {
win = &bp->windows[i];
hit_left_wall(mi, ball, win->y, win->height, win->x + win->width, 0);
hit_right_wall(mi, ball, win->y, win->height, win->x, 0);
}
hit_right_wall(mi, ball, 0, bp->height, bp->width, 1);
hit_left_wall(mi, ball, 0, bp->height, 0, 1);
ball->vy++;
ball->y += ball->vy;
for (i = 0; i < bp->nwindow; i++) {
win = &bp->windows[i];
hit_top_wall(bp, ball, win->x, win->width, win->y + win->height, 0);
hit_bottom_wall(bp, ball, win->x, win->width, win->y, 0);
}
hit_top_wall(bp, ball, 0, bp->width, 0, 1);
hit_bottom_wall(bp, ball, 0, bp->width, bp->height, 1);
if (ball->spindir) {
ball->spincount--;
if (!ball->spincount) {
ball->orient = (ball->spindir + ball->orient) % ORIENTS;
ball->spincount = ball->spindelay;
}
}
}
static int
collide(bouncestruct * bp, int aball)
{
int i, d, x, y;
for (i = 0; i < aball; i++) {
x = (bp->balls[i].x - bp->balls[aball].x);
y = (bp->balls[i].y - bp->balls[aball].y);
d = (int) sqrt((double) (x * x + y * y));
if (d < bp->avgsize)
return i;
}
return i;
}
static void
bounce_windows(ModeInfo * mi, bouncestruct * bs)
{
Window root, parent, *children;
unsigned int nchildren;
int i;
int n;
if (!MI_IS_INROOT(mi)) {
bs->nwindow = 0;
return;
}
if (XQueryTree(MI_DISPLAY(mi), MI_WINDOW(mi),
&root, &parent, &children, &nchildren) == 0) { /* failure */
bs->nwindow = 0;
return;
}
bs->nwindow = nchildren;
if (bs->windows != NULL) {
(void) free((void *) bs->windows);
}
bs->windows = (ballwindow *) malloc(bs->nwindow * sizeof (ballwindow));
for (n = 0, i = 0; i < bs->nwindow; i++) {
XWindowAttributes att;
/*-
May give
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 3 (X_GetWindowAttributes)
*/
if (XGetWindowAttributes(MI_DISPLAY(mi), children[i], &att) == 0) { /* failure */
bs->nwindow = 0;
return;
}
if ((att.x < 0) || (att.x > bs->width) ||
(att.y < 0) || (att.y > bs->height) ||
#if defined(__cplusplus) || defined(c_plusplus)
(att.c_class != InputOutput) ||
#else
(att.class != InputOutput) ||
#endif
(att.map_state != IsViewable)) {
continue;
}
bs->windows[n].x = att.x;
bs->windows[n].y = att.y;
bs->windows[n].width = att.width;
bs->windows[n].height = att.height;
n++;
}
bs->nwindow = n;
XFree((caddr_t) children);
return;
}
void
init_bounce(ModeInfo * mi)
{
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
int size = MI_SIZE(mi);
bouncestruct *bp;
XGCValues gcv;
int i, tryagain = 0;
if (bounces == NULL) {
if ((bounces = (bouncestruct *) calloc(MI_NUM_SCREENS(mi),
sizeof (bouncestruct))) == NULL)
return;
}
bp = &bounces[MI_SCREEN(mi)];
if (!bp->stippledGC) {
gcv.foreground = MI_BLACK_PIXEL(mi);
gcv.background = MI_BLACK_PIXEL(mi);
if ((bp->stippledGC = XCreateGC(display, window,
GCForeground | GCBackground, &gcv)) == None)
return;
}
if (!bp->init_orients) {
BALLBITS(bounce0_bits, bounce0_width, bounce0_height);
BALLBITS(bounce1_bits, bounce1_width, bounce1_height);
BALLBITS(bounce2_bits, bounce2_width, bounce2_height);
BALLBITS(bounce3_bits, bounce3_width, bounce3_height);
BALLBITS(bouncemask_bits, bouncemask_width, bouncemask_height);
}
bp->width = MI_WIDTH(mi);
bp->height = MI_HEIGHT(mi);
if (bp->width < 2)
bp->width = 2;
if (bp->height < 2)
bp->height = 2;
bp->restartnum = TIME;
bounce_windows(mi, bp);
bp->nballs = MI_COUNT(mi);
if (bp->nballs < -MINBALLS) {
/* if bp->nballs is random ... the size can change */
if (bp->balls != NULL) {
(void) free((void *) bp->balls);
bp->balls = NULL;
}
bp->nballs = NRAND(-bp->nballs - MINBALLS + 1) + MINBALLS;
} else if (bp->nballs < MINBALLS)
bp->nballs = MINBALLS;
if (!bp->balls)
bp->balls = (ballstruct *) malloc(bp->nballs * sizeof (ballstruct));
if (size == 0 ||
MINGRIDSIZE * size > bp->width || MINGRIDSIZE * size > bp->height) {
if (bp->width > MINGRIDSIZE * bounce0_width &&
bp->height > MINGRIDSIZE * bounce0_height) {
bp->pixelmode = False;
bp->xs = bounce0_width;
bp->ys = bounce0_height;
} else {
bp->pixelmode = True;
bp->ys = MAX(MINSIZE, MIN(bp->width, bp->height) / MINGRIDSIZE);
bp->xs = bp->ys;
}
} else {
bp->pixelmode = True;
if (size < -MINSIZE)
bp->ys = NRAND(MIN(-size, MAX(MINSIZE, MIN(bp->width, bp->height) /
MINGRIDSIZE)) - MINSIZE + 1) + MINSIZE;
else if (size < MINSIZE)
bp->ys = MINSIZE;
else
bp->ys = MIN(size, MAX(MINSIZE, MIN(bp->width, bp->height) /
MINGRIDSIZE));
bp->xs = bp->ys;
}
bp->avgsize = (bp->xs + bp->ys) / 2;
i = 0;
while (i < bp->nballs) {
bp->balls[i].vx = ((LRAND() & 1) ? -1 : 1) * (NRAND(MAX_STRENGTH) + 1);
bp->balls[i].x = (bp->balls[i].vx >= 0) ? 0 : bp->width - bp->xs;
bp->balls[i].y = NRAND(bp->height / 2);
if (i == collide(bp, i) || tryagain >= 8) {
if (MI_NPIXELS(mi) > 2)
bp->balls[i].color =
MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
else
bp->balls[i].color = MI_WHITE_PIXEL(mi);
bp->balls[i].xlast = -1;
bp->balls[i].ylast = 0;
bp->balls[i].orientlast = 0;
bp->balls[i].spincount = 1;
bp->balls[i].spindelay = 1;
bp->balls[i].vy = ((LRAND() & 1) ? -1 : 1) * NRAND(MAX_STRENGTH);
bp->balls[i].spindir = 0;
bp->balls[i].vang = 0;
bp->balls[i].orient = NRAND(ORIENTS);
i++;
} else
tryagain++;
}
MI_CLEARWINDOW(mi);
}
void
draw_bounce(ModeInfo * mi)
{
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
int i;
MI_IS_DRAWN(mi) = True;
for (i = 0; i < bp->nballs; i++) {
drawball(mi, &bp->balls[i]);
moveball(mi, &bp->balls[i]);
}
for (i = 0; i < bp->nballs; i++)
checkCollision(bp, i);
if (!NRAND(TIME)) /* Put some randomness into the time */
bp->restartnum--;
if (!bp->restartnum)
init_bounce(mi);
}
void
release_bounce(ModeInfo * mi)
{
if (bounces != NULL) {
int screen;
for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
bouncestruct *bp = &bounces[screen];
int bits;
if (bp->balls != NULL)
(void) free((void *) bp->balls);
if (bp->stippledGC != NULL)
XFreeGC(MI_DISPLAY(mi), bp->stippledGC);
for (bits = 0; bits < bp->init_orients; bits++)
XFreePixmap(MI_DISPLAY(mi), bp->pixmaps[bits]);
}
(void) free((void *) bounces);
bounces = NULL;
}
}
void
refresh_bounce(ModeInfo * mi)
{
bouncestruct *bp = &bounces[MI_SCREEN(mi)];
MI_CLEARWINDOW(mi);
bounce_windows(mi, bp);
}
|