Festive Matrix-like terminal animation

From WTFwiki
Revision as of 16:58, 13 November 2012 by Stian (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Party like it's 1999. With Neo.

Compile with -lncurses.

/* Funky Matrix-screen lookalike, (c) 1999 Stian Sletner <stian@sletner.com>
 *
 * To compile: gcc -o matrix matrix.c -lncurses
 */

#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define        DEFAULT_DELAY   5

#define        MAX_X                   256
#define        MAX_Y                   128

#define MATRICES               (COLS * 1.5)

#define        MATRIX_BRIGHT   COLOR_PAIR(1) | A_BOLD
#define        MATRIX_MIDDLE   COLOR_PAIR(1)
#define        MATRIX_DARK             COLOR_PAIR(2) | A_BOLD

int LINES, COLS;

int main(int argc, char **argv) {
       char key;
       int i, x[MAX_X], y[MAX_Y];

       if (argc != 1) {
               fprintf(stderr, "Usage: %s\n", argv[0]);
               fprintf(stderr, "   use 0-9 to change speed, q to quit\n");
               return -1;
       }

       if (LINES >= MAX_X || COLS >= MAX_Y) {
               fprintf(stderr, "Your terminal is too large, edit MAX_X and MAX_Y\n");
               return -1;
       }

       initscr();
       cbreak();
       noecho();

       if (has_colors() == FALSE) {
               fprintf(stderr, "Your terminal lacks color capability\n");
               return -1;
       }

       start_color();

       init_pair(1, COLOR_GREEN, COLOR_BLACK);
       init_pair(2, COLOR_BLACK, COLOR_BLACK);

       halfdelay(DEFAULT_DELAY);
       curs_set(0);

       srand(time(NULL));

       for (i = 0; i < MATRICES; i++) {
               y[i] = rand() % LINES;
               x[i] = rand() % COLS;
       }

       while ((key = getch()) != 'q') {
               if (key < '9' && key > '0')
                       halfdelay(key - '0');

               for (i = 0; i < MATRICES; i++) {
                       y[i]++;

                       if (y[i] >= LINES + 6) {
                               y[i] = 0;
                               x[i] = rand() % COLS;
                       }

                       attrset(MATRIX_BRIGHT);
                       mvaddch(y[i], x[i], rand() % 10 + '0');

                       attrset(MATRIX_MIDDLE);
                       mvaddch(y[i] - 3, x[i], rand() % 10 + '0');

                       attrset(MATRIX_DARK);
                       mvaddch(y[i] - 6, x[i], rand() % 10 + '0');
               }

               refresh();
       }

       endwin();

       return 0;
}