-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
83 lines (73 loc) · 2.06 KB
/
matrix.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
#include <stdlib.h>
#include "ui.h"
#define NUM_DRIPS 250
#define PROB_DRIP_SPAWN 0.65
#define PROB_DIM 0.55
#define PROB_CHANGE 0.95
#define RANDOM_PRINTABLE_CHARACTER (33 + (rand() % 94))
typedef struct {
int x, y;
bool live;
bool bright;
} drip;
cell matrix[MAXX][MAXY];
drip drips[NUM_DRIPS];
double rand01() {
return (double)rand() / (double)RAND_MAX;
}
void init_drips() {
for(int i = 0; i < NUM_DRIPS; i++) drips[i].live = false;
}
void matrix_init() {
// set the matrix all to black
// move this later to matrix.c
// x, y instead of i, j
for(int x = 0; x < MAXX; x++) {
for(int y = 0; y < MAXY; y++) {
matrix[x][y].char_value = 0;
matrix[x][y].intensity = 0;
}
}
// init drips
init_drips();
}
void fade_n_change_matrix() {
for(int x = 0; x < MAXX; x++) {
for(int y = 0; y < MAXY; y++) {
// randomly change characters -- even invisible ones
if(rand01() < PROB_CHANGE || matrix[x][y].char_value == 0)
matrix[x][y].char_value = RANDOM_PRINTABLE_CHARACTER;
// randomly dim the cells
if(rand01() < PROB_DIM && matrix[x][y].intensity > MIN_INTENSITY)
matrix[x][y].intensity--;
}
}
}
void try_add_drips() {
for(int i = 0; i < NUM_DRIPS; i++) {
if(!drips[i].live) {
drips[i].live = true;
drips[i].x = rand() % MAXX;
drips[i].y = 0;
drips[i].bright = rand() % 2;
return;
}
}
}
void update_drips() {
for(int i = 0; i < NUM_DRIPS; i++) {
if(drips[i].live) {
if(drips[i].bright)
matrix[drips[i].x][drips[i].y].intensity = MAX_INTENSITY;
else
matrix[drips[i].x][drips[i].y].intensity = MIN_INTENSITY;
// drips die when they leave the screen
if (++ drips[i].y >= MAXY-1) drips[i].live = false;
}
}
}
void matrix_update() {
if(rand01() < PROB_DRIP_SPAWN) try_add_drips();
update_drips();
fade_n_change_matrix();
}