From 5cab2b6a89097e0647295588e46901aaf049be65 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Fri, 12 Dec 2025 23:16:51 +0800 Subject: Matrix post. --- _site/feed.xml | 2 +- _site/posts.xml | 2 +- _site/projects/matrix-digital-rain/index.html | 98 +++++++++++++++++++++++---- _site/robots.txt | 2 +- _site/sitemap.xml | 30 ++++---- 5 files changed, 104 insertions(+), 30 deletions(-) (limited to '_site') diff --git a/_site/feed.xml b/_site/feed.xml index 444d5a0..ff9191d 100644 --- a/_site/feed.xml +++ b/_site/feed.xml @@ -1 +1 @@ -Jekyll2025-12-12T19:52:43+08:00/feed.xmlASCIIMX | BlogW. D. Sadeep MadurangeHow I manage Suckless software installations2025-11-30T00:00:00+08:002025-11-30T00:00:00+08:00/blog/suckless-softwareW. D. Sadeep MadurangeNeo4J A* search2025-09-14T00:00:00+08:002025-09-14T00:00:00+08:00/blog/neo4j-a-star-searchW. D. Sadeep MadurangeMOSFETs as electronic switches2025-06-22T00:00:00+08:002025-06-22T00:00:00+08:00/blog/mosfet-switchesW. D. Sadeep MadurangeHow to configure ATmega328P microcontrollers to run at 3.3V and 5V2025-04-10T00:00:00+08:002025-04-10T00:00:00+08:00/blog/arduino-unoW. D. Sadeep MadurangeHow to set up ATSAM3X8E microcontrollers for bare-metal programming in C2024-10-05T00:00:00+08:002024-10-05T00:00:00+08:00/blog/arduino-dueW. D. Sadeep Madurange \ No newline at end of file +Jekyll2025-12-12T23:16:15+08:00http://localhost:4000/feed.xmlASCIIMX | BlogW. D. Sadeep MadurangeHow I manage Suckless software installations2025-11-30T00:00:00+08:002025-11-30T00:00:00+08:00http://localhost:4000/blog/suckless-softwareW. D. Sadeep MadurangeNeo4J A* search2025-09-14T00:00:00+08:002025-09-14T00:00:00+08:00http://localhost:4000/blog/neo4j-a-star-searchW. D. Sadeep MadurangeMOSFETs as electronic switches2025-06-22T00:00:00+08:002025-06-22T00:00:00+08:00http://localhost:4000/blog/mosfet-switchesW. D. Sadeep MadurangeHow to configure ATmega328P microcontrollers to run at 3.3V and 5V2025-04-10T00:00:00+08:002025-04-10T00:00:00+08:00http://localhost:4000/blog/arduino-unoW. D. Sadeep MadurangeHow to set up ATSAM3X8E microcontrollers for bare-metal programming in C2024-10-05T00:00:00+08:002024-10-05T00:00:00+08:00http://localhost:4000/blog/arduino-dueW. D. Sadeep Madurange \ No newline at end of file diff --git a/_site/posts.xml b/_site/posts.xml index f9d56bb..803d3c7 100644 --- a/_site/posts.xml +++ b/_site/posts.xml @@ -1 +1 @@ -Jekyll2025-12-12T19:52:43+08:00/posts.xmlASCIIMXW. D. Sadeep Madurange \ No newline at end of file +Jekyll2025-12-12T23:16:15+08:00http://localhost:4000/posts.xmlASCIIMXW. D. Sadeep Madurange \ No newline at end of file diff --git a/_site/projects/matrix-digital-rain/index.html b/_site/projects/matrix-digital-rain/index.html index 666456b..b69ac2d 100644 --- a/_site/projects/matrix-digital-rain/index.html +++ b/_site/projects/matrix-digital-rain/index.html @@ -44,23 +44,97 @@

THE MATRIX DIGITAL RAIN

12 JANUARY 2024

-

The famous digital rain from The Matrix, implemented in C.

+

“All I see is blonde, brunette, red head.” The iconic digital rain from The +Matrix, implemented in C, without dependencies (not even ncurses).

-

This project is a fork of Domsson’s beautiful Fakesteak.

- -

There are three color settings: head, tail, and background. They are configured -by setting the 24-bit RGB channels using COLOR_*_RED, COLOR_*_GRN, and -COLOR_*_BLU definitions. The ghosting effect of old monochrome screens is -achieved by scaling the RGB channels. This results in a rain effect that -closely resembles the original from the first Matrix movie.

- -

In addition, this implementation supports UTF-32 character sets. The -UNICODE_MIN and UNICODE_MAX controls the Unicode block used. For -instance, setting them to 0x30A1 and 0x30F6 rains Katakana:

+

This project is a fork of Domsson’s beautiful Fakesteak. Use the following commands to compile +and run the program:

+ +
$ cc -O3 main.c -o matrix
+$ ./matrix
+
+ +

