1
- // Matrix-style lines moving from top to bottom.
1
+ // Matrix-style runners moving from top to bottom.
2
2
// However, in random colors!
3
3
//
4
4
// Copyright (c) 2019, Adrian "vifino" Pistol <vifino@tty.sh>
21
21
#include <random.h>
22
22
#include <stddef.h>
23
23
#include <stdlib.h>
24
+ #include <stdio.h>
24
25
25
26
#define FPS 30
26
27
#define FRAMETIME (T_SECOND / FPS)
@@ -33,60 +34,66 @@ static oscore_time nexttick;
33
34
static int mx ;
34
35
static int my ;
35
36
36
- typedef struct line {
37
+ typedef struct runner {
37
38
RGB color ;
38
39
int pos_x ;
39
40
int pos_y ;
40
41
int speed ;
41
- int length ;
42
- } line ;
43
-
44
- static int numlines ;
45
- static line * lines ;
46
-
47
- static RGB black = RGB (0 , 0 , 0 );
48
-
49
- static void randomize_line (int line ) {
50
- lines [line ].color = HSV2RGB (HSV (randn (255 ), 255 , 255 )); // HSV instead of RGB to get sameish brightness but differing color.
42
+ } runner ;
43
+
44
+ static int numrunners ;
45
+ static runner * runners ;
46
+ static int HYPERSPEED = 3 ;
47
+
48
+
49
+ static void randomize_runner (runner * runner ) {
50
+ runner -> color = HSV2RGB (HSV (randn (255 ), 255 , 255 ));
51
+ if (randn (4 )== 0 ){
52
+ runner -> pos_y = 0 ;
53
+ } else if (runner -> pos_y != my ){
54
+ runner -> pos_y = randn (my - 1 );
55
+ } else {
56
+ runner -> pos_y = randn (my /8 );
57
+ }
58
+ runner -> pos_x = randn (mx - 1 );
59
+ runner -> speed = randn (2 )+ 1 ;
60
+ }
51
61
52
- lines [line ].pos_y = - randn (my /4 );
53
- lines [line ].pos_x = randn (mx - 1 );
62
+ static RGB greenify (int intensity ){
63
+ if (intensity > 511 ) intensity = 511 ;
64
+ if (intensity < 256 ) {
65
+ return RGB (0 ,intensity ,intensity /2 );
66
+ //return RGB(0,intensity/2,intensity);
67
+ } else {
68
+ return RGB (intensity - 256 ,255 ,intensity /2 );
69
+ //return RGB(intensity-256,255,intensity/2);
70
+ }
71
+ }
54
72
55
- int speed = 0 ;
56
- while (speed == 0 )
57
- speed = randn (2 );
58
- lines [line ].speed = speed ;
73
+ static int degreenify (RGB px ){
74
+ if (px .red ){
75
+ return px .red + 256 ;
76
+ } else {
77
+ return px .green ;
78
+ }
79
+ }
59
80
60
- int length = 0 ;
61
- while (length < (my /4 ))
62
- length = randn (my /2 );
63
- lines [line ].length = length ;
81
+ int out_of_bounds (int x , int y ){
82
+ return (x >= mx ) || (x < 0 ) || (y >= my ) || (y < 0 );
64
83
}
65
84
66
- static void randomize_lines () {
67
- int line ;
68
- for (line = 0 ; line < numlines ; ++ line ) {
69
- randomize_line (line );
70
- }
85
+ void matrix_set_safe (int x , int y , RGB color ){
86
+ if (out_of_bounds (x ,y )) return ;
87
+ matrix_set (x ,y ,color );
71
88
}
72
89
73
- static void update_lines () {
74
- int line ;
75
- int y ;
76
- for (line = 0 ; line < numlines ; ++ line ) {
77
- y = lines [line ].pos_y + lines [line ].speed ;
78
-
79
- if ((y - lines [line ].length ) > my ) {
80
- // clear old line
81
- /*int py;
82
- for (py = y; py >= (y - lines[line].length); py--)
83
- if (py < my)
84
- matrix_set(lines[line].pos_x, py, black);*/
85
- randomize_line (line );
86
- } else {
87
- lines [line ].pos_y = y ;
88
- }
89
- }
90
+ void run_runner (runner * runner ){
91
+ if (!randn (runner -> speed )) runner -> pos_y ++ ;
92
+ if (out_of_bounds (runner -> pos_x , runner -> pos_y )){
93
+ randomize_runner (runner );
94
+ } else {
95
+ matrix_set (runner -> pos_x , runner -> pos_y , greenify (400 ));
96
+ }
90
97
}
91
98
92
99
int init (int moduleno , char * argstr ) {
@@ -96,10 +103,14 @@ int init(int moduleno, char* argstr) {
96
103
if ((mx * my ) < 16 )
97
104
return 1 ;
98
105
99
- numlines = mx / 4 ; // not sure if this is the best thing to do, but meh.
100
- lines = malloc (numlines * sizeof (line ));
106
+ numrunners = mx /2 ; // not sure if this is the best thing to do, but meh.
107
+ printf ("numrunners %d\n" ,numrunners );
108
+ runners = malloc (numrunners * sizeof (runner ));
101
109
102
- randomize_lines ();
110
+ int runner ;
111
+ for (runner = 0 ; runner < numrunners ; ++ runner ){
112
+ randomize_runner (& (runners [runner ]));
113
+ }
103
114
104
115
modno = moduleno ;
105
116
frame = 0 ;
@@ -108,29 +119,26 @@ int init(int moduleno, char* argstr) {
108
119
109
120
void reset (int _modno ) {
110
121
nexttick = udate ();
111
- matrix_clear ();
112
-
122
+ //matrix_clear();
113
123
frame = 0 ;
114
124
}
115
125
116
- int draw (int _modno , int argc , char * argv []) {
117
- int line ;
118
- // clear out old points of lines
119
- for (line = 0 ; line < numlines ; ++ line ) {
120
- int y = lines [line ].pos_y - lines [line ].length ;
121
- int speed ;
122
- for (speed = 0 ; speed <= lines [line ].speed ; speed ++ )
123
- if ((y - speed ) >= 0 && (y - speed ) < my )
124
- matrix_set (lines [line ].pos_x , y - speed , black );
125
- }
126
126
127
- // update the lines and draw them
128
- update_lines (); // todo, move back below matrix_render, to get a more consistant framerate
129
- for (line = 0 ; line < numlines ; ++ line ) {
130
- int y ;
131
- for (y = lines [line ].pos_y - lines [line ].length ; y <= lines [line ].pos_y ; y ++ )
132
- if (y >= 0 && y < my )
133
- matrix_set (lines [line ].pos_x , y , lines [line ].color );
127
+ int draw (int _modno , int argc , char * argv []) {
128
+ int x ,y ;
129
+ for (x = 0 ; x < mx ; ++ x ){
130
+ for (y = 0 ;y < my ; ++ y ){
131
+ int intense_px = degreenify (matrix_get (x ,y ))* (256 - HYPERSPEED * 5 )/256 ;
132
+ RGB res = greenify (intense_px );
133
+ matrix_set (x ,y ,res );
134
+ }
135
+ }
136
+ int runner ;
137
+ int dont_care ;
138
+ for (runner = 0 ; runner < numrunners ; ++ runner ) {
139
+ for (dont_care = 0 ;dont_care < HYPERSPEED ; dont_care ++ ){
140
+ run_runner (& (runners [runner ]));
141
+ }
134
142
}
135
143
136
144
matrix_render ();
@@ -146,5 +154,5 @@ int draw(int _modno, int argc, char* argv[]) {
146
154
}
147
155
148
156
void deinit (int _modno ) {
149
- free (lines );
157
+ free (runners );
150
158
}
0 commit comments