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
|
#include <locale.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <uchar.h>
#include <wchar.h>
#include <sys/ioctl.h>
#define RHO 0.7 /* Rain density: (0, 1) */
#define RGB_BG_RED 34 /* Background color */
#define RGB_BG_GRN 34
#define RGB_BG_BLU 34
#define RGB_HD_RED 255 /* Color of the first raindrop */
#define RGB_HD_GRN 255
#define RGB_HD_BLU 255
#define RGB_TL_RED 40 /* Color of the rain */
#define RGB_TL_GRN 254
#define RGB_TL_BLU 20
#define DECAY_MPLIER 2 /* Phosphor decay multiplier */
#define DELAY_US 60000 /* Delay between frames: increase to slow the rain */
#define ANSI_CRSR_HIDE "\e[?25l"
#define ANSI_CRSR_SHOW "\e[?25h"
#define ANSI_CRSR_RESET "\x1b[H"
#define ANSI_FONT_BOLD "\x1b[1m"
#define ANSI_FONT_RESET "\x1b[0m"
#define ANSI_SCRN_CLEAR "\x1b[2J"
#define UNICODE(min, max) (((uint64_t)max << 32) | min)
static uint64_t glyphs[] = {
UNICODE(0x0021, 0x007E), /* ASCII */
#ifndef NOKANA
UNICODE(0xFF65, 0xFF9F), /* Half-width Katakana */
#endif
};
static uint8_t glyphlen = (sizeof glyphs) / (sizeof glyphs[0]);
enum {
R, /* Red */
G, /* Green */
B, /* Blue */
PD /* Phosphor decay multiplier */
};
typedef union color_tag {
uint32_t value;
unsigned char color[4];
} color;
typedef struct matrix_tag {
size_t rowlen; /* Row count: terminal screen height */
size_t collen; /* Column count: terminal screen width */
size_t *col; /* Column indices in random order (shuffle()) */
size_t *row; /* Current row index of columns: 1-1 map with col */
color *rgb; /* RGB color of the cell */
char32_t *code; /* Code point */
} matrix;
static inline size_t index(const matrix *mat,
size_t row, size_t col)
{
return mat->collen * row + col;
}
static inline void print(const matrix *mat,
size_t row, size_t col)
{
size_t idx;
idx = index(mat, row, col);
wprintf(L"\x1b[%d;%dH\x1b[38;2;%d;%d;%dm%lc", row, col,
mat->rgb[idx].color[R], mat->rgb[idx].color[G],
mat->rgb[idx].color[B], mat->code[idx]);
}
static inline void insert_code(matrix *mat,
size_t row, size_t col)
{
uint64_t blk;
uint32_t min, max;
blk = glyphs[(rand() % glyphlen)];
min = (uint32_t)blk;
max = (uint32_t)(blk >> 32);
mat->code[index(mat, row, col)] = rand() % (max - min) + min;
}
static inline void delete_code(matrix *mat,
size_t row, size_t col)
{
mat->code[index(mat, row, col)] = ' ';
print(mat, row, col);
}
static inline void recolor_head(matrix *mat,
size_t row, size_t col)
{
unsigned char *sc, *tc;
sc = mat->rgb[index(mat, 0, col)].color;
tc = mat->rgb[index(mat, row, col)].color;
tc[R] = sc[R];
tc[G] = sc[G];
tc[B] = sc[B];
print(mat, row, col);
}
static inline void draw_tail(matrix *mat,
size_t row, size_t col)
{
unsigned char *color;
color = mat->rgb[index(mat, row, col)].color;
color[R] = RGB_TL_RED;
color[G] = RGB_TL_GRN;
color[B] = RGB_TL_BLU;
print(mat, row, col);
}
static inline void draw_head(matrix *mat,
size_t row, size_t col)
{
unsigned char *color;
color = mat->rgb[index(mat, row, col)].color;
color[R] = RGB_HD_RED;
color[G] = RGB_HD_GRN;
color[B] = RGB_HD_BLU;
insert_code(mat, row, col);
print(mat, row, col);
}
static inline void blend(matrix *mat,
size_t row, size_t col)
{
unsigned char *color;
color = mat->rgb[index(mat, row, col)].color;
color[R] = color[R] - (color[R] - RGB_BG_RED) / DECAY_MPLIER;
color[G] = color[G] - (color[G] - RGB_BG_GRN) / DECAY_MPLIER;
color[B] = color[B] - (color[B] - RGB_BG_BLU) / DECAY_MPLIER;
}
static inline void swap_col(matrix *mat, size_t i, size_t max)
{
size_t j;
mat->row[i] = 0;
mat->rgb[i].color[PD] = 0;
j = rand() % (mat->collen - max) + max;
mat->col[i] = mat->col[i] ^ mat->col[j];
mat->col[j] = mat->col[i] ^ mat->col[j];
mat->col[i] = mat->col[i] ^ mat->col[j];
}
static inline uint8_t is_tail(matrix *mat,
size_t row, size_t col)
{
unsigned char *color;
color = mat->rgb[index(mat, row, col)].color;
return color[R] == RGB_TL_RED
&& color[G] == RGB_TL_GRN
&& color[B] == RGB_TL_BLU;
}
static inline void glitch(matrix *mat)
{
size_t i, j;
i = rand() % (mat->rowlen - 1);
j = rand() % mat->collen;
if (mat->code[index(mat, i, j)] != ' '
&& is_tail(mat, i, j)) {
insert_code(mat, i, j);
print(mat, i, j);
}
}
static inline void shuffle(size_t *a, size_t n)
{
size_t i, j;
for (i = n - 1; i > 0; i--) {
j = rand() % (i + 1);
a[j] = a[i] ^ a[j];
a[i] = a[i] ^ a[j];
a[j] = a[i] ^ a[j];
}
}
static inline int init_matrix(matrix *mat,
const struct winsize *ws)
{
size_t i;
mat->collen = ws->ws_col;
mat->rowlen = ws->ws_row + 1;
mat->code = realloc(mat->code,
sizeof mat->code[0] * mat->rowlen * mat->collen);
if (!mat->code)
return 0;
mat->rgb = realloc(mat->rgb,
sizeof mat->rgb[0] * mat->rowlen * mat->collen);
if (!mat->rgb)
return 0;
mat->col = realloc(mat->col,
sizeof mat->col[0] * mat->collen);
if (!mat->col)
return 0;
mat->row = realloc(mat->row,
sizeof mat->row[0] * mat->collen);
if (!mat->row)
return 0;
for (i = 0; i < mat->collen; i++) {
mat->row[i] = 0;
mat->col[i] = i;
}
shuffle(mat->col, mat->collen);
return 1;
}
static inline void destroy_matrix(matrix *mat)
{
free(mat->code);
free(mat->col);
free(mat->row);
free(mat->rgb);
}
static inline int init_term(const struct winsize *ws)
{
struct termios ta;
if (tcgetattr(STDIN_FILENO, &ta) == 0) {
ta.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSANOW, &ta) == 0) {
wprintf(L"\x1b[48;2;%d;%d;%dm",
RGB_BG_RED, RGB_BG_GRN, RGB_BG_BLU);
wprintf(L"%s", ANSI_FONT_BOLD);
wprintf(L"%s", ANSI_CRSR_HIDE);
wprintf(L"%s", ANSI_CRSR_RESET);
wprintf(L"%s", ANSI_SCRN_CLEAR);
setvbuf(stdout, 0, _IOFBF, 0);
ioctl(STDOUT_FILENO, TIOCGWINSZ, ws);
return 1;
}
}
return 0;
}
static inline void reset_term()
{
struct termios ta;
wprintf(L"%s", ANSI_FONT_RESET);
wprintf(L"%s", ANSI_CRSR_SHOW);
wprintf(L"%s", ANSI_SCRN_CLEAR);
wprintf(L"%s", ANSI_CRSR_RESET);
if (tcgetattr(STDIN_FILENO, &ta) == 0) {
ta.c_lflag |= ECHO;
if (tcsetattr(STDIN_FILENO, TCSANOW, &ta) != 0)
perror("reset_term()");
}
setvbuf(stdout, 0, _IOLBF, 0);
}
static volatile int run;
static void handle_signal(int sa)
{
switch (sa) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
run = 0;
break;
}
}
int main(int argc, char *argv[])
{
matrix mat;
struct winsize ws;
struct sigaction sa;
size_t i, j, n, nmax;
setlocale(LC_CTYPE, "");
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, 0);
sigaction(SIGQUIT, &sa, 0);
sigaction(SIGTERM, &sa, 0);
srand(time(0));
if (!init_term(&ws))
return 1;
mat = (matrix){0};
if (!init_matrix(&mat, &ws)) {
reset_term();
return 1;
}
run = 1;
n = 1; /* Used to ramp up the tracks from 1 to nmax to stagger the tracks. */
nmax = mat.collen * RHO; /* Number of rain tracks. */
while (run) {
for (i = 0; run && i < n; i++) {
if (mat.row[i] == mat.rowlen) {
recolor_head(&mat, mat.row[i] - 1, mat.col[i]);
mat.row[i] = 0;
}
if (mat.rgb[i].color[PD] == 0) {
if (mat.row[i] > 0)
draw_tail(&mat, mat.row[i] - 1, mat.col[i]);
draw_head(&mat, mat.row[i], mat.col[i]);
if (mat.row[i] == mat.rowlen - 1)
mat.rgb[i].color[PD] = 1;
mat.row[i]++;
} else if (mat.rgb[i].color[PD] > 0 &&
mat.rgb[i].color[PD] <= DECAY_MPLIER) {
blend(&mat, mat.row[i], mat.col[i]);
print(&mat, mat.row[i], mat.col[i]);
if (mat.row[i] == mat.rowlen - 1)
mat.rgb[i].color[PD]++;
mat.row[i]++;
} else {
delete_code(&mat, mat.row[i], mat.col[i]);
if (mat.row[i] == mat.rowlen - 1)
/* Track complete, start a new one. */
swap_col(&mat, i, nmax);
else
mat.row[i]++;
}
glitch(&mat);
}
if (n < nmax &&
/* Track ramp up: add a new one when the first track
* exceeds a random distance beyond 25% of the screen
* length. Do that until we reach the target density. */
mat.row[n - 1] >= rand() % (int)(mat.rowlen * 0.25)) {
mat.row[n++] = 0;
}
fflush(stdout);
usleep(DELAY_US);
}
reset_term();
destroy_matrix(&mat);
return 0;
}
|