Added extra display options

This commit is contained in:
Kevin Lange 2012-03-25 19:32:39 -05:00
parent ed21034c7b
commit 415f8251d9
2 changed files with 124 additions and 38 deletions

View File

@ -1,3 +1,9 @@
/*
* Pop Tart Cat animation frames
*/
#ifndef ANIMATION_H
#define ANIMATION_H
char * frame0[] = { char * frame0[] = {
",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,,,,,,,,,,,,,,,,,,,,,,,,,", ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,,,,,,,,,,,,,,,,,,,,,,,,,",
",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,.,,,,,,,,,,,,,,,,,,,,,,,,", ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,.,,,,,,,,,,,,,,,,,,,,,,,,",
@ -805,3 +811,8 @@ char ** frames[] = {
frame11, frame11,
NULL NULL
}; };
#define FRAME_WIDTH 64
#define FRAME_HEIGHT 64
#endif

View File

@ -100,6 +100,17 @@ char * output = " ";
*/ */
int telnet = 0; int telnet = 0;
/*
* Whether or not to show the counter
*/
int show_counter = 1;
/*
* Number of frames to show before quitting
* or 0 to repeat forever (default)
*/
int frame_count = 0;
/* /*
* Environment to use for setjmp/longjmp * Environment to use for setjmp/longjmp
* when breaking out of options handler * when breaking out of options handler
@ -125,18 +136,26 @@ int digits(int val) {
* These values crop the animation, as we have a full 64x64 stored, * These values crop the animation, as we have a full 64x64 stored,
* but we only want to display 40x24 (double width). * but we only want to display 40x24 (double width).
*/ */
#define MIN_ROW 20 int min_row = 20;
#define MAX_ROW 43 int max_row = 43;
#define MIN_COL 10 int min_col = 10;
#define MAX_COL 50 int max_col = 50;
/*
* Print escape sequences to return cursor to visible mode
* and exit the application.
*/
void finish() {
printf("\033[?25h\033[0m\033[H\033[2J");
exit(0);
}
/* /*
* In the standalone mode, we want to handle an interrupt signal * In the standalone mode, we want to handle an interrupt signal
* (^C) so that we can restore the cursor and clear the terminal. * (^C) so that we can restore the cursor and clear the terminal.
*/ */
void SIGINT_handler(int sig){ void SIGINT_handler(int sig){
printf("\033[?25h\033[0m\033[H\033[2J"); finish();
exit(0);
} }
/* /*
@ -244,11 +263,19 @@ void usage(char * argv[]) {
printf( printf(
"Terminal Nyancat\n" "Terminal Nyancat\n"
"\n" "\n"
"usage: %s [-h] [-t] [-i] \n" "usage: %s [-hitn] [-f \033[3mframes\033[0m]\n"
"\n" "\n"
" -i --intro \033[3mShow the introduction / about informaiton at startup.\033[0m\n" " -i --intro \033[3mShow the introduction / about informaiton at startup.\033[0m\n"
" -t --telnet \033[3mTelnet mode.\033[0m\n", " -t --telnet \033[3mTelnet mode.\033[0m\n"
" -h --help \033[3mShow this help message.\033[0m\n", " -n --no-counter \033[3mDo not display the timer\033[0m\n"
" -f --frames \033[3mDisplay the requested number of frames, then quit\033[0m\n"
" -r --min-rows \033[3mCrop the animation from the top\033[0m\n"
" -R --max-rows \033[3mCrop the animation from the bottom\033[0m\n"
" -c --min-cols \033[3mCrop the animation from the left\033[0m\n"
" -C --max-cols \033[3mCrop the animation from the right\033[0m\n"
" -W --width \033[3mCrop the animation to the given width\033[0m\n"
" -H --height \033[3mCrop the animation to the given height\033[0m\n"
" -h --help \033[3mShow this help message.\033[0m\n",
argv[0]); argv[0]);
} }
@ -268,15 +295,23 @@ int main(int argc, char ** argv) {
/* Long option names */ /* Long option names */
static struct option long_opts[] = { static struct option long_opts[] = {
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"telnet", no_argument, 0, 't'}, {"telnet", no_argument, 0, 't'},
{"intro", no_argument, 0, 'i'}, {"intro", no_argument, 0, 'i'},
{"no-counter", no_argument, 0, 'n'},
{"frames", required_argument, 0, 'f'},
{"min-rows", required_argument, 0, 'r'},
{"max-rows", required_argument, 0, 'R'},
{"min-cols", required_argument, 0, 'c'},
{"max-cols", required_argument, 0, 'C'},
{"width", required_argument, 0, 'W'},
{"height", required_argument, 0, 'H'},
{0,0,0,0} {0,0,0,0}
}; };
/* Process arguments */ /* Process arguments */
int index, c; int index, c;
while ((c = getopt_long(argc, argv, "hit", long_opts, &index)) != -1) { while ((c = getopt_long(argc, argv, "hitnf:r:R:c:C:W:H:", long_opts, &index)) != -1) {
if (!c) { if (!c) {
if (long_opts[index].flag == 0) { if (long_opts[index].flag == 0) {
c = long_opts[index].val; c = long_opts[index].val;
@ -293,6 +328,32 @@ int main(int argc, char ** argv) {
usage(argv); usage(argv);
exit(0); exit(0);
break; break;
case 'n':
show_counter = 0;
break;
case 'f':
frame_count = atoi(optarg);
break;
case 'r':
min_row = atoi(optarg);
break;
case 'R':
max_row = atoi(optarg);
break;
case 'c':
min_col = atoi(optarg);
break;
case 'C':
max_col = atoi(optarg);
break;
case 'W':
min_col = (FRAME_WIDTH - atoi(optarg)) / 2;
max_col = (FRAME_WIDTH + atoi(optarg)) / 2;
break;
case 'H':
min_row = (FRAME_HEIGHT - atoi(optarg)) / 2;
max_row = (FRAME_HEIGHT + atoi(optarg)) / 2;
break;
default: default:
break; break;
} }
@ -640,14 +701,15 @@ int main(int argc, char ** argv) {
time_t start, current; time_t start, current;
time(&start); time(&start);
int playing = 1; /* Animation should continue [left here for modifications] */ int playing = 1; /* Animation should continue [left here for modifications] */
size_t i = 0; /* Current frame # */ size_t i = 0; /* Current frame # */
char last = 0; /* Last color index rendered */ unsigned int f = 0; /* Total frames passed */
size_t y, x; /* x/y coordinates of what we're drawing */ char last = 0; /* Last color index rendered */
size_t y, x; /* x/y coordinates of what we're drawing */
while (playing) { while (playing) {
/* Render the frame */ /* Render the frame */
for (y = MIN_ROW; y < MAX_ROW; ++y) { for (y = min_row; y < max_row; ++y) {
for (x = MIN_COL; x < MAX_COL; ++x) { for (x = min_col; x < max_col; ++x) {
if (always_escape) { if (always_escape) {
/* Text mode (or "Always Send Color Escapse") */ /* Text mode (or "Always Send Color Escapse") */
printf("%s", colors[frames[i][y][x]]); printf("%s", colors[frames[i][y][x]]);
@ -665,28 +727,41 @@ int main(int argc, char ** argv) {
/* End of row, send newline */ /* End of row, send newline */
newline(1); newline(1);
} }
/* Get the current time for the "You have nyaned..." string */ if (show_counter) {
time(&current); /* Get the current time for the "You have nyaned..." string */
double diff = difftime(current, start); time(&current);
/* Now count the length of the time difference so we can center */ double diff = difftime(current, start);
int nLen = digits((int)diff); /* Now count the length of the time difference so we can center */
int width = (terminal_width - 29 - nLen) / 2; int nLen = digits((int)diff);
/* Spit out some spaces so that we're actually centered */ int anim_width = terminal_width == 80 ? (max_col - min_col) * 2 : (max_col - min_col);
while (width > 0) { /*
printf(" "); * 29 = the length of the rest of the string;
width--; * XXX: Replace this was actually checking the written bytes from a
* call to sprintf or something
*/
int width = (anim_width - 29 - nLen) / 2;
/* Spit out some spaces so that we're actually centered */
while (width > 0) {
printf(" ");
width--;
}
/* You have nyaned for [n] seconds!
* The \033[J ensures that the rest of the line has the dark blue
* background, and the \033[1;37m ensures that our text is bright white.
* The \033[0m prevents the Apple ][ from flipping everything, but
* makes the whole nyancat less bright on the vt220
*/
printf("\033[1;37mYou have nyaned for %0.0f seconds!\033[J\033[0m", diff);
} }
/* You have nyaned for [n] seconds!
* The \033[J ensures that the rest of the line has the dark blue
* background, and the \033[1;37m ensures that our text is bright white.
* The \033[0m prevents the Apple ][ from flipping everything, but
* makes the whole nyancat less bright on the vt220
*/
printf("\033[1;37mYou have nyaned for %0.0f seconds!\033[J\033[0m", diff);
/* Reset the last color so that the escape sequences rewrite */ /* Reset the last color so that the escape sequences rewrite */
last = 0; last = 0;
/* Update frame crount */ /* Update frame crount */
++f;
if (frame_count != 0 && f == frame_count) {
finish();
} else {
printf("f_c = %d,%d\n", f, frame_count);
}
++i; ++i;
if (!frames[i]) { if (!frames[i]) {
/* Loop animation */ /* Loop animation */