


//also see reference at http://www.talideon.com/notes/dcom3/curses.pdf


#include <unistd.h>
#include <stdlib.h>
#include <curses.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <iostream.h>
#include <time.h>


double getNOWTime() {
  struct timeval tv;
   gettimeofday(&tv, NULL);
   return tv.tv_sec;
   }

//*******************************************

int main()
{
	int s=7;
	int x[s],y[s],dx[s],dy[s],c[s];
	int i, t;
	int MAXX, MAXY;
	int length = 30;
	WINDOW *new_win_ptr;

	initscr();
	noecho();
	
	if(has_colors() == FALSE){
		printf("No color support\n");
		exit(1);
	}

	start_color();
	init_pair(1, COLOR_WHITE, COLOR_BLACK);
	init_pair(2, COLOR_GREEN, COLOR_BLACK);
	attrset(COLOR_PAIR(1));

	getmaxyx(stdscr, MAXY, MAXX);

	srand(time(NULL));

	// *** Play around with the standard screen:
	// *** Randomly pick some char positions and place a random character

	int count;

	attrset(COLOR_PAIR(1));

	for (int a=0; a<=int((MAXX * MAXY)/60); a++)
	{
		for(i=0; i<s; i++){
			x[i] = (int) (double(MAXX)*rand()/(RAND_MAX+1.0));
			y[i] = (int) (double(MAXY-20)*rand()/(RAND_MAX+1.0));
			dx[i] = (int) (double(MAXX)*rand()/(RAND_MAX+1.0));
			dy[i] = (int) (double(MAXY-20)*rand()/(RAND_MAX+1.0));
			t = (int) (6.0*rand()/(RAND_MAX+1.0));	//randomizes number of blank characters
		}
	  
		count = 0;
	  
	  
		while( /*(y<MAXY) && */ (count < length)){
	    
			for (i=0;i<s;i++){
				y[i]++;			//move curser down
				move(y[i],x[i]);

			if ((count > (i*t))&&(count<(length-i))&&(y[i]<MAXY)){
				/***Print white character to screen***/
				attrset(COLOR_PAIR(1));
				c[i] = 33 + (int) (94.0*rand()/(RAND_MAX+1.0));
				addch(c[i]);
			}
			else if(y[i]<MAXY)
				/***Write blank character instead of white character***/
				addch(' ');
		      
			/***Delete random columns of characters***/
			dy[i]++;
			move(dy[i],dx[i]);
			addch(' ');  
			}
	    
			refresh();
	    
			for(i=0;i<3;i++){
				usleep(20);
			}
	    
			for (i=0;i<s;i++){
				if((count!=0)&&(count>(i*t))&&(count<(length-i))&&(y[i]<MAXY)){
					/***Print green character to screen)***/
					attrset(COLOR_PAIR(2));
					move(y[i],x[i]);
					addch(c[i]);
					refresh();    
				}
			}
	    
			count++;
		}
	}


	// *** Invert the screen:

	flash();
	sleep(1);

	// *** and now clear it:
	clear();
	refresh();



	endwin();
	return 1;
}

