-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
433 lines (355 loc) · 7.69 KB
/
main.c
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
/**
* tdpw - Two Dimensional Physics Workbench
* Copyright (C) Iain R. Learmonth 2012-2014.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* Defaults, may be overridden
*/
#define TYPE 0
#define WIDTH 640
#define HEIGHT 360
#define FRAMES 200
#define ELASTIC 1
#define WRAP 1
#define FREEZE 0
#include "shape.h"
#include "pnm.h"
#include "circle.h"
#include "square.h"
#include "modulo.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
/**
* Convert an ASCII representation of a colour
* to its hex value.
*/
int strtocolour(char* colour)
{
if ( strcmp(colour, "random") == 0 )
{
int r = rand() % 7;
switch ( r )
{
case 0: colour = (char*)"red";
break;
case 1: colour = (char*)"green";
break;
case 2: colour = (char*)"blue";
break;
case 3: colour = (char*)"yellow";
break;
case 4: colour = (char*)"cyan";
break;
case 5: colour = (char*)"purple";
break;
case 6: colour = (char*)"white";
break;
}
}
if ( strcmp(colour, "red") == 0 )
{
return 0xff000000;
}
else if ( strcmp(colour, "green") == 0 )
{
return 0x00ff0000;
}
else if ( strcmp(colour, "blue") == 0 )
{
return 0x0000ff00;
}
else if ( strcmp(colour, "yellow") == 0 )
{
return 0xffff0000;
}
else if ( strcmp(colour, "cyan") == 0 )
{
return 0x00ffff00;
}
else if ( strcmp(colour, "purple") == 0 )
{
return 0xff00ff00;
}
else if ( strcmp(colour, "white") == 0 )
{
return 0xffffff00;
}
}
/**
* Parse the input script. This may either be interactive if a
* tty is detected, or simply reading stdin with no prompts if
* no tty is detected. In interactive mode, the printf statement
* for each new frame is replaced with a nice progress bar.
*/
void parseInput(int *t, int *w, int *h, int *f, int *el, int *wr, int *fz, int *ss, Shape *s)
{
*t = TYPE;
*w = WIDTH;
*h = HEIGHT;
*f = FRAMES;
*el = ELASTIC;
*wr = WRAP;
*fz = FREEZE;
*ss = 0;
char *input;
char *op;
char prompt[7];
if ( isatty(fileno(stdin)) )
{
printf("Entering interactive mode:\n");
strcpy(prompt, "tdpw> ");
}
else
{
prompt[0] = '\0';
}
while (1)
{
input = readline(prompt);
if ( input == 0 || *input == 0 )
continue;
add_history(input);
op = strtok(input, " ");
if ( strcmp(op, "exit") == 0 )
{
exit(0);
}
if ( strcmp(op, "run") == 0 )
{
break;
}
if ( strcmp(op, "squares") == 0 )
{
*t = 0;
continue;
}
if ( strcmp(op, "circles") == 0 )
{
*t = 1;
continue;
}
if ( strcmp(op, "echo") == 0 )
{
op = strtok(NULL, "");
printf("%s\n", op);
continue;
}
if ( strcmp(op, "frames") == 0 )
{
op = strtok(NULL, " ");
*f = atoi(op);
continue;
}
if ( strcmp(op, "elastic") == 0 )
{
*el = 1;
continue;
}
if ( strcmp(op, "inelastic") == 0 )
{
*el = 0;
continue;
}
if ( strcmp(op, "nocollide") == 0 )
{
*el = 2;
continue;
}
if ( strcmp(op, "freeze") == 0 )
{
*fz = atoi(strtok(NULL, " "));
continue;
}
if ( strcmp(op, "shape") == 0 )
{
s[*ss].x = atoi(strtok(NULL, " "));
s[*ss].y = atoi(strtok(NULL, " "));
s[*ss].d = atoi(strtok(NULL, " "));
s[*ss].xv = atoi(strtok(NULL, " "));
s[*ss].yv = atoi(strtok(NULL, " "));
s[*ss].h = strtocolour(strtok(NULL, " "));
++*ss;
continue;
}
if ( strcmp(op, "wrap") == 0 )
{
*wr = 0;
continue;
}
if ( strcmp(op, "nowrap") == 0 )
{
*wr = 1;
continue;
}
printf("Syntax error, please try again.\n");
}
}
/**
* Main Control
*
* 1) Initialise everything and get input
* 2) Draw frames
* a. Draw shapes
* b. Do movement
* 3) Make animated gif with gifsicle
* 4) Launch a sane HTML viewer
*/
int main(int argc, char* argv[])
{
//Shapes array to be populated
Shape s[1000];
Pic p;
/**
* t: Type
* w: Width of image
* h: Height of image
* f: Number of frames to create in total
* el: Collision type (0: inelastic, 1: elastic, 2: none)
* ss: Total number of shapes
* wr: Wrap around edges (0: true, 1: false)
* fz: Number of frames before movement begins
*/
int t, w, h, f, el, ss, wr, fz;
/**
* Loop Counters
* i, j: Shape indexes
* cf: Current frame
* e: Progress bar drawing
*/
int i, j, cf, e;
struct winsize tty;
//Function pointers
int (*cd)(Shape, Shape);
void (*ds)(Pic, int, int, int, int);
parseInput(&t, &w, &h, &f, &el, &wr, &fz, &ss, s);
switch ( t )
{
case 0: cd = &squareCollisionDetect;
ds = □
break;
case 1: cd = &circleCollisionDetect;
ds = &circle;
break;
}
printf("Creating frames...\n");
for ( cf = 0; cf < f ; ++cf )
{
char ppmname[30], gifname[30], cmd[100];
p = makenewpic(w, h);
p.type = colour;
//Draw background
rect(p, 0, 0, w, h, 0x00000000 );
//Draw shapes
for ( i = 0 ; i < ss ; ++i )
{
ds(p, s[i].x, s[i].y, s[i].d, s[i].h);
}
//If movement has begun, do movement
if ( cf > fz )
{
for ( i = 0 ; i < ss ; ++i )
{
for ( j = i + 1 ; j < ss ; ++j )
{
if ( cd(s[i], s[j]) && el == 1 )
{
s[i].xv ^= s[j].xv;
s[j].xv ^= s[i].xv;
s[i].xv ^= s[j].xv;
s[i].yv ^= s[j].yv;
s[j].yv ^= s[i].yv;
s[i].yv ^= s[j].yv;
}
if ( cd(s[i], s[j]) && el == 0 )
{
s[i].xv = s[j].xv = ( s[i].xv + s[j].xv ) / 2;
s[i].yv = s[j].yv = ( s[i].yv + s[j].yv ) / 2;
}
}
}
for ( i = 0 ; i < ss ; ++i )
{
s[i].x += s[i].xv;
s[i].y += s[i].yv;
if ( wr )
{
if ( s[i].x + s[i].d >= w )
{
s[i].xv *= -1;
s[i].x = w - s[i].d - 1;
}
else if ( s[i].x < 0 )
{
s[i].xv *= -1;
s[i].x = 0;
}
if ( s[i].y + s[i].d >= h )
{
s[i].yv *= -1;
s[i].y = h - s[i].d - 1;
}
else if ( s[i].y < 0 )
{
s[i].yv *= -1;
s[i].y = 0;
}
} else {
s[i].x = modulo(s[i].x, w);
s[i].y = modulo(s[i].y, h);
}
}
}
sprintf(ppmname,"_%03d.ppm",cf);
sprintf(gifname,"_%03d.gif",cf);
//Write image files
writepnm(p, ppmname);
sprintf(cmd,"ppmtogif %s 2>/dev/null > %s", ppmname, gifname);
system(cmd);
if ( isatty(fileno(stdin)) )
{
ioctl(0, TIOCGWINSZ, &tty);
putchar('[');
for ( e = 0 ; e < ( ( ( ( cf + 1 ) * 100 ) / f ) * ( tty.ws_col - 12 ) / 100 ) ; ++e )
putchar('=');
putchar('>');
for ( e = 0 ; e < ( tty.ws_col - 12 ) - ( ( ( ( cf + 1 ) * 100 ) / f ) * ( tty.ws_col - 12 ) / 100 ) ; ++e )
putchar(' ');
putchar(']');
printf(" %3d/%3d\r", cf + 1, f);
fflush(stdout);
}
else
{
printf("Creating frame %3d of %3d...\n", cf + 1, f);
}
}
if ( isatty(fileno(stdin)) )
{
for ( e = 0 ; e < tty.ws_col ; e ++ )
putchar(' ');
putchar('\r');
}
printf("Creating animated GIF...\n");
system("gifsicle _*.gif > anim.gif");
printf("Cleaning up...\n");
system("rm -f _*.ppm _*.gif");
}