From 90a6b134231db335ae5868d04078f23ed6844ce4 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 4 Mar 2019 17:41:42 +0000 Subject: add color support --- cetris.c | 38 ++++++++++++++++++++++++++++++-------- cetris.h | 13 +++++++++++++ ncurses_ui.c | 14 ++++++++++---- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/cetris.c b/cetris.c index af7c78b..fb3379d 100644 --- a/cetris.c +++ b/cetris.c @@ -203,7 +203,7 @@ void rotate_matrix(struct cetris_game* g, int clockwise) { wall_kick = (next == RRIGHT) ? 3 : 4; break; default: // check for UB causing invalid rotations - assert(false); + assert(0); } int set_current = 0; @@ -322,13 +322,34 @@ void clear_move_queue(struct cetris_game* g) { void init_piece_queue(struct cetris_game* g) { for (int i = 0; i < 7; i++) { switch (i) { - case 0: g->piece_queue[i].t = O; break; - case 1: g->piece_queue[i].t = I; break; - case 2: g->piece_queue[i].t = S; break; - case 3: g->piece_queue[i].t = Z; break; - case 4: g->piece_queue[i].t = L; break; - case 5: g->piece_queue[i].t = J; break; - case 6: g->piece_queue[i].t = T; break; + case 0: + g->piece_queue[i].t = O; + g->piece_queue[i].c = COLOR_O; + break; + case 1: + g->piece_queue[i].t = I; + g->piece_queue[i].c = COLOR_I; + break; + case 2: + g->piece_queue[i].t = S; + g->piece_queue[i].c = COLOR_S; + break; + case 3: + g->piece_queue[i].t = Z; + g->piece_queue[i].c = COLOR_Z; + break; + case 4: + g->piece_queue[i].t = L; + g->piece_queue[i].c = COLOR_L; + break; + case 5: + g->piece_queue[i].t = J; + g->piece_queue[i].c = COLOR_J; + break; + case 6: + g->piece_queue[i].t = T; + g->piece_queue[i].c = COLOR_T; + break; } memcpy(g->piece_queue[i].mat, default_matrices[i], sizeof(piece_matrix)); g->piece_queue[i].r = INIT; @@ -442,6 +463,7 @@ void wipe_board(struct cetris_game* g) { r = (vec2){x + g->current.pos.x, y + g->current.pos.y}; if (g->current.mat[y][x]) { g->board[r.x][r.y].occupied = 1; + g->board[r.x][r.y].c = g->current.c; } } } diff --git a/cetris.h b/cetris.h index 2049699..a7dea7d 100644 --- a/cetris.h +++ b/cetris.h @@ -15,6 +15,17 @@ typedef enum { O, I, S, Z, L, J, T } type; +typedef enum { + COLOR_NONE, + COLOR_O, + COLOR_I, + COLOR_S, + COLOR_Z, + COLOR_L, + COLOR_J, + COLOR_T +} color; + typedef enum { INIT, RRIGHT, @@ -27,6 +38,7 @@ typedef int piece_matrix[4][4]; struct tetrimino { type t; rstate r; + color c; piece_matrix mat; vec2 pos; }; @@ -35,6 +47,7 @@ typedef struct { int occupied; int constant; int remove_tick; + color c; } slot; enum movements { diff --git a/ncurses_ui.c b/ncurses_ui.c index 0f5af4e..1ae07b2 100644 --- a/ncurses_ui.c +++ b/ncurses_ui.c @@ -40,10 +40,14 @@ void curses_init() { timeout(1000 / CETRIS_HZ); start_color(); - init_pair(0, COLOR_MAGENTA, COLOR_BLACK); - init_pair(1, COLOR_GREEN, COLOR_BLACK); - init_pair(2, COLOR_BLUE, COLOR_BLACK); - init_pair(3, COLOR_YELLOW, COLOR_BLACK); + init_pair(COLOR_NONE, COLOR_BLACK, COLOR_BLACK); + init_pair(COLOR_O, COLOR_MAGENTA, COLOR_BLACK); + init_pair(COLOR_Z, COLOR_RED, COLOR_BLACK); + init_pair(COLOR_S, COLOR_CYAN, COLOR_BLACK); + init_pair(COLOR_T, COLOR_WHITE, COLOR_BLACK); + init_pair(COLOR_L, COLOR_GREEN, COLOR_BLACK); + init_pair(COLOR_I, COLOR_BLUE, COLOR_BLACK); + init_pair(COLOR_J, COLOR_YELLOW, COLOR_BLACK); clear(); } @@ -52,6 +56,7 @@ void draw_board(struct cetris_game* g) { for (int x = 0; x < BOARD_X; x++) { for (int y = 0; y < BOARD_Y; y++) { if (g->board[x][y].occupied) { + attron(COLOR_PAIR(g->board[x][y].c)); if (g->board[x][y].remove_tick > 0) { if (g->tick % 2 == 0) { mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); @@ -59,6 +64,7 @@ void draw_board(struct cetris_game* g) { } else { mvaddstr(y + 1, x * 2 + X_OFFSET, BLOCK); } + attroff(COLOR_PAIR(g->board[x][y].c)); } } } -- cgit v1.2.3-101-g0448