Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Autodetect color support, allow override through arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Sep 12, 2014
1 parent 37c1d36 commit a49ed8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/2048.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,15 @@ int main(int argc, char **argv)

/* parse options */
int c;
while ((c = getopt(argc, argv, "rchs:b:")) != -1) {
int enable_color = has_colors();
while ((c = getopt(argc, argv, "rcChs:b:")) != -1) {
switch (c) {
/* Color support */
case 'c':
if (has_colors()) {
start_color();
init_pair(0, 1, 0);
init_pair(1, 2, 0);
init_pair(2, 3, 0);
init_pair(3, 4, 0);
init_pair(4, 5, 0);
init_pair(5, 6, 0);
init_pair(6, 7, 0);
}
else {
fprintf(stderr, "Terminal does not support color\n");
}
enable_color = 1;
break;
case 'C':
enable_color = 0;
break;
// different board sizes
case 's':;
Expand Down Expand Up @@ -275,7 +267,22 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS);
}
}


if (enable_color) {
if (!has_colors()) {
fprintf(stderr, "Terminal does not support color\n");
} else {
start_color();
init_pair(0, 1, 0);
init_pair(1, 2, 0);
init_pair(2, 3, 0);
init_pair(3, 4, 0);
init_pair(4, 5, 0);
init_pair(5, 6, 0);
init_pair(6, 7, 0);
}
}

/* Allocate memory once we actually know amount */
CALLOC2D(grid, grid_size);

Expand Down
3 changes: 2 additions & 1 deletion src/2048.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef enum {
"Options:\n"\
" -s <size> Set the grid border length\n"\
" -b <rate> Set the block spawn rate\n"\
" -c Enables color support (ncurses version only)\n"
" -c Enables color support (ncurses version only)\n"\
" -C Disabled color support (ncurses version only)\n"

#endif

0 comments on commit a49ed8d

Please sign in to comment.