Reintroduced color support for ncurses version

This commit is contained in:
Tiehuis 2015-02-22 16:30:40 +13:00
parent d8cef1def4
commit 5d6c3f92be
4 changed files with 31 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/2048* 2048*
*.o
*.core *.core

View File

@ -51,7 +51,7 @@ Set the rate at which blocks are spawned. Default is 1.
Set the target end condition. Default is 2048. Set the target end condition. Default is 2048.
.SH AUTHORS .SH AUTHORS
Originally written by Tiehuis. Originally written by Marc Tiehuis.
All contributions can be found at \fIhttps://github.com/Tiehuis/2048-cli\fR. All contributions can be found at \fIhttps://github.com/Tiehuis/2048-cli\fR.
.SH COPYRIGHT .SH COPYRIGHT

View File

@ -8,6 +8,8 @@
#define GFX_EXTRA_RIGHT case KEY_RIGHT: #define GFX_EXTRA_RIGHT case KEY_RIGHT:
#define GFX_EXTRA_LEFT case KEY_LEFT: #define GFX_EXTRA_LEFT case KEY_LEFT:
#define NUMBER_OF_COLORS 7
#define iterate(n, expression)\ #define iterate(n, expression)\
do {\ do {\
int i;\ 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_width = g->opts->grid_width * (g->print_width + 2) + 1;
s->window = newwin(s->window_height, s->window_width, 1, 1); s->window = newwin(s->window_height, s->window_width, 1, 1);
keypad(s->window, TRUE); 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; 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) void gfx_draw(struct gfx_state *s, struct gamestate *g)
{ {
if (g->score_last) 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) { for (x = 0; x < g->opts->grid_width; ++x) {
if (g->grid[x][y]) { 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]); 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, " |"); mvwprintw(s->window, ypos, xpos + g->print_width, " |");
} }
else { else {

View File

@ -47,6 +47,7 @@ get_new_key:;
/* Game will only end if 0 moves available */ /* Game will only end if 0 moves available */
if (game_running) { 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)) if (gamestate_tick(s, g, direction, g->opts->animate ? draw_then_sleep : NULL))
gamestate_new_block(g); gamestate_new_block(g);