File: matrix.c

package info (click to toggle)
asciijump 1.0.2~beta-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 880 kB
  • sloc: ansic: 4,665; sh: 2,582; makefile: 190
file content (185 lines) | stat: -rw-r--r-- 2,994 bytes parent folder | download | duplicates (6)
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
/* :: asciijump (client), gnu gpl v 2
   :: copyright (c) grzegorz moskal, g.moskal@opengruop.org */

#define MATRIX_C
#include "matrix.h"


#define START (53)
static struct column *first, *last;

static int mx_width;
static int mx_height;
static int mx_x;
static int mx_y;
static int mx_color_normal;
static int mx_color_shine;
int mx_mode = SNOW;

static void col_flush(struct column *c)
{
	int i = 0;

	for (; i < mx_height; i++)
		c->caption[i] = ' ';
	c->caption[i] = '\0';
	
	c->last_y = 0;
	c->height = 0;
	c->shine = (d(5) == 5);
	c->stop = 1;
	c->draw = 1;
	c->cut = 0;
	c->async = 0;
	c->async_pattern = d(2)+1;
}

void mx_init(int x, int y, int w, int h)
{
	struct column *c;
	int i = 0;

	mx_x = x;
	mx_y = y;
	mx_width = w;
	mx_height = h;
	
	mx_color_normal = (mx_mode < MATRIX_FULL) ? Black : Grey;
	mx_color_shine = (mx_mode < MATRIX_FULL) ? Grey : White;

	
	for (; i < w; i++) {
		c = xmalloc(sizeof(struct column));
		c->height = h;
		c->caption = xmalloc(sizeof(char)*(c->height+1));
		c->next = 0;
		col_flush(c);
		if (first) {
			last->next = c;
			last = last->next;
		} else
			first = last = c;
	}
	for (i = 40; i; i--)
		mx_service();
}

void mx_free()
{
	struct column *tmp, *c = first;
	while (c) {
		tmp = c;
		SWITCH(c);
		xfree(tmp->caption);
		xfree(tmp);
	}
	first = last = NULL;
}

static void col_check_grow(struct column *c)
{
	int p = -1;
	int i = mx_height/2;

	for (; i < mx_height; i+=4)
		if (c->height == i)
			p = 3*i;
	
	if (d(100) <= p)  
		c->cut = 1;
}

static void col_service(struct column *c)
{
	if (c->stop) {
		c->stop = d(START)-1;
		return;
	}

	if (c->async > 0)
		c->async--;
	else {
		c->async = d(c->async_pattern);
		if (c->draw) {
			col_check_grow(c);
			c->caption[c->last_y] = d(93) + 33;
			c->last_y++;
			c->height++;
		} 
		if (c->cut)
			c->caption[c->last_y - c->height--] = ' ';
	}

	if (c->last_y >= mx_height-1) {
		c->draw = 0;
		c->cut = 1;
	}
	
	if (c->height == 0 && c->cut == 1) 
		col_flush(c);
}

void mx_service(void)
{
	struct column *c = first;

	for (c = first; c; c = c->next)
		col_service(c);

	for (c = first; c; c = c->next)
		col_service(c);
}

static void col_draw(struct column *c, int x)
{
	char *tab = ".";
	int len = strlen(tab);
	int h = 0;
	
	sl_color(mx_color_normal);
	
	for (; h < mx_height; h++) {
		sl_goto(x, h+mx_y);
		
		// if it is not the last
		if (h+1 != c->last_y) {
			sl_putch(c->caption[h]);
			continue;
		}
		// elsewere
		sl_color(mx_color_shine);
		if (mx_mode != SNOW)
			sl_putch(c->caption[h]);
		else 
			sl_putch(tab[d(len)-1]);
		sl_color(mx_color_normal);
	}
}

void mx_draw(void)
{
	struct column *c = first;
	int x = mx_x;
	
	for (; c; c = c->next) 
		col_draw(c, x++);
}

void mx_move(int n)
{
	struct column *tmp;
	while (n--) {
		tmp = first;
		first = first->next;
		last->next = tmp;
		last = last->next;
		last->next = 0;
	}
}

void mx_change_mode(int c_normal, int c_shine, int mode)
{
	mx_mode = mode;
	mx_color_normal = c_normal;
	mx_color_shine = c_shine;
}