mirror of
https://github.com/PoetryInCode/matrix.git
synced 2025-04-19 09:57:13 -04:00
First update in 4 years
This commit is contained in:
parent
5361ff3af1
commit
aca854fa12
25
Makefile
25
Makefile
@ -1,31 +1,16 @@
|
||||
all: build
|
||||
|
||||
CC=clang
|
||||
STD=gnu99
|
||||
OPT=-Os
|
||||
DBG=-g -ggdb
|
||||
OTHER=
|
||||
|
||||
CMD=$(CC) $(DBG) -std=$(STD) $(OPT) $(OPT)
|
||||
LINK=-lncurses
|
||||
|
||||
FINBIN=bin/matrix
|
||||
CFLAGS=-lncurses
|
||||
|
||||
install: build ;
|
||||
cp -u $(FINBIN) /usr/local/bin/
|
||||
|
||||
build:
|
||||
$(CMD) ./src/main.c $(LINK) -o ./bin/matrix
|
||||
build: ./matrix
|
||||
|
||||
valgrind: build ;
|
||||
valgrind ./$(FINBIN)
|
||||
./matrix: ./matrix.c
|
||||
|
||||
clean:
|
||||
rm -rf ./bin/*
|
||||
|
||||
clean-logs:
|
||||
rm ./vgcore.*
|
||||
rm ./massif.out.*
|
||||
rm -f ./matrix
|
||||
|
||||
run: bin/matrix ;
|
||||
./bin/matrix --tickTime 15 --streamLen 20
|
||||
./bin/matrix --tickTime 15 --streamLen 20
|
||||
|
@ -1,5 +1,11 @@
|
||||
# Matrix
|
||||
A matrix program to get some practice with C and the [ncurses](https://github.com/mirror/ncurses) library
|
||||
A matrix program to get some practice with C and the [ncurses](https://github.com/mirror/ncurses) library.
|
||||
|
||||
This project just recently turned 4 years old, and was one of my first C projects!
|
||||
I have been using it as a screen saver for my office all this time.
|
||||
|
||||
After staring at it running on this old CRT monitor in my office off of a Mac trash can (aka. 2013 Mac Pro)
|
||||
that is running Void Linux. I decided that it could do with a makeover.
|
||||
|
||||
# Features
|
||||
|
||||
|
BIN
bin/matrix
BIN
bin/matrix
Binary file not shown.
188
matrix.c
Normal file
188
matrix.c
Normal file
@ -0,0 +1,188 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
#include <curses.h>
|
||||
#include <string.h>
|
||||
|
||||
#define UP (1)
|
||||
#define DOWN (2)
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
int
|
||||
help()
|
||||
{
|
||||
puts("Program Flags:");
|
||||
printf("\t--tickTime [integer]\n\t\tWill change the time waited between frame updates in miliseconds\n\n");
|
||||
printf("\t--streanLen [integer]\n\t\tChanges the vertical length of the stream in characters\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int time = 15;
|
||||
int initTailLen = 20;
|
||||
for(int i=1; i<argc; i++) {
|
||||
if(strcmp(argv[i],"--help") == 0 || strcmp(argv[i], "-h") == 0) {
|
||||
endwin();
|
||||
help();
|
||||
return 1;
|
||||
} else {
|
||||
if(strcmp(argv[i],"--tickTime") == 0) {
|
||||
time=atoi(argv[i+1]);
|
||||
i++;
|
||||
} else {
|
||||
if(strcmp(argv[i],"--streamLen") == 0) {
|
||||
initTailLen=atoi(argv[i+1]);
|
||||
i++;
|
||||
} else {
|
||||
endwin();
|
||||
printf("[ERROR] Invalid flag\n\n");
|
||||
help();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WINDOW *term = initscr();
|
||||
start_color();
|
||||
init_pair(1,COLOR_GREEN,COLOR_BLACK);
|
||||
attron(COLOR_PAIR(1));
|
||||
noecho();
|
||||
if(nodelay(term,TRUE)==ERR) {
|
||||
puts("[ERROR] could not enter no-delay mode");
|
||||
return ERR;
|
||||
}
|
||||
timeout(time);
|
||||
|
||||
int maxX = COLS;
|
||||
int maxY = LINES;
|
||||
|
||||
int tailLen[maxX];
|
||||
|
||||
for(int i=0; i<maxX; i++) {
|
||||
int num=rand()%2;
|
||||
if(num>1) {
|
||||
tailLen[i]=initTailLen+(rand()%(4%initTailLen));
|
||||
} else {
|
||||
if(num<=1) {
|
||||
tailLen[i]=initTailLen-(rand()%initTailLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
int cascade[maxX];
|
||||
int tail[maxX];
|
||||
int jmpTail[maxX];
|
||||
|
||||
for(int i=0; i<maxX; i++)
|
||||
cascade[i]=ERR;
|
||||
|
||||
int pause = 0;
|
||||
int dir = DOWN;
|
||||
int limit = maxY;
|
||||
int startPos = 0;
|
||||
int get = getch();
|
||||
while(get!='q') {
|
||||
if(get=='s') {
|
||||
pause=TRUE;
|
||||
}
|
||||
int xPos = rand()%maxX;//get random x position for the stream
|
||||
if(cascade[xPos]==ERR) {//make sure the stream is set to active
|
||||
cascade[xPos]=startPos;
|
||||
}
|
||||
for(int i=0; i<maxX; i++) {//move the streams up/down
|
||||
if(!pause) {
|
||||
if(cascade[i]!=ERR) {
|
||||
if(dir==DOWN) {
|
||||
tail[i] = cascade[i]-tailLen[i];
|
||||
} else {
|
||||
tail[i] = cascade[i]+tailLen[i];
|
||||
}
|
||||
if(cascade[i]==limit) {//check if the position is at the bottom of the screen
|
||||
cascade[i]=ERR;//set the position to ERR if it is at the bottom
|
||||
} else {
|
||||
if(rand()%10==1) {//check if the stream will jump
|
||||
if(!(jmpTail[i]<=maxY)) {
|
||||
if(dir==DOWN) {
|
||||
cascade[i]=cascade[i]+rand()%8;
|
||||
jmpTail[i]=cascade[i]-tailLen[i];
|
||||
} else {
|
||||
cascade[i]=cascade[i]-rand()%8;
|
||||
jmpTail[i]=cascade[i]+tailLen[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(dir==DOWN) {
|
||||
cascade[i]++;//increase the position
|
||||
} else {
|
||||
cascade[i]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(tail[i]!=limit) {//keep iterating the tail intill it is at the limit
|
||||
if(dir==DOWN) {
|
||||
tail[i]++;
|
||||
} else {
|
||||
tail[i]--;
|
||||
}
|
||||
}
|
||||
if(jmpTail[i]==maxY || jmpTail[i]==0) {
|
||||
jmpTail[i]=OK;
|
||||
} else {
|
||||
if(dir==DOWN) {
|
||||
jmpTail[i]++;
|
||||
} else {
|
||||
jmpTail[i]--;
|
||||
}
|
||||
}
|
||||
mvaddch(jmpTail[i],i,' ');
|
||||
mvaddch(tail[i],i,' ');
|
||||
mvaddch(cascade[i],i,(rand()%60)+65);
|
||||
}
|
||||
refresh();
|
||||
if(!pause) {
|
||||
timeout(time);
|
||||
} else {
|
||||
timeout(-1);
|
||||
get=getch();
|
||||
switch (get) {
|
||||
case 's':
|
||||
pause=!pause;
|
||||
break;
|
||||
case 'r':
|
||||
pause=FALSE;
|
||||
if(dir==DOWN) {
|
||||
dir=UP;
|
||||
limit=0;
|
||||
startPos=maxY;
|
||||
} else {
|
||||
dir=DOWN;
|
||||
limit=maxY;
|
||||
startPos=0;
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
get='q';
|
||||
break;
|
||||
}
|
||||
timeout(time);
|
||||
}
|
||||
if(get!='q') {
|
||||
get=getch();
|
||||
}
|
||||
}
|
||||
endwin();
|
||||
return 1;
|
||||
}
|
184
src/main.c
184
src/main.c
@ -1,184 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
#include <curses.h>
|
||||
#include <string.h>
|
||||
|
||||
#define UP (1)
|
||||
#define DOWN (2)
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
int help() {
|
||||
puts("Program Flags:");
|
||||
printf("\t--tickTime [integer]\n\t\tWill change the time waited between frame updates in miliseconds\n\n");
|
||||
printf("\t--streanLen [integer]\n\t\tChanges the vertical length of the stream in characters\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int time = 15;
|
||||
int initTailLen = 20;
|
||||
for(int i=1; i<argc; i++) {
|
||||
if(strcmp(argv[i],"--help") == 0 || strcmp(argv[i], "-h") == 0) {
|
||||
endwin();
|
||||
help();
|
||||
return 1;
|
||||
} else {
|
||||
if(strcmp(argv[i],"--tickTime") == 0) {
|
||||
time=atoi(argv[i+1]);
|
||||
i++;
|
||||
} else {
|
||||
if(strcmp(argv[i],"--streamLen") == 0) {
|
||||
initTailLen=atoi(argv[i+1]);
|
||||
i++;
|
||||
} else {
|
||||
endwin();
|
||||
printf("[ERROR] Invalid flag\n\n");
|
||||
help();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WINDOW term = *initscr();
|
||||
start_color();
|
||||
init_pair(1,COLOR_GREEN,COLOR_BLACK);
|
||||
attron(COLOR_PAIR(1));
|
||||
noecho();
|
||||
if(nodelay(&term,TRUE)==ERR) {
|
||||
puts("[ERROR] could not enter no-delay mode");
|
||||
return ERR;
|
||||
}
|
||||
timeout(time);
|
||||
|
||||
int maxX = COLS;
|
||||
int maxY = LINES;
|
||||
|
||||
int tailLen[maxX];
|
||||
|
||||
for(int i=0; i<maxX; i++) {
|
||||
int num=rand()%2;
|
||||
if(num>1) {
|
||||
tailLen[i]=initTailLen+(rand()%(4%initTailLen));
|
||||
} else {
|
||||
if(num<=1) {
|
||||
tailLen[i]=initTailLen-(rand()%initTailLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
int cascade[maxX];
|
||||
int tail[maxX];
|
||||
int jmpTail[maxX];
|
||||
|
||||
for(int i=0; i<maxX; i++)
|
||||
cascade[i]=ERR;
|
||||
|
||||
int pause = 0;
|
||||
int dir = DOWN;
|
||||
int limit = maxY;
|
||||
int startPos = 0;
|
||||
int get = getch();
|
||||
while(get!='q') {
|
||||
if(get=='s') {
|
||||
pause=TRUE;
|
||||
}
|
||||
int xPos = rand()%maxX;//get random x position for the stream
|
||||
if(cascade[xPos]==ERR) {//make sure the stream is set to active
|
||||
cascade[xPos]=startPos;
|
||||
}
|
||||
for(int i=0; i<maxX; i++) {//move the streams up/down
|
||||
if(!pause) {
|
||||
if(cascade[i]!=ERR) {
|
||||
if(dir==DOWN) {
|
||||
tail[i] = cascade[i]-tailLen[i];
|
||||
} else {
|
||||
tail[i] = cascade[i]+tailLen[i];
|
||||
}
|
||||
if(cascade[i]==limit) {//check if the position is at the bottom of the screen
|
||||
cascade[i]=ERR;//set the position to ERR if it is at the bottom
|
||||
} else {
|
||||
if(rand()%10==1) {//check if the stream will jump
|
||||
if(!(jmpTail[i]<=maxY)) {
|
||||
if(dir==DOWN) {
|
||||
cascade[i]=cascade[i]+rand()%8;
|
||||
jmpTail[i]=cascade[i]-tailLen[i];
|
||||
} else {
|
||||
cascade[i]=cascade[i]-rand()%8;
|
||||
jmpTail[i]=cascade[i]+tailLen[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(dir==DOWN) {
|
||||
cascade[i]++;//increase the position
|
||||
} else {
|
||||
cascade[i]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(tail[i]!=limit) {//keep iterating the tail intill it is at the limit
|
||||
if(dir==DOWN) {
|
||||
tail[i]++;
|
||||
} else {
|
||||
tail[i]--;
|
||||
}
|
||||
}
|
||||
if(jmpTail[i]==maxY || jmpTail[i]==0) {
|
||||
jmpTail[i]=OK;
|
||||
} else {
|
||||
if(dir==DOWN) {
|
||||
jmpTail[i]++;
|
||||
} else {
|
||||
jmpTail[i]--;
|
||||
}
|
||||
}
|
||||
mvaddch(jmpTail[i],i,' ');
|
||||
mvaddch(tail[i],i,' ');
|
||||
mvaddch(cascade[i],i,(rand()%60)+65);
|
||||
}
|
||||
refresh();
|
||||
if(!pause) {
|
||||
timeout(time);
|
||||
} else {
|
||||
timeout(-1);
|
||||
get=getch();
|
||||
switch (get) {
|
||||
case 's':
|
||||
pause=!pause;
|
||||
break;
|
||||
case 'r':
|
||||
pause=FALSE;
|
||||
if(dir==DOWN) {
|
||||
dir=UP;
|
||||
limit=0;
|
||||
startPos=maxY;
|
||||
} else {
|
||||
dir=DOWN;
|
||||
limit=maxY;
|
||||
startPos=0;
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
get='q';
|
||||
break;
|
||||
}
|
||||
timeout(time);
|
||||
}
|
||||
if(get!='q') {
|
||||
get=getch();
|
||||
}
|
||||
}
|
||||
endwin();
|
||||
return 1;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user