Commits

Anonymous committed 7797c41

added r for frame rate

Comments (0)

Files changed (2)

 .OP \-i infile
 .OP \-o outfile
 .OP \-p
+.OP \-r
 .OP \-s size
 .YS
 
 .B \-p
 start paused instead of running
 .TP
+.B \-r
+show frame rate at top right
+.TP
 .BI \-s " size"
 set the size of the board (defaults to 32) (HINT: can be as large as malloc will give you)
 
 .I c
 toggle coordinate display
 .TP
+.I r
+toggle frame rate display
+.TP
 .I g
 toggle generation display
 .TP
 int i_org, j_org, cols, rows, gen, fd;
 struct termios set, dfl;
 char *outfile = NULL, *infile = "/dev/urandom";
-int fps = 15, size = 32, show_gen = 0, show_coord = 0, paused = 0;
+int fps = 15, size = 32, paused = 0;
+int show_coord = 0, show_fps = 0, show_gen = 0;
 
 void sigwinch(int unused)
 {
 	fflush(stdout);
 }
 
+void print_gen()
+{
+	if (!show_gen)
+		return;
+	cursor_crd(MIN(rows, size), 1);
+	printf("Generation: %d", gen);
+	fflush(stdout);
+}
+
+void print_fps()
+{
+	if (!show_fps)
+		return;
+	cursor_crd(0, MIN(cols, size) * 2 - strlen("fps: xxx"));
+	printf("fps: %03d", fps);
+	fflush(stdout);
+}
+
 void quit(int sig)
 {
 	tcsetattr(0, TCSANOW, &dfl);
 		if (i < size - 1  && i < i_org + rows - 1)
 			fputc('\n', stdout);
 	}
-	if (show_gen) {
-		cursor_beg();
-		printf("Generation: %d", gen);
-		fflush(stdout);
-	}
+	print_gen();
 	print_coord(i_org, j_org);
+	print_fps();
 }
 
 void stop(void)
 {
 	char c;
-	int i, j, print_flag = 0;
+	int print_flag = 0;
+	static int i = -1, j = -1;
 
-	i = i_org ? i_org + rows / 2 : size / 2;
-	j = j_org ? j_org + rows / 2 : size / 2;
+	if (i < i_org || i > i_org + rows)
+		i = i_org ? i_org + rows / 2 : size / 2;
+	if (j < j_org || j > j_org + cols)
+		j = j_org ? j_org + cols / 2 : size / 2;
 
 	set.c_cc[VMIN] = 1;
 	tcsetattr(0, TCSANOW, &set);
 				show_coord = !show_coord;
 				print_flag = 1;
 				break;
+			case 'r' :
+				show_fps = !show_fps;
+				print_flag = 1;
+				break;
 			case 'g' :
 				show_gen = !show_gen;
 				print_flag = 1;
 				return;
 			case 'q' :
 				quit(0);
+			case '+' :
+				fps++;
+				print_flag = 1;
+				break;
+			case '-' :
+				fps--;
+				print_flag = 1;
+				break;
 		}
 		if (print_flag) {
 			print_flag = 0;
 		usleep(1000000 / fps);
 		while (!paused && read(0, &c, sizeof(c)) > 0) {
 			switch (c) {
-				case 'c' : show_coord = !show_coord;               break;
-				case 'g' : show_gen   = !show_gen;                 break;
+				case 'c' : show_coord = !show_coord; break;
+				case 'r' : show_fps   = !show_fps;   break;
+				case 'g' : show_gen   = !show_gen;   break;
 				case 'j' : i_org += (i_org + rows < size) ? 1 : 0; break;
 				case 'k' : i_org -= (i_org > 0)           ? 1 : 0; break;
 				case 'h' : j_org -= (j_org > 0)           ? 1 : 0; break;
 				case 'l' : j_org += (j_org + cols < size) ? 1 : 0; break;
 				case 'p' : paused = 1; break;
+				case '+' : fps++;      break;
+				case '-' : fps--;      break;
 				case 'q' : quit(0);
 			}
 		}
 			case 'i' : infile = argv[++i];     break; // default "/dev/urandom"
 			case 'o' : outfile = argv[++i];    break; // default NULL
 			case 'p' : paused = 1;             break; // default 0
+			case 'r' : show_fps = 1;           break; // default 0
 			case 's' : size = atoi(argv[++i]); break; // default 32
 			default  : return 1;
 		}