Mon, 8th Sep 2008 04:48:53
Never fear, this site is here

#nulldigital.c

Language: C
Written by doug on 2008-01-29 23:21:40

/*
 * author: kay ~ irc.nullnetwork.net
 * date: 20/04/2006
 * ncurses animation thing.
 * purpose: keep me entertained, if u dont like it
 * dont run it. mmk?
 * 
 * gcc -o nulldigital nulldigital.c -lncurses
 */

#include <stdio.h>
#include <stdlib.h> 
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#include <signal.h>


/* put the following in a file called ./nulldigital.asc
 * 
 * --------------------------------------------------
1111111111111111111111111111111111111111111111111111111
1111111111110011100100111001001111100111111111111111111
11111111111100011N0100111001001111100111111111111111111
1111111111110000100100111001L01111100111111111111111111
1111111111110010000100111001001111100111111111111111111
1111111111110011000100010U0100111110L111111111111111111
1111111111110011100110000011000000100000011111111111111
1111111111111111111111111111111111111111111111111111111
11100000110000I0110000110000001000000110000111001111111
1110D110011100111001100111001111100111001100110L1111111
111001110011001100111111110I111110011001111001001111111
111001110011001100110G011100111110011000000A01001111111
111001100111001110011101110011111T011001111001001111111
1110000011000000110000010000001110011001111001000000111
1111111111111111111111111111111111111111111111111111111
 * --------------------------------------------------
 *
 */

#define getrnd(x) rand() % x

char ** string2map(char * s, int slen, int x, int y) {
char ** map;
int i, ay = 0, ax = 0;
	map = (char**)calloc(y, 4);
	for(i = 0; i < slen; i++) {
		
		if (!(*(map+ay))) *(map+ay) = (char*)calloc(x+1, 1);
		
		if (s[i] == '\n' || ax > x) {
			ay++;
			ax = 0;
		} else {
			map[ay][ax] = s[i];
			ax++;
		}
	}
	return map;
}

int draw(char ** map, int factor, int x, int y) {
int i, j, c;
attr_t attrs;	
	//system("clear"); // HACK!
	for(i = 0; i < y; i++) {
		for(j = 0; j < x; j++) {
			if (getrnd(1000) > factor) {
				c = getrnd(2);
				
				if (c == 1) {
					attrs = COLOR_PAIR(1);
				} else if (c == 0) {
					attrs = COLOR_PAIR(2);
				}
				attron(attrs);
				mvprintw(i, j, "%d", c);
				attroff(attrs);
			} else {
				
				if (map[i][j] == '1') {
					attrs = COLOR_PAIR(1);
				} else if (map[i][j] == '0') {
					attrs = COLOR_PAIR(2);
				} else {
					attrs = COLOR_PAIR(3);
				}
				attron(attrs);
				mvprintw(i, j, "%c", map[i][j]);
				attroff(attrs);
			}
		}
		mvprintw(i, x, "\n");
	}
	mvprintw(y, x, "\n");
	refresh();
}


// b - banner
// blen - strlen(banner)
int getbannerxy(char * b, int blen, int * x, int * y) {
int i = 0, tempx = 0, hix = 0;
	*y = 0;
	
	for(i = 0; i < blen; i++) {
		if (b[i] == '\n') {
			if (tempx > hix) hix = tempx;
			(*y)++;
			tempx = 0;
		} else {
			tempx++;
		}
	}
	*x = hix;
}

char * getbanner(char * filename) {
char * ret;
int size = 0;
FILE * fp = fopen(filename, "r");
	if (!fp) return NULL;
	
	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	if (size <= 0) return NULL;
	ret = (char*)malloc(size+1);
	fseek(fp, 0, SEEK_SET);
	
	fread(ret, 1, size, fp);
	
	ret[size] = '\0';
	fclose(fp);
	return ret;
}


void signalh(int sig) {
	endwin();
	exit(1);
}

int main() {
int i = 0, x = 55, y = 15, j = 100;
char ** map, * banner;
int row, col;
	
	signal(SIGINT, signalh);
	
	banner = getbanner("./nulldigital.asc");
	
	if (!banner) {
		printf("Error reading banner\n");
		return 1;
	}
	
	initscr();
	noecho();
	keypad(stdscr, TRUE);
	cbreak();
	getmaxyx(stdscr, row, col);
	
	start_color();
	init_pair(1, COLOR_WHITE, COLOR_BLACK);
	init_pair(2, COLOR_RED, COLOR_BLACK);
	init_pair(3, COLOR_BLUE, COLOR_BLACK);
	
	
	getbannerxy(banner, strlen(banner), &x, &y);
	
	if (x > col || y > row) {
		endwin();
		printf("Your terminal is too small for this program\n");
		return 1;
	} 
	
	map = string2map(banner, strlen(banner), x, y);
	
	srand((unsigned)time(NULL));
	
	for(i = 0; i <= 1000; i += j) {
		draw(map, i, x, y);
		j -= j/2;
		
	}
	free(map);
	free(banner);
	
	sleep(3);
	endwin();
	return 0;
}
Powered by Debian, Jack Daniels, Guinness, and excessive quantities of caffeine and sugar.