the balls now have color, using the color scheme of stats(8). this makes it easier to follow the balls - and it is more pretty too. Axel. Reference: /n/sources/patch/sorry/juggle-color Date: Sat May 28 23:29:12 CES 2005 --- /sys/src/games/juggle.c Sat May 28 23:22:45 2005 +++ /sys/src/games/juggle.c Sat May 28 23:22:43 2005 @@ -2,19 +2,26 @@ #include #include #include -#define NSTEP 10 /* number of steps between throws */ -#define RBALL 10 /* radius of ball images */ -#define NBALL 100 +enum +{ + Ncolor = 6, + NSTEP = 10, /* number of steps between throws */ + RBALL = 10, /* radius of ball images */ + Nball = 100, +}; + int nhand=2; int delay=20; /* ms delay between steps */ int nball; int maxhgt; Rectangle win; + #define add addpt #define sub subpt #define inset insetrect + /* * pattern lists the heights of a repeating sequence of throws. * At time t, hand t%nhand throws. At that time, it must @@ -29,7 +36,7 @@ int time; /* time at which ball will arrive */ int hand; /* hand in which ball will rest on arrival */ }; -Ball ball[NBALL]; +Ball ball[Nball]; void throw(int t, int hgt){ int hand=t%nhand; int i, b, n; @@ -63,15 +70,17 @@ return (Point){win.min.x+(win.max.x-win.min.x)*alpha, win.max.y-1+(win.min.y-win.max.y)*hgt}; } -Image *image, *disk; + +Image *image, *cols[Ncolor], *disk[Ncolor]; + void move(int t){ int i, j; for(i=0;i!=NSTEP;i++){ if(ecanmouse()) emouse(); draw(image, inset(image->r, 3), display->white, nil, ZP); for(j=0;j!=nball;j++) - draw(image, rectaddpt(disk->r, sub(bpos(j, i, t), Pt(RBALL, RBALL))), - disk, nil, ZP); + draw(image, rectaddpt(disk[j%Ncolor]->r, sub(bpos(j, i, t), Pt(RBALL, RBALL))), + disk[j%Ncolor], nil, ZP); draw(screen, screen->r, image, nil, image->r.min); flushimage(display, 1); if(delay>0) @@ -80,6 +89,26 @@ } void +mkdisk(int i, int c) +{ + cols[i]=allocimage(display, Rect(0,0,1,1), CMAP8, 1, c); + disk[i]=allocimage(display, Rect(0, 0, 2*RBALL+1, 2*RBALL+1), screen->chan, 0, DWhite); + fillellipse(disk[i], Pt(RBALL, RBALL), RBALL, RBALL, cols[i], ZP); +} + +void +diskinit(void) +{ + /* colors taken from /sys/src/cmd/stats.c */ + mkdisk(0, 0xFFAAAAFF); + mkdisk(1, DPalegreygreen); + mkdisk(2, DDarkyellow); + mkdisk(3, DMedgreen); + mkdisk(4, 0x00AAFFFF); + mkdisk(5, 0xCCCCCCFF); +} + +void usage(char *name) { fprint(2, "usage: %s [start] pattern\n", name); @@ -161,8 +190,7 @@ if(initdraw(nil, nil, "juggle") < 0) sysfatal("initdraw failed: %r"); einit(Emouse); - disk=allocimage(display, Rect(0, 0, 2*RBALL+1, 2*RBALL+1), screen->chan, 0, DWhite); - fillellipse(disk, Pt(RBALL, RBALL), RBALL, RBALL, display->black, ZP); + diskinit(); eresized(0); if(image==0){ print("can't allocate bitmap");