-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c.backup
305 lines (241 loc) · 7.17 KB
/
matrix.c.backup
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
/* There is something very wrong with deleting the last element. I suspect
* that previous elements are not conscious of this delete. Obvious leak.
*
* */
#include <stdlib.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include "vroot.h"
#include <time.h>
#define MAXSPEED 100
// Indexes of valid matrix symbols in array symbols are between 0 and 63.
#define SYMS 64
// Acquired 19 using the two following lines:
// int test = XTextWidth(fs, symbols, 63);
// fprintf(stderr, "lineheight: %d", fs->ascent + fs->descent);
#define LINEHEIGHT 19
// Acquired by printing width of each character.
#define LINEWIDTH 10
// falling->movTime has to reach MOVTHRESH so that code can move down.
#define MOVTHRESH 100
// GENPROB/1000 is the probability that a frame creates a new falling code.
#define GENPROB 1000
// FALLDEATH/1000 is the probability that a falling code dies.
#define FALLDEATH 10
// The maximum amount of frames the tail of a falling code will exist.
#define MAXTAILLIFE 250
char symbols[64] = "098754321abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+=-[]';/.,<>?:\"{}";
int sizeFall = 0;
int sizeStill = 0;
typedef struct Falling{
int sym;
//int coprime;
int x;
int y;
int speed;
int movTime;
int tailLife;
struct Falling *next;
} Falling;
typedef struct Still{
int sym;
int x;
int y;
int time;
int lifespan;
struct Still *next;
} Still;
void addFalling(Falling **head, int nCols, int nRows){
Falling *old = *head;
(*head) = malloc(sizeof(Falling));
(*head)->sym = rand()%SYMS;
//(*head)->coprime = 2*(rand()%31)+1;
(*head)->x = (rand()%nCols) * LINEWIDTH;
(*head)->y = (rand()%nRows) * LINEHEIGHT;
// Low speeds should be rare.
(*head)->speed = (rand()%MAXSPEED + rand()%MAXSPEED) / 2;
(*head)->movTime = 0;
(*head)->tailLife = rand()%MAXTAILLIFE+1;
(*head)->next = old;
sizeFall++;
}
void deleteFalling(Falling **pb){
Falling *b = *pb;
*pb = (*pb)->next;
free(b);
sizeFall--;
return;
}
void addStill(Still **head, int x, int y, int lifespan){
Still *old = *head;
(*head) = malloc(sizeof(Still));
(*head)->sym = rand()%SYMS;
(*head)->x = x;
(*head)->y = y;
(*head)->time = 0;
(*head)->lifespan = lifespan;
(*head)->next = old;
sizeStill++;
}
void deleteStill(Still **pb){
Still *b = *pb;
*pb = (*pb)->next;
free(b);
sizeStill--;
return;
}
Still *headStill;
Falling *headFall;
void printList(Falling *headFall){
Falling *try = headFall;
fprintf(stderr, "##################\n");
while(try != NULL){
fprintf(stderr, "%c, ", try->sym);
try = try->next;
}
if (try == NULL)
fprintf(stderr, "NULL.");
fprintf(stderr, "\n");
}
int updateStill(Display *dpy, Window root, GC g, XFontStruct *fs, XColor blacks, XColor greens, Still **tryStill){
// Clean last movement
int cleanWidth = XTextWidth(fs, symbols + (*tryStill)->sym, 1);
int cleanHeight = fs->ascent + fs->descent;
XSetForeground(dpy, g, blacks.pixel);
XFillRectangle(dpy, root, g, (*tryStill)->x,
(*tryStill)->y - fs->ascent, LINEWIDTH,
fs->ascent + fs->descent);
(*tryStill)->time++;
// Change symbol displayed
//(*tryFall)->sym = rand()%SYMS;
if ((*tryStill)->sym >= 63)
(*tryStill)->sym= 0;
else
(*tryStill)->sym++;
if ((*tryStill)->time == (*tryStill)->lifespan){
/*int cleanWidth = XTextWidth(fs, symbols + (*tryStill)->sym, 1);
int cleanHeight = fs->ascent + fs->descent;
XSetForeground(dpy, g, blacks.pixel);
XFillRectangle(dpy, root, g, (*tryStill)->x,
(*tryStill)->y - fs->ascent, LINEWIDTH,
fs->ascent + fs->descent);*/
deleteStill(tryStill);
return 1;
}
// Final draw
//if((*tryStill)->time == 1){
XSetForeground(dpy, g, greens.pixel);
XDrawString(dpy, root, g, (*tryStill)->x, (*tryStill)->y,
symbols + (*tryStill)->sym, 1);
//}
tryStill = &(*tryStill)->next;
return 0;
}
int updateFalling(Display *dpy, Window root, GC g, XFontStruct *fs, XColor blacks, XColor whites, XColor greens, int height, Falling **tryFall){
// Clean last movement
int cleanWidth = XTextWidth(fs, symbols + (*tryFall)->sym, 1);
int cleanHeight = fs->ascent + fs->descent;
XSetForeground(dpy, g, blacks.pixel);
XFillRectangle(dpy, root, g, (*tryFall)->x,
(*tryFall)->y - fs->ascent, LINEWIDTH,
fs->ascent + fs->descent);
// Used to acquire LINEWIDTH.
// fprintf(stderr, "cleanWidth: %d\n", cleanWidth);
// Die randomly or due to screen exit
if(rand()%10000 < 80 || (*tryFall)->y > height){
deleteFalling(tryFall);
return 1;
}
// Movement
(*tryFall)->movTime += (*tryFall)->speed;
if ((*tryFall)->movTime >= MOVTHRESH){
(*tryFall)->movTime = 0;
// Create tail
addStill(&headStill, (*tryFall)->x, (*tryFall)->y,
(*tryFall)->tailLife);
updateStill(dpy, root, g, fs, blacks, greens, &headStill);
(*tryFall)->y = (*tryFall)->y + LINEHEIGHT;
}
// Change symbol displayed
//(*tryFall)->sym = rand()%SYMS;
if ((*tryFall)->sym >= 63)
(*tryFall)->sym = 0;
else
(*tryFall)->sym++;
// Final draw
XSetForeground(dpy, g, whites.pixel);
XDrawString(dpy, root, g, (*tryFall)->x, (*tryFall)->y,
symbols + (*tryFall)->sym, 1);
tryFall = &(*tryFall)->next;
return 0;
}
main (){
Display *dpy;
Window root;
XWindowAttributes wa;
GC g;
Font f;
XFontStruct *fs;
XGCValues v;
XColor whitex, whites, greenx, greens, blackx, blacks;
int nCols, nRows;
srand(time(NULL));
/* open the display (connect to the X server) */
dpy = XOpenDisplay (getenv ("DISPLAY"));
/* get the root window */
root = DefaultRootWindow (dpy);
/* get attributes of the root window */
XGetWindowAttributes(dpy, root, &wa);
/* create a GC for drawing in the window */
g = XCreateGC (dpy, root, 0, NULL);
/* load a font */
f=XLoadFont(dpy, "-*-matrix code nfi-*-*-*-*-*-120-*-*-*-*-*-*");
XSetFont(dpy, g, f);
/* get font metrics */
XGetGCValues (dpy, g, GCFont, &v);
fs = XQueryFont (dpy, v.font);
// Allocate white
XAllocNamedColor(dpy,
DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
"white",
&whites, &whitex);
// Allocate black
XAllocNamedColor(dpy,
DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
"black",
&blacks, &blackx);
// Allocate green
XAllocNamedColor(dpy,
DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)),
"green",
&greens, &greenx);
/* set foreground color */
XSetForeground(dpy, g, whites.pixel);
// Create lists of falling and stills.
headFall = NULL;
headStill = NULL;
nCols = wa.width / LINEWIDTH;
nRows = wa.height / LINEHEIGHT;
//XFlush(dpy);
while (1) {
// Generate new falling code
if (rand()%FALLDEATH < GENPROB)
addFalling(&headFall, nCols, nRows);
// Update stills
Still **tryStill = &headStill;
while((*tryStill) != NULL){
if (!updateStill(dpy, root, g, fs, blacks, greens, tryStill))
tryStill = &(*tryStill)->next;
}
// Update falling
Falling **tryFall = &headFall;
while((*tryFall) != NULL){
if (!updateFalling(dpy, root, g, fs, blacks, whites, greens, wa.height, tryFall))
tryFall = &(*tryFall)->next;
}
/* flush changes and sleep */
XFlush(dpy);
usleep (55000);
}
XCloseDisplay (dpy);
}