home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!news.tek.com!master!saab!billr
- From: billr@saab.CNA.TEK.COM (Bill Randle)
- Newsgroups: comp.sources.games
- Subject: v15i042: xballs - bouncing balls on the background window, Part01/01
- Message-ID: <4198@master.CNA.TEK.COM>
- Date: 14 Jan 93 03:35:00 GMT
- Sender: news@master.CNA.TEK.COM
- Lines: 663
- Approved: billr@saab.CNA.TEK.COM
- Xref: uunet comp.sources.games:1541
-
- Submitted-by: bmh@terminus.ericsson.se (Bernard Hatt)
- Posting-number: Volume 15, Issue 42
- Archive-name: xballs/Part01
- Environment: X11, Xlib
-
- [More of a novelty than a game. It does work with olvwm 3.3. -br]
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 1 (of 1)."
- # Contents: README MANIFEST Makefile xballs.c
- # Wrapped by billr@saab on Wed Jan 13 19:32:01 1993
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(860 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- X
- XBouncing balls for an X windows background.
- X
- XKnown not work to with tvtwm, may not work with other virtual desktop
- Xsystems. Works with twm and olwm.
- X
- XCan take a lot of cpu and X bandwidth.
- X
- XAlter the Makefile as appropriate and type:
- X make
- X
- XTo run type:
- X xballs
- X
- XOptions are:
- X -d hostname display on hostname
- X -n number number of balls (6 by default)
- X -r radius radius of balls
- X -c coeff coefficient of restitution (sp?)
- X -t percent percentage of balls generated at the to of the screen
- X
- XThe program is a bit 'rough round the edges'
- X
- XThe balls interact with the mouse pointer, but not with each other (I'm
- Xsure there is a simple method for this using matrices but I havn't been
- Xable to find it)
- X
- XThe more balls you have, the more cpu power used. (I would suggest a
- Xmaximum of about 6 for a Sun 3 and 12 for a Sun 4)
- X
- XBernard Hatt
- Xbmh@terminus.ericsson.se
- END_OF_FILE
- if test 860 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'MANIFEST' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'MANIFEST'\"
- else
- echo shar: Extracting \"'MANIFEST'\" \(238 characters\)
- sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
- X File Name Archive # Description
- X-----------------------------------------------------------
- X MANIFEST 1 This shipping list
- X Makefile 1
- X README 1
- X xballs.c 1
- END_OF_FILE
- if test 238 -ne `wc -c <'MANIFEST'`; then
- echo shar: \"'MANIFEST'\" unpacked with wrong size!
- fi
- # end of 'MANIFEST'
- fi
- if test -f 'Makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Makefile'\"
- else
- echo shar: Extracting \"'Makefile'\" \(147 characters\)
- sed "s/^X//" >'Makefile' <<'END_OF_FILE'
- X
- Xxballs: xballs.c
- X cc xballs.c -O -o xballs -lX11 -I/usr/openwin/include
- X
- Xshar: xballs.c README Makefile
- X shar README xballs.c Makefile >xballs.sh
- END_OF_FILE
- if test 147 -ne `wc -c <'Makefile'`; then
- echo shar: \"'Makefile'\" unpacked with wrong size!
- fi
- # end of 'Makefile'
- fi
- if test -f 'xballs.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'xballs.c'\"
- else
- echo shar: Extracting \"'xballs.c'\" \(8589 characters\)
- sed "s/^X//" >'xballs.c' <<'END_OF_FILE'
- X/*
- X * Simple demo, animated (bouncing balls) background.
- X *
- X * Author: Bernard Hatt (bmh@terminus.ericsson.se)
- X *
- X * Camtec Electronics (Ericsson), Leicester, England, LE1 4SA
- X *
- X */
- X
- X#include <stdio.h>
- X#include <signal.h>
- X#include <ctype.h>
- X#include <sys/time.h>
- X
- X#include <X11/X.h>
- X#include <X11/Xlib.h>
- X#include <X11/cursorfont.h>
- X
- XDisplay *Disp;
- Xint Scr;
- XWindow Root;
- XGC GcF,GcB;
- X
- X#define NONE 0 /* not in a window */
- X#define V 1 /* vertical */
- X#define H 2 /* horizontal */
- X#define B 3 /* ball */
- X
- X#define MOUSEH 24
- X#define MOUSEW 24
- X
- X#define DEFRAD 16 /* default diameter */
- X#define DEFNO 6
- X#define SPEED 128
- X#define SQLIMIT (SPEED*SPEED/(20*20)) /* square of lower speed limit */
- X
- Xint dispx,dispy;
- X
- Xtypedef struct rectangle_struct
- X{
- X int x1,y1; /* x and y coords top left */
- X int x2,y2; /* x and y coords bottom right */
- X} RECTANGLE;
- X
- Xint nor;
- XRECTANGLE *rp=NULL;
- X
- Xtypedef struct ball_struct
- X{
- X int x,y; /* x and y coords */
- X int dx,dy; /* x and y velocity */
- X} BALL;
- X
- Xint noballs=DEFNO;
- Xint radius=DEFRAD;
- XBALL *bp;
- Xint coeff=85;
- Xint attop=100;
- X
- Xint backflag=0;
- Xint invertflag=0;
- Xint trailflag=0;
- Xint mouseflag=1;
- Xint sizeflag=0;
- Xint momentumflag=0;
- Xint interact=0;
- Xint mx,my,mn,mdx,mdy;
- Xint failed=0;
- Xint currmax=0; /* current maximum number of windows */
- X
- Xchar *getenv();
- X
- Xint lasttime;
- X
- X
- Xlong lastsec=0;
- Xstruct timezone tz={0,0};
- Xstruct timeval tv;
- Xstruct timeval tv2;
- X
- Xint
- Xrnd(n)
- Xint n;
- X{
- X return((rand()/153)%n);
- X}
- X
- X
- Xvoid /* because Ultrix doesn't provide the nice SunOS usleep() */
- Xmyusleep(val)
- Xint val;
- X{
- X tv2.tv_sec=0;
- X tv2.tv_usec=val;
- X
- X select(0,NULL,NULL,NULL,&tv2);
- X}
- X
- Xvoid
- Xdointr()
- X{
- X clearballs();
- X exit(1);
- X}
- X
- Xvoid
- XXerr()
- X{
- X failed++;
- X}
- X
- Xvoid
- XXfatal()
- X{
- X exit(1);
- X}
- X
- Xvoid
- Xrandomball(i)
- Xint i;
- X{
- X int x,y;
- X int dum;
- X int attempts;
- X attempts=0;
- X do
- X {
- X x=rnd(dispx);
- X y=rnd(dispy);
- X if(rnd(100)<attop)
- X y=0;
- X attempts++;
- X if(attempts>2000)
- X {
- X clearballs();
- X exit(1);
- X }
- X }
- X while(inwin(x,y,0,0,&dum)!=NONE);
- X bp[i].x=x;
- X bp[i].y=y;
- X bp[i].dx=rnd(SPEED)+(SPEED/2);
- X bp[i].dy=rnd(SPEED)+(SPEED/2);
- X switch(rnd(4))
- X {
- X case 0:
- X break;
- X case 1:
- X bp[i].dx=(0-bp[i].dx);
- X break;
- X case 2:
- X bp[i].dy=(0-bp[i].dy);
- X break;
- X case 3:
- X bp[i].dx=(0-bp[i].dx);
- X bp[i].dy=(0-bp[i].dy);
- X break;
- X }
- X}
- X
- Xint
- Xgettime()
- X{
- X gettimeofday(&tv,&tz);
- X if(lastsec==0)
- X lastsec=tv.tv_sec;
- X return((tv.tv_sec-lastsec)*100+(tv.tv_usec/10000));
- X}
- X
- Xint
- Xinwin(x,y,dx,dy,n)
- Xint x,y,dx,dy;
- Xint *n;
- X{
- X int i,diffx,diffy;
- X int coln;
- X coln=(-1);
- X if((x<0)||(x>dispx))
- X return(V);
- X if((y<0)||(y>dispy))
- X return(H);
- X for(i=0;i<nor;i++)
- X {
- X if((x<rp[i].x1)||(y<rp[i].y1)||(y>rp[i].y2)||(x>rp[i].x2))
- X continue;
- X coln=i;
- X break;
- X }
- X if(coln==(-1))
- X {
- X if(!interact)
- X return(NONE);
- X for(i=0;i<noballs;i++)
- X {
- X if(i==(*n))
- X break;
- X diffx=(bp[i].x-x);
- X diffy=(bp[i].y-y);
- X if((diffx*diffx+diffy*diffy)<(radius*radius*4))
- X {
- X (*n)=i;
- X return(B);
- X }
- X }
- X return(NONE);
- X }
- X *n=coln;
- X x-=dx;
- X if((x<rp[i].x1)||(y<rp[i].y1)||(y>rp[i].y2)||(x>rp[i].x2))
- X return(V);
- X return(H);
- X}
- X
- Xclearballs()
- X{
- X int i;
- X XClearWindow(Disp,Root);
- X XFlush(Disp);
- X}
- X
- Xvoid
- Xdoballs()
- X{
- X int i,n;
- X int newtime,td;
- X int dx,dy,x,y,nx,ny;
- X int redo;
- X int diffx,diffy;
- X int dumint;
- X Window dumwin;
- X
- X newtime=gettime();
- X td=newtime-lasttime;
- X if(td>20)
- X td=20;
- X
- X if(mouseflag)
- X {
- X XQueryPointer(Disp,Root,&dumwin,&dumwin,&nx,&ny,&dumint,&dumint,&dumint);
- X mdx=nx-mx;
- X mx=nx;
- X mdy=ny-my;
- X my=ny;
- X rp[mn].x1=mx-(MOUSEW/2);
- X rp[mn].y1=my-(MOUSEH/2);
- X rp[mn].x2=mx+(MOUSEW/2);
- X rp[mn].y2=my+(MOUSEH/2);
- X }
- X
- X for(i=0;i<noballs;i++)
- X {
- X if(backflag||trailflag)
- X XFillArc(Disp,Root,GcB,bp[i].x-(radius/2),bp[i].y-(radius/2),radius,radius,0,360*64);
- X else
- X XClearArea(Disp,Root,bp[i].x-(radius/2),bp[i].y-(radius/2),radius,radius,0);
- X redo=0;
- X if((bp[i].dx*bp[i].dx+bp[i].dy*bp[i].dy)<SQLIMIT)
- X redo=10;
- X do
- X {
- X dx=(td*bp[i].dx)/100;
- X dy=(td*bp[i].dy)/100;
- X if(redo>5)
- X {
- X redo=0;
- X randomball(i);
- X }
- X
- X
- X/* printf("coords are %d,%d [%d,%d]\n",bp[i].x,bp[i].y,bp[i].dx,bp[i].dy);*/
- X n=i;
- X switch(inwin(dx+bp[i].x,dy+bp[i].y,dx,dy,&n))
- X {
- X case NONE:
- X bp[i].x+=dx;
- X bp[i].y+=dy;
- X redo=0;
- X break;
- X case V:
- X if((n==mn)&momentumflag)
- X {
- X bp[i].dx=mdx;
- X bp[i].dy=mdy;
- X bp[i].x+=dx;
- X bp[i].y+=dy;
- X }
- X else
- X bp[i].dx=(coeff*bp[i].dx)/100;
- X
- X redo++;
- X break;
- X case H:
- X if((n==mn)&momentumflag)
- X {
- X bp[i].dy=mdy;
- X bp[i].dx=mdx;
- X bp[i].x+=dx;
- X bp[i].y+=dy;
- X }
- X else
- X bp[i].dy=(coeff*bp[i].dy)/100;
- X
- X if(coeff!=100)
- X {
- X/* printf("[%d] y=%d dy=%d\n",i,bp[i].y,dy);*/
- X if((bp[i].y>=(dispy-3))&&(dy<=5))
- X redo=10;
- X if((dx==0)&&(dy<=3))
- X redo=10;
- X }
- X
- X redo++;
- X break;
- X case B:
- X diffx=bp[i].x-bp[n].x;
- X diffy=bp[i].y-bp[n].y;
- X
- X break;
- X }
- X if(n==mn)
- X redo=0;
- X }
- X while(redo);
- X bp[i].dy+=td;
- X
- X XFillArc(Disp,Root,GcF,bp[i].x-(radius/2),bp[i].y-(radius/2),radius,radius,0,360*64);
- X XFlush(Disp);
- X }
- X lasttime=newtime;
- X}
- X
- X
- Xint
- Xgetwins()
- X{
- X int i;
- X Window dummy1,dummy2,*list;
- X XWindowAttributes WAttrib;
- X XEvent Event;
- X
- X unsigned int listsize;
- X
- X XQueryTree(Disp,Root,&dummy1,&dummy2,&list,&listsize);
- X
- X if((listsize+2)>currmax)
- X {
- X if(rp!=NULL)
- X free(rp);
- X currmax=listsize+20;
- X/* printf("currmax=%d\n",currmax);*/
- X rp=(RECTANGLE*)malloc(sizeof(RECTANGLE)*currmax);
- X }
- X else
- X {
- X while(XCheckWindowEvent(Disp,Root,SubstructureNotifyMask,&Event))
- X {
- X while(XCheckWindowEvent(Disp,Root,SubstructureNotifyMask,&Event));
- X lasttime=gettime();
- X myusleep(50*1000);
- X }
- X }
- X if(rp==NULL)
- X {
- X fprintf(stderr,"Failed to malloc %d bytes\n",sizeof(RECTANGLE)*listsize*2);
- X exit(1);
- X }
- X
- X nor=0;
- X for(i=0;i<listsize;i++)
- X {
- X failed=0;
- X XGetWindowAttributes(Disp,list[i],&WAttrib);
- X if((!failed)&&WAttrib.map_state==IsViewable)
- X {
- X rp[nor].x1=WAttrib.x;
- X rp[nor].y1=WAttrib.y;
- X rp[nor].x2=WAttrib.x+WAttrib.width;
- X rp[nor].y2=WAttrib.y+WAttrib.height;
- X/* printf("Window id 0x%x, x1=%d,y1=%d,x2=%d,y2=%d\n",list[nor],rp[nor].x1,rp[nor].y1,rp[nor].x2,rp[nor].y2);*/
- X nor++;
- X }
- X }
- X if(mouseflag)
- X {
- X mn=nor;
- X nor++;
- X }
- X XFree(list);
- X}
- X
- Xmain(argc,argv)
- Xint argc;
- Xchar *argv[];
- X{
- X unsigned long bg,fg;
- X char *dispname;
- X int i,j;
- X int x,y;
- X char *p;
- X XEvent Event;
- X
- X srand(time(0));
- X dispname=getenv("DISPLAY");
- X for(i=1;i<argc;i++)
- X {
- X p=argv[i];
- X if(*p++=='-')
- X {
- X switch(*p++)
- X {
- X case 'd':
- X if(*p=='\0')
- X dispname=argv[++i];
- X else
- X dispname=p;
- X break;
- X case 'n':
- X if(*p=='\0')
- X noballs=atoi(argv[++i]);
- X else
- X noballs=atoi(p);
- X break;
- X case 'r':
- X if(*p=='\0')
- X radius=atoi(argv[++i]);
- X else
- X radius=atoi(p);
- X break;
- X case 'c':
- X if(*p=='\0')
- X coeff=atoi(argv[++i]);
- X else
- X coeff=atoi(p);
- X break;
- X case 't':
- X if(*p=='\0')
- X attop=atoi(argv[++i]);
- X else
- X attop=atoi(p);
- X break;
- X case 'b':
- X backflag=(!backflag);
- X break;
- X case 'i':
- X invertflag=(!invertflag);
- X break;
- X case 'm':
- X mouseflag=(!mouseflag);
- X break;
- X case 'M':
- X momentumflag=(!momentumflag);
- X break;
- X case 'I':
- X interact=(!interact);
- X break;
- X case 'l':
- X trailflag=(!trailflag);
- X break;
- X case 's':
- X sizeflag=(!sizeflag);
- X break;
- X default:
- X /* usage */
- X break;
- X }
- X }
- X }
- X if(coeff>100)
- X coeff=100;
- X if(coeff<0)
- X coeff=0;
- X coeff=(0-coeff);
- X bp=(BALL*)malloc(sizeof(BALL)*noballs);
- X if(bp==NULL)
- X {
- X fprintf(stderr,"Failed to malloc %d bytes\n",sizeof(BALL)*noballs);
- X exit(1);
- X }
- X Disp=XOpenDisplay(dispname);
- X
- X Root=XDefaultRootWindow(Disp);
- X Scr=DefaultScreen(Disp);
- X
- X clearballs();
- X if(noballs==0)
- X exit(0);
- X
- X
- X if(invertflag)
- X {
- X fg=BlackPixel(Disp,Scr);
- X bg=WhitePixel(Disp,Scr);
- X }
- X else
- X {
- X bg=BlackPixel(Disp,Scr);
- X fg=WhitePixel(Disp,Scr);
- X }
- X
- X GcF=XCreateGC(Disp,Root,0,0);
- X GcB=XCreateGC(Disp,Root,0,0);
- X XSetForeground(Disp,GcF,fg);
- X XSetForeground(Disp,GcB,bg);
- X
- X dispx=XDisplayWidth(Disp,Scr);
- X dispy=XDisplayHeight(Disp,Scr);
- X
- X if(sizeflag)
- X fprintf(stderr,"Screen size is %dX by %dY\n",dispx,dispy);
- X
- X if(backflag)
- X {
- X XSetWindowBackground(Disp,Root,bg);
- X XClearWindow(Disp,Root);
- X }
- X XSelectInput(Disp,Root,SubstructureNotifyMask);
- X
- X/* printf("Width and Height are %d,%d\n",dispx,dispy);*/
- X getwins();
- X XFlush(Disp);
- X for(i=0;i<noballs;i++)
- X {
- X randomball(i);
- X }
- X lasttime=gettime();
- X signal(SIGINT,dointr);
- X signal(SIGHUP,dointr);
- X signal(SIGTERM,dointr);
- X XSetErrorHandler(Xerr);
- X XSetIOErrorHandler(Xfatal);
- X while(1)
- X {
- X if(XCheckWindowEvent(Disp,Root,SubstructureNotifyMask,&Event))
- X {
- X getwins();
- X }
- X doballs();
- X myusleep(50*1000);
- X }
- X}
- X
- END_OF_FILE
- if test 8589 -ne `wc -c <'xballs.c'`; then
- echo shar: \"'xballs.c'\" unpacked with wrong size!
- fi
- # end of 'xballs.c'
- fi
- echo shar: End of archive 1 \(of 1\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have the archive.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-