diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-04-11 11:40:01 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-04-11 11:40:01 +0300 |
commit | 28d65efb66a2fb068ed9d5be082fa5167007d731 (patch) | |
tree | f6c76dc7c7170b73c3d879bba32278be4c5fdf76 | |
parent | a6dc21034a55be0a9c1910787d615b5bca7131c6 (diff) | |
download | carcassonne-engine-c-28d65efb66a2fb068ed9d5be082fa5167007d731.tar.gz |
refactor: move GUI to separate files
-rw-r--r-- | src/gui.c | 34 | ||||
-rw-r--r-- | src/gui.h | 7 | ||||
-rw-r--r-- | src/main.c | 42 |
3 files changed, 45 insertions, 38 deletions
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); @@ -3,52 +3,18 @@ #include <curses.h> #include <time.h> #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]; |