From 5d6c3f92bed1c15c2018594746f9a275c5a8d1b3 Mon Sep 17 00:00:00 2001 From: Tiehuis Date: Sun, 22 Feb 2015 16:30:40 +1300 Subject: [PATCH] Reintroduced color support for ncurses version --- .gitignore | 3 ++- man/2048.1 | 2 +- src/gfx_curses.c | 27 +++++++++++++++++++++++++++ src/main.c | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6d260d6..fd8eb11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -/2048* +2048* +*.o *.core diff --git a/man/2048.1 b/man/2048.1 index db81d97..a763b9c 100644 --- a/man/2048.1 +++ b/man/2048.1 @@ -51,7 +51,7 @@ Set the rate at which blocks are spawned. Default is 1. Set the target end condition. Default is 2048. .SH AUTHORS -Originally written by Tiehuis. +Originally written by Marc Tiehuis. All contributions can be found at \fIhttps://github.com/Tiehuis/2048-cli\fR. .SH COPYRIGHT diff --git a/src/gfx_curses.c b/src/gfx_curses.c index ee6fa8a..d9869ef 100644 --- a/src/gfx_curses.c +++ b/src/gfx_curses.c @@ -8,6 +8,8 @@ #define GFX_EXTRA_RIGHT case KEY_RIGHT: #define GFX_EXTRA_LEFT case KEY_LEFT: +#define NUMBER_OF_COLORS 7 + #define iterate(n, expression)\ do {\ int i;\ @@ -32,9 +34,32 @@ struct gfx_state* gfx_init(struct gamestate *g) s->window_width = g->opts->grid_width * (g->print_width + 2) + 1; s->window = newwin(s->window_height, s->window_width, 1, 1); keypad(s->window, TRUE); + + if (g->opts->enable_color && has_colors()) { + start_color(); + + int x = 0; + init_pair(x++, 1, 0); + init_pair(x++, 2, 0); + init_pair(x++, 3, 0); + init_pair(x++, 4, 0); + init_pair(x++, 5, 0); + init_pair(x++, 6, 0); + init_pair(x++, 7, 0); + char dummy[x == NUMBER_OF_COLORS ? 1 : -1]; + } + return s; } +static int int_log2(int n) +{ + int k = 0; + while (n) + ++k, n /= 2; + return k; +} + void gfx_draw(struct gfx_state *s, struct gamestate *g) { if (g->score_last) @@ -54,7 +79,9 @@ void gfx_draw(struct gfx_state *s, struct gamestate *g) for (x = 0; x < g->opts->grid_width; ++x) { if (g->grid[x][y]) { + wattron(s->window, COLOR_PAIR(int_log2(g->grid[x][y]) % NUMBER_OF_COLORS)); mvwprintw(s->window, ypos, xpos, "%*d", g->print_width, g->grid[x][y]); + wattroff(s->window, COLOR_PAIR(int_log2(g->grid[x][y]) % NUMBER_OF_COLORS)); mvwprintw(s->window, ypos, xpos + g->print_width, " |"); } else { diff --git a/src/main.c b/src/main.c index f683559..a724ed2 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,7 @@ get_new_key:; /* Game will only end if 0 moves available */ if (game_running) { + /* Maybe change this behaviour so if we don't move, we still generate a block */ if (gamestate_tick(s, g, direction, g->opts->animate ? draw_then_sleep : NULL)) gamestate_new_block(g);