-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplode.c
202 lines (177 loc) · 5.33 KB
/
explode.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
//stolen from https://codegolf.stackexchange.com/questions/24462/display-the-explosion-of-a-star-in-ascii-art
#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define NUM_FRAMES 150
#define NUM_BLOBS 400
#define PERSPECTIVE 50.0
typedef struct {
double x,y,z;
} spaceblob;
double prng() {
static long long s=1;
s = s * 1488248101 + 981577151;
return ((s % 65536) - 32768) / 32768.0;
}
int main() {
char *frames[NUM_FRAMES], *p;
int i,j,x,y,z,v,rows,cols,ith,i0;
float radius, theta;
int maxx,minx,maxy,miny,delay=1E6;
double bx,by,bz,br,r,th,t;
spaceblob *blobs;
/* Initialize ncurses and get window dimensions */
initscr();
erase();
refresh();
getmaxyx(stdscr,rows,cols);
minx = -cols / 2;
maxx = cols+minx-1;
miny = -rows / 2;
maxy = rows+miny-1;
/* Generate random blob coordinates */
blobs = malloc(NUM_BLOBS * sizeof(spaceblob));
for (i=0; i<NUM_BLOBS; i++) {
bx = prng();
by = prng();
bz = prng();
br = sqrt(bx*bx + by*by + bz*bz);
blobs[i].x = (bx / br) * (1.3 + 0.2 * prng());
blobs[i].y = (0.5 * by / br) * (1.3 + 0.2 * prng());;
blobs[i].z = (bz / br) * (1.3 + 0.2 * prng());;
}
/* Generate animation frames */
for (i=0; i<NUM_FRAMES; i++) {
t = (1. * i) / NUM_FRAMES;
p = frames[i] = malloc(cols * rows + 1);
for (y=miny; y<=maxy; y++) {
for (x=minx; x<=maxx; x++) {
/* Otherwise show blast wave */
theta = atan2(y*2.+0.01,x+0.01);
radius = sqrt(x*x + 4*y*y);
r = radius * (.7 + (prng()*.1) * cos(16. * theta+i/10.));
ith = 32 + th * 32. * M_1_PI;
v = i - r;
if (v<7) *p++ = (i<19)?"%@W#H=+~-:."[i-1]:' '; /* initial flash */
else if (v<20) *p++ = " .:!HIOMW#%$&@08O=+-"[v];
else *p++=' ';
}
}
/* Add blobs with perspective effect */
if (i>7) {
i0 = i;
for (j=0; j<NUM_BLOBS; j++) {
bx = blobs[j].x * i0;
by = blobs[j].y * i0;
bz = blobs[j].z * i0;
if (bz<5-PERSPECTIVE || bz>PERSPECTIVE) continue;
x = cols / 2 + bx * PERSPECTIVE / (bz+PERSPECTIVE);
y = rows / 2 + by * PERSPECTIVE / (bz+PERSPECTIVE);
if (x>=0 && x<cols && y>=0 && y<rows)
frames[i][x+y*cols] = (bz>40) ? '.' : (bz>-20) ? 'o' : '@';
}
}
/* Terminate the text string for this frame */
*p = '\0';
}
/* Now play back the frames in sequence */
curs_set(0); /* hide text cursor (supposedly) */
for (i=0; i<NUM_FRAMES; i++) {
erase();
mvaddstr(0,0,frames[i]);
refresh();
usleep(delay);
delay=30000; /* Change to 30fps after first frame */
}
for (i=NUM_FRAMES-1; i>=0; i--) {
erase();
mvaddstr(0,0,frames[i]);
refresh();
usleep(delay);
delay=30000; /* Change to 30fps after first frame */
}
curs_set(1); /* unhide cursor */
endwin(); /* Exit ncurses */
return 0;
}
int old() {
char *frames[NUM_FRAMES], *p;
int i,j,x,y,z,v,rows,cols,ith,i0;
int maxx,minx,maxy,miny,delay=1E6;
double bx,by,bz,br,r,th,t;
spaceblob *blobs;
/* Initialize ncurses and get window dimensions */
initscr();
getmaxyx(stdscr,rows,cols);
minx = -cols / 2;
maxx = cols+minx-1;
miny = -rows / 2;
maxy = rows+miny-1;
/* Generate random blob coordinates */
blobs = malloc(NUM_BLOBS * sizeof(spaceblob));
for (i=0; i<NUM_BLOBS; i++) {
bx = prng();
by = prng();
bz = prng();
br = sqrt(bx*bx + by*by + bz*bz);
blobs[i].x = (bx / br) * (1.3 + 0.2 * prng());
blobs[i].y = (0.5 * by / br) * (1.3 + 0.2 * prng());;
blobs[i].z = (bz / br) * (1.3 + 0.2 * prng());;
}
/* Generate animation frames */
for (i=0; i<NUM_FRAMES; i++) {
t = (1. * i) / NUM_FRAMES;
p = frames[i] = malloc(cols * rows + 1);
for (y=miny; y<=maxy; y++) {
for (x=minx; x<=maxx; x++) {
/* Show a single '*' in first frame */
if (i==0) {
*p++ = (x==0 && y==0) ? '*' : ' ';
continue;
}
/* Show expanding star in next 7 frames */
if (i<8) {
r = sqrt(x*x + 4*y*y);
*p++ = (r < i*2) ? '@' : ' ';
continue;
}
/* Otherwise show blast wave */
r = sqrt(x*x + 4*y*y) * (0.5 + (prng()/3.0)*cos(16.*atan2(y*2.+0.01,x+0.01))*.3);
ith = 32 + th * 32. * M_1_PI;
v = i - r - 7;
if (v<0) *p++ = (i<19)?"%@W#H=+~-:."[i-8]:' '; /* initial flash */
else if (v<20) *p++ = " .:!HIOMW#%$&@08O=+-"[v];
else *p++=' ';
}
}
/* Add blobs with perspective effect */
if (i>6) {
i0 = i-6;
for (j=0; j<NUM_BLOBS; j++) {
bx = blobs[j].x * i0;
by = blobs[j].y * i0;
bz = blobs[j].z * i0;
if (bz<5-PERSPECTIVE || bz>PERSPECTIVE) continue;
x = cols / 2 + bx * PERSPECTIVE / (bz+PERSPECTIVE);
y = rows / 2 + by * PERSPECTIVE / (bz+PERSPECTIVE);
if (x>=0 && x<cols && y>=0 && y<rows)
frames[i][x+y*cols] = (bz>40) ? '.' : (bz>-20) ? 'o' : '@';
}
}
/* Terminate the text string for this frame */
*p = '\0';
}
/* Now play back the frames in sequence */
curs_set(0); /* hide text cursor (supposedly) */
for (i=0; i<NUM_FRAMES; i++) {
erase();
mvaddstr(0,0,frames[i]);
refresh();
usleep(delay);
delay=33333; /* Change to 30fps after first frame */
}
curs_set(1); /* unhide cursor */
endwin(); /* Exit ncurses */
return 0;
}