While I loved Domsson’s take on the +digital rain, what blew my mind was the minimalistic elegance of his code. As I +carefully examined it, I thought about what it might take to recreate the +original digital rain from the first Matrix movie with it. The challenge is +adding these features without destroying fakesteak’s elegance.

+ +

How does it work?

+ +

The matrix struct makes use of three 2D arrays to encode the Matrix: the +code array for 32-bit Unicode characters, the rgb array for 24-bit RGB +values of the character (foreground color), and the shade array for the +degree of transparency of the character to simulate the ghosting effect of old +monochrome displays. The dimensions of these arrays depend on the size of the +terminal screen. Each slot in the array corresponds to a cursor position on the +screen.

+ +

The ghosting effect, which is arguably the crowning feature of my version, is +implemented by carefully scaling and mixing the RGB channels:

+ +
static void mat_shade(matrix *mat, size_t row, size_t col) 
+{
+    unsigned char *color;
+    color = mat->rgb[mat_idx(mat, row, col)].color;
+    color[R] = color[R] - (color[R] - COLOR_BG_RED) / 2;
+    color[G] = color[G] - (color[G] - COLOR_BG_GRN) / 2;
+    color[B] = color[B] - (color[B] - COLOR_BG_BLU) / 2;
+}
+
+ +

The above algorithm achieves transparency by iteratively bringing the +foreground color closer to the background color with each pass of the rain. +This approach offers multiple advantages, such as simpler and more natural +color configuration (background, foreground, and the color of the first drop) +that lends itself well to Unix ricing, and of course, recreates The Matrix rain +with high fidelity.

+ +

Rather than heavy-weight graphics tool kits, we use ANSI escape codes to +control the terminal screen. It’s the effective use of the ANSI escape codes +that greatly contributes to the minimalism of the solution:

+ +
static void term_print(const matrix *mat, size_t row, size_t col)
+{
+    size_t idx;
+    idx = mat_idx(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]);
+}
+
+ +

Finally, the glitch effect is controlled by the following code:

+ +
if (mat.row[i] > 0 && rand() % 6 == 0) {
+    j = rand() % mat.row[i];
+    if (mat.code[mat_idx(&mat, j, mat.col[i])] != ' ') {
+        mat_put_code(&mat, j, mat.col[i]);
+        term_print(&mat, j, mat.col[i]);
+    }
+}
+
+ +

The above code causes glitches in the Matrix with a probablity of 1/6.

+ +

Customizing the rain

+ +

While you can customize almost any aspect of the rain including its speed, +glitch frequency, and the density of the rain, the most useful settings for +ricing are the color scheme and the character set used for the rain.

+ +

There are three color settings: the head, the tail, and the background. They +are configured by setting the COLOR_*_RED, COLOR_*_GRN, and COLOR_*_BLU +definitions in main.c. The UNICODE_MIN and UNICODE_MAX values control the +Unicode block used. For instance, setting them to 0x30A1 and 0x30F6 rains +Katakana code points:

diff --git a/_site/robots.txt b/_site/robots.txt index e087884..d297064 100644 --- a/_site/robots.txt +++ b/_site/robots.txt @@ -1 +1 @@ -Sitemap: /sitemap.xml +Sitemap: http://localhost:4000/sitemap.xml diff --git a/_site/sitemap.xml b/_site/sitemap.xml index 1b23cd3..5130d9f 100644 --- a/_site/sitemap.xml +++ b/_site/sitemap.xml @@ -1,59 +1,59 @@ -/blog/arduino-due/ +http://localhost:4000/blog/arduino-due/ 2024-10-05T00:00:00+08:00 -/blog/arduino-uno/ +http://localhost:4000/blog/arduino-uno/ 2025-04-10T00:00:00+08:00 -/blog/mosfet-switches/ +http://localhost:4000/blog/mosfet-switches/ 2025-06-22T00:00:00+08:00 -/blog/neo4j-a-star-search/ +http://localhost:4000/blog/neo4j-a-star-search/ 2025-09-14T00:00:00+08:00 -/blog/suckless-software/ +http://localhost:4000/blog/suckless-software/ 2025-11-30T00:00:00+08:00 -/projects/e-reader/ +http://localhost:4000/projects/e-reader/ 2023-10-24T00:00:00+08:00 -/projects/matrix-digital-rain/ +http://localhost:4000/projects/matrix-digital-rain/ 2024-01-12T00:00:00+08:00 -/projects/etlas/ +http://localhost:4000/projects/etlas/ 2024-09-05T00:00:00+08:00 -/projects/bumblebee/ +http://localhost:4000/projects/bumblebee/ 2025-04-02T00:00:00+08:00 -/projects/my-first-pcb/ +http://localhost:4000/projects/my-first-pcb/ 2025-07-14T00:00:00+08:00 -/projects/fpm-door-lock/ +http://localhost:4000/projects/fpm-door-lock/ 2025-10-03T00:00:00+08:00 -/about/ +http://localhost:4000/about/ -/blog/ +http://localhost:4000/blog/ -/ +http://localhost:4000/ -/projects/ +http://localhost:4000/projects/ -- cgit v1.2.3