-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbles.c
383 lines (346 loc) · 8.68 KB
/
bubbles.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
//******************************************************************************
#include "display.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#define WIDTH 448
#define HEIGHT 236
#define BORDER 30
#define MAX_DIST 574
#define EMPTY 25
#define QUEUE_LEN 100000
#define FRAMES_PER_BUBBLE 20
#define FRAMES_PER_RING 10
#define MAX_SAME 3
#define DELAY 25
#define MIN_DELAY 20
//******************************************************************************
typedef struct
{
int16_t x;
int16_t y;
int next[3];
} pixel_t;
static pixel_t pixels[(WIDTH + BORDER) * (HEIGHT + BORDER)];
static int dist_first[MAX_DIST + 1];
static void init()
{
memset(pixels, 0, WIDTH * HEIGHT * sizeof(pixel_t));
memset(dist_first, 0, (MAX_DIST + 1) * sizeof(int));
int n = 0;
for (int d = 0; d < MAX_DIST; d++)
{
dist_first[d] = n;
for (int x = 0; x < WIDTH + BORDER; x++)
for (int y = 0; y < HEIGHT + BORDER; y++)
{
int dist = (int)(sqrtf((float)x * (float)x + (float)y * (float)y) + 0.5);
if (dist == d)
{
pixels[n].x = x;
pixels[n].y = y;
if (d)
{
int nearest = -1;
float best_dist = (float)(MAX_DIST + 1);
for (int i = dist_first[d - 1]; i < dist_first[d]; i++)
{
float dx = (float)(pixels[i].x - x);
float dy = (float)(pixels[i].y - y);
float di = sqrtf(dx * dx + dy * dy);
if (di < best_dist)
{
best_dist = di;
nearest = i;
}
assert(nearest >= 0);
}
if (pixels[nearest].next[0] == 0)
pixels[nearest].next[0] = n;
else if (pixels[nearest].next[1] == 0)
pixels[nearest].next[1] = n;
else if (pixels[nearest].next[2] == 0)
pixels[nearest].next[2] = n;
else
assert(0);
}
n++;
}
}
}
assert(n == (WIDTH + BORDER) * (HEIGHT + BORDER));
dist_first[MAX_DIST] = n;
}
//*****************************************************************************
static bool screen[WIDTH + 2 * BORDER][HEIGHT + 2 * BORDER];
static void set(int x, int y, bool value)
{
assert(x >= 0);
assert(x < WIDTH + 2 * BORDER);
assert(y >= 0);
assert(y < HEIGHT + 2 * BORDER);
screen[x][y] = value;
x -= BORDER;
y -= BORDER;
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
display_set_virt(x, y, value);
}
//*****************************************************************************
typedef struct
{
int16_t x0;
int16_t y0;
int16_t xs;
int16_t ys;
int pixels_i;
bool value;
} job_t;
static job_t queue[QUEUE_LEN];
static int head = 0;
static int tail = 0;
static void bubble_create(int x, int y, int8_t value)
{
set(x, y, value);
queue[head].x0 = x;
queue[head].y0 = y;
queue[head].xs = 1;
queue[head].ys = 1;
queue[head].pixels_i = 0;
queue[head].value = value;
head++;
head %= QUEUE_LEN;
assert(head != tail);
set(x - 1, y, value);
queue[head].x0 = x - 1;
queue[head].y0 = y;
queue[head].xs = -1;
queue[head].ys = 1;
queue[head].pixels_i = 0;
queue[head].value = value;
head++;
head %= QUEUE_LEN;
assert(head != tail);
set(x, y - 1, value);
queue[head].x0 = x;
queue[head].y0 = y - 1;
queue[head].xs = 1;
queue[head].ys = -1;
queue[head].pixels_i = 0;
queue[head].value = value;
head++;
head %= QUEUE_LEN;
assert(head != tail);
set(x - 1, y - 1, value);
queue[head].x0 = x - 1;
queue[head].y0 = y - 1;
queue[head].xs = -1;
queue[head].ys = -1;
queue[head].pixels_i = 0;
queue[head].value = value;
head++;
head %= QUEUE_LEN;
assert(head != tail);
display_flush();
}
static void queue_run()
{
int end = head;
while (tail != end)
{
int16_t x0 = queue[tail].x0;
int16_t y0 = queue[tail].y0;
int16_t xs = queue[tail].xs;
int16_t ys = queue[tail].ys;
int i = queue[tail].pixels_i;
bool value = queue[tail].value;
for (int k = 0; k < 3; k++)
{
int j = pixels[i].next[k];
if (!j)
break;
int16_t x = x0 + xs * pixels[j].x;
int16_t y = y0 + ys * pixels[j].y;
if (x >= 0 && x < WIDTH + 2 * BORDER && y >= 0 && y < HEIGHT + 2 * BORDER
&& screen[x][y] != value)
{
set(x, y, value);
queue[head].x0 = x0;
queue[head].y0 = y0;
queue[head].xs = xs;
queue[head].ys = ys;
queue[head].pixels_i = j;
queue[head].value = value;
head++;
head %= QUEUE_LEN;
assert(head != tail);
}
}
tail++;
tail %= QUEUE_LEN;
}
}
//*****************************************************************************
static bool same(int x, int y)
{
bool reference = screen[x][y];
return ((x - EMPTY < 0 || screen[x - EMPTY][y] == reference)
&& (x + EMPTY >= WIDTH + 2 * BORDER || screen[x + EMPTY][y] == reference)
&& (y - EMPTY < 0 || screen[x][y - EMPTY] == reference)
&& (y + EMPTY >= HEIGHT + 2 * BORDER || screen[x][y + EMPTY]== reference)
&& (x - EMPTY < 0 || y - EMPTY < 0 || screen[x - EMPTY][y - EMPTY] == reference)
&& (x - EMPTY < 0 || y + EMPTY >= HEIGHT + 2 * BORDER || screen[x - EMPTY][y + EMPTY] == reference)
&& (x + EMPTY >= WIDTH + 2 * BORDER || y - EMPTY < 0 || screen[x + EMPTY][y - EMPTY] == reference)
&& (x + EMPTY >= WIDTH + 2 * BORDER || y + EMPTY >= HEIGHT + 2 * BORDER || screen[x + EMPTY][y + EMPTY] == reference));
}
static void filter()
{
for (int x = 1; x < WIDTH + 2 * BORDER - 1; x++)
for (int y = 1; y < HEIGHT + 2 * BORDER - 1; y++)
{
bool center = screen[x][y];
int count = 0;
if (screen[x - 1][y] != center)
count++;
if (screen[x + 1][y] != center)
count++;
if (screen[x][y - 1] != center)
count++;
if (screen[x][y + 1] != center)
count++;
if (count >= 3)
set(x, y, !center);
}
}
//******************************************************************************
static void animation_bubbles(int time)
{
for (int n = 0; display_button() == 0; n++)
{
time--;
if (!time)
return;
if (!(n % FRAMES_PER_BUBBLE))
{
int x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
int y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (same(x, y))
bubble_create(x, y, !screen[x][y]);
else
{
int x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
int y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (same(x, y))
bubble_create(x, y, !screen[x][y]);
else
{
int x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
int y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (same(x, y))
bubble_create(x, y, !screen[x][y]);
else
{
int x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
int y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (same(x, y))
bubble_create(x, y, !screen[x][y]);
}
}
}
}
queue_run();
filter();
display_flush();
usleep(DELAY * 1000);
}
}
static void animation_rings(int time)
{
int x = -1;
int y = -1;
for (int n = 0; display_button() == 0; n++)
{
time--;
if (!time)
return;
if (n % FRAMES_PER_BUBBLE == 0)
{
x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (!screen[x][y] && same(x, y))
bubble_create(x, y, true);
else
{
x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (!screen[x][y] && same(x, y))
bubble_create(x, y, true);
else
{
x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (!screen[x][y] && same(x, y))
bubble_create(x, y, true);
else
{
x = rand() % (WIDTH + 2 * BORDER - 1) + 1;
y = rand() % (HEIGHT + 2 * BORDER - 1) + 1;
if (!screen[x][y] && same(x, y))
bubble_create(x, y, true);
else
{
x = -1;
y = -1;
}
}
}
}
}
else if (n % FRAMES_PER_BUBBLE == FRAMES_PER_RING)
{
if (x >= 0 && y >= 0 && screen[x][y])
bubble_create(x, y, false);
}
queue_run();
filter();
display_flush();
usleep(DELAY * 1000);
}
}
int main(int ac, char *av[])
{
bool rings = false;
int display_select = 0;
int timeout = 0;
for (int ai = 1; ai < ac; ai++)
{
assert(av[ai][0] == '-');
if (av[ai][1] == 's')
display_select |= DISPLAY_SELECT_SP;
else if (av[ai][1] == 'x')
display_select |= DISPLAY_SELECT_GX;
else if (av[ai][1] == 'r')
rings = true;
else if (isdigit(av[ai][1]))
timeout = atoi(&av[ai][1]) * 36;
else
assert(0);
}
srand(time(NULL));
init();
memset(screen, 0, (WIDTH + 2 * BORDER) * (HEIGHT + 2 * BORDER) * sizeof(int8_t));
display_create(display_select);
if (rings)
animation_rings(timeout);
else
animation_bubbles(timeout);
display_free();
return EXIT_SUCCESS;
}
//*****************************************************************************