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
|
#include "balls.h"
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
#define G_FACTOR 15.0
#define G (9.81/G_FACTOR)
enum {
BG = DWhite,
WALLS = DBlack,
WIDTH = 1280,
HEIGHT=720,
KEY_QUIT = 'q' ,
DEFAULT_NBALLS = 3,
VMAX = 3,
MASS_FACTOR = 75,
G_PER_KG = 1000,
FPS = 60,
NS_PER_SEC = 1000000000,
FRAME_TIME = NS_PER_SEC / FPS,
TICK_BUFSIZE = 1,
BALL_BUFSIZE = 1,
};
typedef struct {
Ball b;
int color;
Channel *frametick;
/* threads communicate in a ring */
Channel *lneighbor; /* <-chan Ball */
Channel *rneighbor; /* chan<- Ball */
int nothers; /* number of other balls */
} BallArg;
static int ballcolors[] = { DRed, DGreen, DBlue };
static uint radii[] = { 24, 32, 48 };
static Rectangle bounds = {{0, 0}, {WIDTH, HEIGHT}};
void spawnballs(int n);
void eventloop(Image *bg, Image *walls);
Channel **allocchans(int nchans, int elsize, int nel);
void nooverlapcircles(Point centers[], int n, uint radius);
Point randptinrect(Rectangle r);
int randint(int lo, int hi);
uint maxelem(uint arr[], uint n);
double mass(uint radius);
double area(double radius);
void ball(void *arg);
void frametick(void *arg);
void
threadmain(int argc, char *argv[]) {
int nballs;
Image *bg, *walls;
nballs = DEFAULT_NBALLS;
if (argc > 1) {
if (sscanf(argv[1], "%d", &nballs) != 1 || nballs < 1) {
printf("usage: balls [number of balls]\n");
threadexitsall(0);
}
}
printf("nballs: %ud\n", nballs);
if (initdraw(nil, nil, "Balls") < 0)
sysfatal("initdraw() failed\n");
printf("screen: (%d,%d) (%d,%d)\n", screen->r.min.x, screen->r.min.y,
screen->r.max.x, screen->r.max.y);
printf("bounds: (%d,%d) (%d,%d)\n", bounds.min.x, bounds.min.y,
bounds.max.x, bounds.max.y);
walls = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, WALLS);
bg = allocimage(display, bounds, RGBA32, 0, BG);
if (bg == nil || walls == nil)
sysfatal("failed to allocate images");
drawbg(walls, bg);
spawnballs(nballs);
eventloop(bg, walls);
}
void
spawnballs(int n) {
Channel **ticks;
Point *ps;
int i;
BallArg *arg;
Channel **cs;
if ((ticks = allocchans(n, sizeof(int), TICK_BUFSIZE)) == nil)
sysfatal("failed to allocate frame ticker channels");
threadcreate(frametick, ticks, mainstacksize);
if ((ps = malloc(n*sizeof(Point))) == nil)
sysfatal("failed to allocate position array");
nooverlapcircles(ps, n, maxelem(radii, NELEMS(radii)));
if ((cs = malloc(n*sizeof(Channel *))) == nil)
sysfatal("failed to allocate channel matrix");
for (i = 0; i < n; i++) {
if ((cs[i] = chancreate(sizeof(Ball), BALL_BUFSIZE)) == nil)
sysfatal("failed to create channel");
}
for (i = 0; i < n; i++) {
if ((arg = malloc(sizeof(BallArg))) == nil)
sysfatal("failed to allocate ball");
arg->b.p = ps[i];
arg->b.v = V((double) randint(-VMAX, VMAX+1), (double) randint(-VMAX, VMAX+1));
arg->b.r = radii[randint(0, NELEMS(radii))];
arg->b.m = mass(arg->b.r);
arg->color = ballcolors[randint(0, NELEMS(ballcolors))];
arg->frametick = ticks[i];
arg->lneighbor = cs[i];
arg->rneighbor = cs[(i+1) % n];
arg->nothers = n-1;
printf("create ball p(%d,%d) v(%f,%f) r(%u) m(%f)\n", arg->b.p.x, arg->b.p.y, arg->b.v.x, arg->b.v.y, arg->b.r, arg->b.m);
threadcreate(ball, arg, mainstacksize);
}
free(ps);
free(cs);
}
void
eventloop(Image *bg, Image *walls) {
Mousectl *mctl;
Keyboardctl *kctl;
int resize[2];
Rune key;
if ((mctl = initmouse(nil, screen)) == nil)
sysfatal("failed to initialize mouse\n");
if ((kctl = initkeyboard(nil)) == nil)
sysfatal("failed to initialize keyboard\n");
enum { RESIZE = 0, KEYBD = 1 };
Alt alts[3] = {
{mctl->resizec, &resize, CHANRCV},
{kctl->c, &key, CHANRCV},
{nil, nil, CHANEND},
};
for (;;) {
switch (alt(alts)) {
case RESIZE:
if (getwindow(display, Refnone) < 0)
sysfatal("getwindow() failed\n");
drawbg(walls, bg);
break;
case KEYBD:
if (key == KEY_QUIT)
threadexitsall(0);
break;
case -1: /* interrupted */
break;
}
}
}
/* allocate nil-terminated array of channels */
Channel **
allocchans(int nchans, int elsize, int nel) {
Channel **cs;
int i;
if (nchans < 0)
return nil;
if ((cs = malloc((nchans+1)*sizeof(Channel *))) == nil)
return nil;
for (i = 0; i < nchans; i++) {
if ((cs[i] = chancreate(elsize, nel)) == nil) {
while (i-- > 0)
chanfree(cs[i]);
free(cs);
return nil;
}
}
cs[nchans] = nil;
return cs;
}
/* populate array of circle coordinates within bounds so that circles don't overlap */
void
nooverlapcircles(Point centers[], int n, uint radius) {
int i, j;
srand(time(0));
for (i = 0; i < n; i++) {
centers[i] = randptinrect(insetrect(bounds, radius));
for (j = 0; j < i; j++)
if (iscollision(centers[j], radius, centers[i], radius))
break;
if (j < i) { /* overlapping */
i--;
continue;
}
}
}
Point
randptinrect(Rectangle r) {
return Pt(randint(r.min.x, r.max.x), randint(r.min.y, r.max.y));
}
int
randint(int lo, int hi) {
return (rand() % (hi-lo)) + lo;
}
uint
maxelem(uint arr[], uint n) {
uint max;
if (n == 0)
return 0;
max = arr[--n];
while (n-- > 0)
if (arr[n] > max)
max = arr[n];
return max;
}
double
mass(uint radius) {
return area(radius) / MASS_FACTOR / G_PER_KG;
}
double
area(double radius) {
return M_PI * radius*radius;
}
void
ball(void *arg) {
BallArg *barg;
Ball b, other;
Point oldpos;
Image *fill, *erase;
int i, t;
barg = (BallArg *) arg;
b = (Ball) barg->b;
fill = alloccircle(barg->color, DTransparent, b.r);
erase = alloccircle(BG, DTransparent, b.r);
if (fill == nil ||erase == nil)
sysfatal("failed to allocate image");
oldpos = b.p; /* keep track of previous position so it can be erased */
for (;;) {
b.v.y += G;
b.p = ptaddv(b.p, b.v);
printf("(%d,%d) %f %f\n", b.p.x, b.p.y, b.v.x, b.v.y);
/* check for ball collision */
other = b; /* send our own ball first */
for (i = 0; i < barg->nothers; i++) {
send(barg->rneighbor, &other); /* pass balls around the ring */
recv(barg->lneighbor, &other);
collideball(&b, &other);
}
collidewall(&b, bounds);
recv(barg->frametick, &t); /* wait for next frame */
drawcircle(erase, oldpos);
drawcircle(fill, b.p);
oldpos = b.p;
}
}
/* arg is nil-terminated array of chan int */
void
frametick(void *arg) {
Channel **cs, **c;
vlong t1, t2;
int s;
cs = (Channel **) arg;
t1 = t2 = nsec();
for (;;) {
while (t2 - t1 < FRAME_TIME)
t2 = nsec();
t1 = t2;
for (c = cs; *c != nil; c++)
send(*c, &s);
flushimage(display, Refnone);
}
}
|