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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
|
/**********************************************************************
* item.c - defines the item object
*
* Copyright 1993, David Nedde
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is granted
* provided that the above copyright notice appears in all copies.
* It is provided "as is" without express or implied warranty.
**********************************************************************/
/* System Headers */
#include <stdlib.h>
#include <X11/StringDefs.h>
#ifdef MOTIF
#include <Xm/ToggleB.h>
#endif
/* Local headers */
#include "item.h"
#include "room.h"
#include "scrollbar.h"
#include "color_l.h"
#include "menu.h"
#include "misc.h"
#include "stipple.xbm"
/* Macros */
#define SHADES 4 /* Number of shades for colors in the rgb.txt file */
#define ITEM_WIDTH 10 /* The item's default size */
#define ITEM_HEIGHT 10
#ifndef RGB_TXT
#define RGB_TXT "/etc/X11/rgb.txt"
#endif
/* put_pixmap - Draws the item's pixmap to the specified location */
#define put_pixmap( item, x, y) \
XCopyArea(item->display, item->pixmap, item->window, \
item->static_data->gc, \
0, 0, \
item->static_data->width, \
item->static_data->height, \
intf2int(x) - item->static_data->half_width, \
intf2int(y) - item->static_data->half_height)
/* File scope variables */
static item_static_struct_type static_struct; /* Item object static data */
item_static_type static_data = &static_struct;
static Bool static_data_initted = False;
/* Resources */
typedef struct {
int item_width;
int item_height;
char * rgb_txt;
int elasticity;
} app_data,*app_data_ptr;
static app_data res_data;
static XtResource resources[] = {
{ "itemWidth", "ItemWidth", XtRInt, sizeof(int),
XtOffset(app_data_ptr,item_width), XtRImmediate, (caddr_t)ITEM_WIDTH },
{ "itemHeight", "ItemHeight", XtRInt, sizeof(int),
XtOffset(app_data_ptr,item_height),XtRImmediate, (caddr_t)ITEM_HEIGHT},
{ "rgbTxt", "RgbTxt", XtRString, sizeof(char *),
XtOffset(app_data_ptr,rgb_txt), XtRString, RGB_TXT}
};
/* Functions */
static item_static_type static_data__create(/*display, window, background*/);
static void draw_item(/* static_data, display, clear_gc, colors,
pixmap, background*/);
static void draw_circle(/* display, pixmap,
x_offset, y_offset,
width, height*/);
static void draw_black_ball(/* display, background*/);
static void draw_ball(/* display, pixmap, x_offset, y_offset,
width, height*/);
/* Public object methods */
/* Create the item object. The item is the thing that bounces on the screen */
/* The item is drawn to the screen after it is created */
item_type item__create( x, y, x_vel, y_vel)
int x;
int y;
int x_vel;
int y_vel;
{
item_type item;
item = (item_type)malloc( sizeof( item_struct_type));
item->static_data = static_data;
item->display = static_data->display;
item->window = static_data->window;
/* Draw with the next avaliable pixmap image (color) */
static_data->curr_pixmap =
(static_data->curr_pixmap + 1) % static_data->num_colors;
item->pixmap = static_data->pixmaps[ static_data->curr_pixmap];
/* Position are stored as intf (int-floats) to get more precision
out of an int */
item->x = int2intf( x);
item->y = int2intf( y);
item->x_vel = int2intf( x_vel);
item->y_vel = int2intf( y_vel);
item->shown = False;
item->rebounded = False;
item__draw( item);
#if 0
/* To generate demos */
printf("create %d,%d,%d,%d\n",
intf2int(item->x),intf2int(item->y),
intf2int(item->x_vel),intf2int(item->y_vel));
#endif
return item;
}
/* Initializes the item's static data. This can only be done once
there is a window, sice we have to get the background color. Thus, we
are called from room's first refresh callback */
void item__init(toplevel, display, window, background)
Widget toplevel;
Display * display;
Window window;
Pixel background;
{
static_data = static_data__create( toplevel, display, window, background);
static_data_initted = True;
}
/* Returns true of the item's static data is initialized */
Bool item__initted()
{
return static_data_initted;
}
/* Destroys an item */
void item__destroy( item)
item_type item;
{
item__undraw( item, item->x, item->y); /* Erase item from screen */
free( (char *)item);
}
/* Draws the item to the screen if it is visible. Handles the case
where the item was already visible on the screen. */
void item__draw( item)
item_type item;
{
if (!item->shown && item->static_data->visible)
{
put_pixmap( item, item->x, item->y);
item->shown = True;
}
}
/* Erases the item from the screen */
void item__undraw( item, x, y)
item_type item;
intf x,y;
{
if (item->shown && item->static_data->visible)
{
put_pixmap( item, x, y);
item->shown = False;
}
}
/* Redraws the item on the screen, regardless of whether it is shown.
This method shoulw be used when the window has to be refreshed.
invisible items will not be drawn. */
void item__redraw( item)
item_type item;
{
if (item->static_data->visible)
{
put_pixmap( item, item->x, item->y);
item->shown = True;
}
}
/* Move an item's image to a new location */
void item__move_pos( item, x, y)
item_type item;
int x,y;
{
item__undraw( item, item->x, item->y);
item->x = int2intf(x);
item->y = int2intf(y);
item__draw( item);
}
/* Give an item a random velocity, based on the passed ranges */
void item__randomize( item, min_x_vel, max_x_vel, min_y_vel, max_y_vel)
item_type item;
int min_x_vel;
int max_x_vel;
int min_y_vel;
int max_y_vel;
{
item->x_vel = int2intf(rand_range( min_x_vel, max_x_vel));
item->y_vel = int2intf(rand_range( min_y_vel, max_y_vel));
}
/* Perform one item movement iteration, taking into account other
balls, gravity, elasticity, and the room boundaries. The item's new
position is calculated and the item's image is moved to the new
location. Invisible items are not redrawn. */
void item__move( item, room, items)
item_type item;
room_type room;
items_type items;
{
/* Calculate new position of item */
intf oldx = item->x;
intf oldy = item->y;
/* Calculate new y position */
/* Gravity adds to velocity */
if ((item->y < room__get_floor( room)) || (item->y_vel != 0))
{
item->y_vel += room__get_gravity( room);
}
/* Move vertically based on velocity */
item->y += item->y_vel;
if (item->y >= room__get_floor( room))
{
/* item hit floor -- bounce off floor */
/* Reverse ball velocity */
item->y_vel = -item->y_vel;
item->y += item->y_vel;
if (ABS(item->y_vel) < room__get_gravity(room))
/* This helps dampen rounding errors that cause balls to
bounce forever */
item->y_vel = 0;
else
{
/* Ball velocity is reduced by the percentage elasticity */
item->y_vel *= item->static_data->elasticity;
#if 1
item->y = room__get_floor( room) -
(room__get_floor( room) - item->y) *
item->static_data->elasticity;
#else
item->y = -item->y + room__get_floor( room)*2;
#endif
}
}
else
if (item->y < room__get_ceiling( room))
{
/* Reverse ball velocity */
item->y_vel = -item->y_vel;
/* Ball velocity is reduced by the percentage elasticity */
item->y_vel *= item->static_data->elasticity;
/* Bounce off the wall */
item->y = -item->y + room__get_ceiling( room)*2;
}
/* Calculate new x position */
/* Move horizontally based on velocity */
item->x += item->x_vel;
if (item->x > room__get_right_wall(room))
{
/* Hit right wall */
/* Reverse ball velocity */
item->x_vel = -item->x_vel;
/* Ball velocity is reduced by the percentage elasticity */
item->x_vel *= item->static_data->elasticity;
/* Bounce off the wall */
item->x = -item->x + room__get_right_wall( room)*2;
}
else
if (item->x < room__get_left_wall( room))
{
/* Hit left wall */
/* Reverse ball velocity */
item->x_vel = -item->x_vel;
/* Ball velocity is reduced by the percentage elasticity */
item->x_vel *= item->static_data->elasticity;
/* Bounce off the wall */
item->x = -item->x + room__get_left_wall( room)*2;
}
/* Slow ball if it is rolling on the floor */
if (ABS(item->y) >= room__get_floor(room) - room__get_gravity(room) &&
item->y_vel == 0)
item->x_vel *= item->static_data->elasticity;
/* Collide with other balls or ball being created */
if (item->static_data->ball_collide)
{
/* See if collided with the ball under the pointer being created */
item_type room_item = room__get_item( room);
if (room_item != (item_type)0)
{
item__rebound_item(item, room_item);
}
/* See if collided with another item */
items__rebound_items( item, items);
}
/* See if item has come to a peaceful rest */
if (item__stopped(item, room))
{
/* on floor && Not bouncing */
if (item->static_data->perpetual)
{
/* Just drop the ball from the ceiling */
item->y = room__get_ceiling( room);
}
else
{
if (item__stopped( item, room))
{
item__undraw( item, oldx, oldy);
return; /* Don't draw item */
}
/* Slow down velocity once rolling based on elasticity */
if (item->x_vel > 0)
item->x_vel *= item->static_data->elasticity;
}
}
/* If item moved, redraw it in the new position */
if ((item->y != oldy || item->x != oldx) && static_data->visible)
{
/* Erase old object */
item__undraw( item, oldx, oldy);
item__draw( item);
}
}
/* See if items have hit and rebound them if they have.
Moved_item is assumed to have just been moved.
Only the moved item will have its x,y positions potentially changed. */
void item__rebound_item( moved_item, fixed_item)
item_type moved_item;
item_type fixed_item;
{
intf xdiff,ydiff;
xdiff = moved_item->x - fixed_item->x;
ydiff = moved_item->y - fixed_item->y;
if (ABS(xdiff) < int2intf(moved_item->static_data->width) &&
ABS(ydiff) < int2intf(moved_item->static_data->height))
{
SWAP( moved_item->x_vel, fixed_item->x_vel, intf);
SWAP( moved_item->y_vel, fixed_item->y_vel, intf);
if (moved_item->y_vel <= int2intf(moved_item->static_data->height) &&
fixed_item->y_vel <= int2intf(moved_item->static_data->height))
{
moved_item->y += moved_item->y_vel;
moved_item->y_vel += ydiff * moved_item->static_data->elasticity/10;
fixed_item->y_vel -= ydiff * moved_item->static_data->elasticity/10;
}
if (moved_item->x_vel <= int2intf(moved_item->static_data->width) &&
fixed_item->x_vel <= int2intf(moved_item->static_data->width))
{
moved_item->x += moved_item->x_vel;
moved_item->x_vel += xdiff * moved_item->static_data->elasticity/10;
fixed_item->x_vel -= xdiff * moved_item->static_data->elasticity/10;
}
}
}
/* A scrollbar callback method that sets all the balls' elasticity */
void item__set_elasticity_cb(w, closure, call_data)
Widget w;
caddr_t closure;
scrollbarCallbackStruct * call_data;
{
static_data->elasticity = call_data->value / 100.0;
}
/* Return all items' width */
int item__get_width()
{
return static_data->width;
}
/* Return all items' height */
int item__get_height()
{
return static_data->height;
}
/* A menu callback (mcb) that sets all items' perpetual status */
void item__perpetual_mcb(w, closure, call_data, extra)
Widget w;
caddr_t closure;
menuCallbackStruct * call_data;
char * extra;
{
static_data->perpetual = call_data->set;
}
/* A menu callback (mcb) that sets all items' collide status */
void item__collide_mcb(w, closure, call_data, extra)
Widget w;
caddr_t closure;
menuCallbackStruct * call_data;
char *extra;
{
static_data->ball_collide = call_data->set;
}
/* Semi-private object methods */
/* Sets all items' perpetual status */
/* Note that the toggle must be set properly also */
void item__set_perpetual(set)
Bool set;
{
static_data->perpetual = set;
}
/* Sets all items' collide status */
/* Note that the toggle must be set properly also */
void item__set_ball_collide(set)
Bool set;
{
static_data->ball_collide = set;
}
/* Set all item's visible status */
/* Note that all items must be undrawn from the screen */
void item__set_visible(visible)
Bool visible;
{
static_data->visible = visible;
}
/* Private object methods */
/* Load items resources */
static void get_resources( toplevel, display, window, background)
Widget toplevel;
Display *display;
Window window;
Pixel background;
{
XtGetApplicationResources(toplevel, (XtPointer)&res_data,
resources, XtNumber(resources),
(ArgList)NULL,(Cardinal)0);
static_data->width = res_data.item_width;
static_data->height = res_data.item_height;
static_data->half_width = static_data->width / 2;
static_data->half_height = static_data->height / 2;
static_data->rgb_txt = res_data.rgb_txt;
}
/* Initialize the item's static data object */
static item_static_type static_data__create( toplevel, display, window,
background)
Widget toplevel;
Display * display;
Window window;
Pixel background;
{
GC clear_gc;
/* The following already set in xball_sys object:
static_data->elasticity
static_data->ball_collide
static_data->perpetual */
get_resources( toplevel, display, window, background);
static_data->visible = True;
static_data->background = background;
static_data->display = display;
static_data->window = window;
static_data->gc = XCreateGC(display, window, 0, 0);
clear_gc = XCreateGC(display, window, 0, 0);
static_data->curr_pixmap = 0;
if (DisplayCells( display, DefaultScreen(display)) > 2)
{
/* Non-monochrome system */
color_list_type color_list = color_list__create(static_data->rgb_txt);
color_type color;
Colormap colormap = DefaultColormap(display, DefaultScreen(display));
XColor colors[ SHADES]; /* The color shades */
Pixel cells[ SHADES];
int pixmap_index;
int shade;
for (shade = 0; shade < SHADES; shade++)
colors[ shade].flags = DoRed | DoGreen | DoBlue;
for (color = color_list__get_last( color_list), pixmap_index = 0;
color != (color_type)0;
pixmap_index++)
{
if (!XAllocColorCells( display, colormap, /*contig =*/True,
/*plane_masks=*/NULL, /*nplanes=*/0,
/*pixels =*/cells, /*ncolors=*/SHADES))
break; /* All out of colors */
for (shade = SHADES-1; shade >= 0; shade--)
{
colors[ shade].red = color->r * 65535 / 255;
colors[ shade].green = color->g * 65535 / 255;
colors[ shade].blue = color->b * 65535 / 255;
colors[ shade].pixel = cells[ shade];
color = color_list__get_prev( color_list);
}
if (shade == -1)
{
XStoreColors( display, colormap, colors, SHADES);
}
else
{
/* Problem with color list */
pixmap_index--;
XFreeColors( display, colormap, cells, SHADES,
/*nplanes=*/0);
break;
}
static_data->pixmaps[ pixmap_index] =
XCreatePixmap(display, window,
static_data->width,static_data->height,
DefaultDepth(display, DefaultScreen(display)));
/* Draw the item onto the pixmap */
draw_item(static_data, display, clear_gc, colors,
static_data->pixmaps[ pixmap_index], background);
}
/* printf("Created %d different colored balls (%d possible).\n",
pixmap_index, color_list__get_count( color_list) / 4); */
color_list__destroy( color_list);
static_data->num_colors = pixmap_index;
}
if ((static_data->num_colors == 0) ||
(DisplayCells( display, DefaultScreen(display)) == 2))
{
/* Monochrome system or cannot allocate colors -
just have 1 pixmap of a black ball */
draw_black_ball( display, background);
}
XFreeGC( display, clear_gc);
XSetFunction( display, static_data->gc, GXxor);
return static_data;
}
/* Draw an item in the passed colors on the passed pixmap */
static void draw_item( static_data, display, clear_gc, colors, pixmap,
background)
item_static_type static_data;
Display *display;
GC clear_gc;
XColor *colors;
Pixmap pixmap;
Pixel background;
{
static struct
{
double x_offset, y_offset; /* The circle's offset on the item */
double width, height; /* The width & height of the circle */
} offsets[SHADES-1] =
{
0.2, 0.2, 0.3, 0.3,
0.1, 0.1, 0.5, 0.5,
0.0, 0.0, 0.8, 0.8
};
int x;
/* Clear pixmap */
XFillRectangle(display, pixmap, clear_gc, 0, 0,
static_data->width,
static_data->height);
XSetForeground( display, static_data->gc,
colors[SHADES-1].pixel ^ background);
draw_circle(display, pixmap, static_data->gc,
0, 0, static_data->width-1, static_data->height-1);
/* Draw the circles in the different shades. Shade SHADES-1 is darkest. */
for (x = SHADES-2; x >= 0; x--)
{
XSetForeground(display, static_data->gc, colors[x].pixel ^ background);
draw_ball( display, pixmap,
offsets[x].x_offset, offsets[x].y_offset,
offsets[x].width, offsets[x].height);
}
/* Draw a white specularity smallest and offset */
XSetForeground(display, static_data->gc,
WhitePixel(display, DefaultScreen(display)) ^ background);
draw_ball( display, pixmap, 0.3, 0.3, 0.1, 0.1);
/* Draw a little black shadow in the lower right corner */
XSetForeground(display, static_data->gc,
BlackPixel(display, DefaultScreen(display)) ^ background);
XDrawArc(display, pixmap, static_data->gc, 0, 0,
static_data->width - 1, static_data->height - 1,
300*64, 35*64);
}
/* Draw a circle outline and filling at the size and offet determined by
the ball's size */
static void draw_ball( display, pixmap, x_offset, y_offset, width, height)
Display * display;
Pixmap pixmap;
double x_offset, y_offset;
double width, height;
{
draw_circle( display, pixmap, static_data->gc,
(int)(static_data->width * x_offset),
(int)(static_data->height * y_offset),
(int)(static_data->width * width),
(int)(static_data->height * height));
}
/* Draw a filled circle of the specified size and position */
static void draw_circle( display, pixmap, gc, x, y, width, height)
Display * display;
Pixmap pixmap;
GC gc;
int x, y;
int width, height;
{
XDrawArc(display, pixmap, static_data->gc, x, y, width, height, 0, 360*64);
XFillArc(display, pixmap, static_data->gc, x, y, width, height, 0, 360*64);
}
/* Draw a black ball onto the first pixmap */
static void draw_black_ball( display, background)
Display * display;
Pixel background;
{
Pixmap stipple_bitmap =
XCreateBitmapFromData( display,
RootWindow( display, DefaultScreen( display)),
stipple_bits, stipple_width, stipple_height);
static_data->pixmaps[ 0] =
XCreatePixmap(display,
RootWindow(display, DefaultScreen(display)),
static_data->width,
static_data->height,
DefaultDepth( display, DefaultScreen(display)));
/* Reset all pixels in the background so they will not have
any effect when the pixmap is xor-copied */
XSetBackground(display, static_data->gc, 0);
XFillRectangle(display, static_data->pixmaps[0], static_data->gc, 0, 0,
static_data->width, static_data->height);
XSetForeground(display, static_data->gc,
BlackPixel(display,DefaultScreen(display)) ^background);
/* Draw item */
draw_circle( display, static_data->pixmaps[0], static_data->gc, 0, 0,
static_data->width-1, static_data->height-1);
/* draw a grey specularity small and offset */
XSetStipple( display, static_data->gc, stipple_bitmap);
XSetFillStyle( display, static_data->gc, FillOpaqueStippled);
draw_ball( display, static_data->pixmaps[0], 0.2, 0.2, 0.3, 0.3);
XSetFillStyle( display, static_data->gc, FillSolid);
/* draw a white specularity smallest and offset */
XSetForeground(display, static_data->gc,
WhitePixel(display,DefaultScreen(display)) ^background);
draw_ball( display, static_data->pixmaps[0], 0.3, 0.3, 0.1, 0.1);
static_data->num_colors = 1;
XFreePixmap( display, stipple_bitmap);
}
#ifdef DEBUG
item__print( item)
item_type item;
{
printf("x,y_pos: %4d,%4d; x,y_vel: %4d,%4d, collide = %d\n",
item__get_x( item), item__get_y( item),
item__get_x_vel( item), item__get_y_vel( item),
(int)static_data->ball_collide);
}
#endif
|