From 28d65efb66a2fb068ed9d5be082fa5167007d731 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 11 Apr 2022 11:40:01 +0300 Subject: refactor: move GUI to separate files --- src/gui.c | 34 ++++++++++++++++++++++++++++++++++ src/gui.h | 7 +++++++ src/main.c | 42 ++++-------------------------------------- 3 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 src/gui.c create mode 100644 src/gui.h diff --git a/src/gui.c b/src/gui.c new file mode 100644 index 0000000..29ad554 --- /dev/null +++ b/src/gui.c @@ -0,0 +1,34 @@ +#include "gui.h" +#include "meeple.h" + +void initialize_colors() { + use_default_colors(); + start_color(); + for (int i = 1; i < 6; i++) init_pair(i, COLOR_BLACK + i, COLOR_BLACK); +} + +int get_player_color(int player) { + return player ? COLOR_PAIR(player + 2) : COLOR_PAIR(0); +} + +void draw_board(BoardUnit* board, int* meeple_map, WINDOW* win) { + wmove(win, 0, 0); + for (int i = 0; i < BOARD_UNITS; i++) { + if (board[i].feature == EMPTY && is_center_index(i)) waddch(win, '.'); + else waddch( + win, + board[i].feature + | get_player_color(get_structure_dominator(board[i].structure_group, meeple_map)) + ); + } + wrefresh(win); +} + +WINDOW* create_framed_window(char* name, int lines, int cols, int x, int y) { + WINDOW* frame = newwin(lines, cols, x, y); + WINDOW* window = derwin(frame, lines - 2, cols - 2, 1, 1); + box(frame, 0, 0); + mvwaddstr(frame, 0, 1, name); + wrefresh(frame); + return window; +} diff --git a/src/gui.h b/src/gui.h new file mode 100644 index 0000000..dc9afd8 --- /dev/null +++ b/src/gui.h @@ -0,0 +1,7 @@ +#include "curses.h" +#include "board.h" + +int get_player_color(int player); +void draw_board(BoardUnit* board, int* meeple_map, WINDOW* win); +void initialize_colors(); +WINDOW* create_framed_window(char* name, int lines, int cols, int x, int y); diff --git a/src/main.c b/src/main.c index 34524a6..f9d35b8 100644 --- a/src/main.c +++ b/src/main.c @@ -3,52 +3,18 @@ #include #include #include "carcassonne.h" +#include "gui.h" -int get_player_color(int player) { - return player ? COLOR_PAIR(player + 2) : COLOR_PAIR(0); -} - -void draw_board(BoardUnit* board, int* meeple_map, WINDOW* win) { - wmove(win, 0, 0); - for (int row = 0; row < BOARD_ROW_UNITS; row++) { - for (int i = 0; i < BOARD_ROW_UNITS; i++) { - int index = BOARD_ROW_UNITS * row + i; - if (board[index].feature == EMPTY && is_center_index(index)) waddch(win, '.'); - else waddch( - win, - board[index].feature - | get_player_color(get_structure_dominator(board[index].structure_group, meeple_map)) - ); - } - } - wrefresh(win); -} int main() { /* initialize curses */ initscr(); cbreak(); - use_default_colors(); srand(time(NULL)); + initialize_colors(); - /* colors */ - start_color(); - for (int i = 1; i < 6; i++) init_pair(i, COLOR_BLACK + i, COLOR_BLACK); - - /* create board window */ - WINDOW* board_box = newwin(BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, 0); - WINDOW* board_win = derwin(board_box, BOARD_ROW_UNITS, BOARD_ROW_UNITS, 1, 1); - box(board_box, 0, 0); - mvwaddstr(board_box, 0, 1, "Board"); - wrefresh(board_box); - - /* create messages window */ - WINDOW* messages_box = newwin(BOARD_ROW_UNITS + 2, 60, 0, BOARD_ROW_UNITS + 3); - WINDOW* messages_win = derwin(messages_box, BOARD_ROW_UNITS, 60 - 2, 1, 1); - box(messages_box, 0, 0); - mvwaddstr(messages_box, 0, 1, "Log"); - wrefresh(messages_box); - + WINDOW* board_win = create_framed_window("Board", BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, 0); + WINDOW* messages_win = create_framed_window("Log", BOARD_ROW_UNITS + 2, 80, 0, BOARD_ROW_UNITS + 3); int meeple_map[MAX_STRUCTURES * PLAYERS]; BoardUnit board[BOARD_UNITS]; -- cgit v1.2.3