diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-03-30 14:34:32 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-03-30 14:34:32 +0300 |
commit | 6f4ac9571a0a6df9ec607e5a9a9b8d3e5f7bbc96 (patch) | |
tree | 482a50f3f67eb8197cf52af1953f5a7a3cf87024 | |
parent | 3247a2c20fde12dd9a58e04544006c988d231e7d (diff) | |
download | carcassonne-engine-c-6f4ac9571a0a6df9ec607e5a9a9b8d3e5f7bbc96.tar.gz |
feat: add NCurses GUI
-rwxr-xr-x | Makefile | 2 | ||||
-rw-r--r-- | src/main.c | 80 |
2 files changed, 74 insertions, 8 deletions
@@ -1,5 +1,5 @@ CC=tcc -CFLAGS=-g +CFLAGS=-g -O2 -lncurses SRC=src OBJ=obj SOURCES=$(wildcard $(SRC)/*.c) @@ -1,22 +1,88 @@ #include <stdio.h> +#include <curses.h> #include "board.h" +void draw_board(char* board, WINDOW* win) { + wmove(win, 0, 0); + for (int row = 0; row < BOARD_ROW_BYTES; row++) { + for (int i = 0; i < BOARD_ROW_BYTES; i++) { + int index = BOARD_ROW_BYTES * row + i; + if (board[index] == EMPTY && is_center_index(index)) waddch(win, '*'); + else waddch(win, board[index]); + } + } +} + int main() { + /* initialize curses */ + initscr(); + cbreak(); + + /* create board window */ + WINDOW* board_box = newwin(BOARD_ROW_BYTES + 2, BOARD_ROW_BYTES + 2, 0, 0); + WINDOW* board_win = derwin(board_box, BOARD_ROW_BYTES, BOARD_ROW_BYTES, 1, 1); + box(board_box, 0, 0); + wrefresh(board_box); + + /* create structures window */ + WINDOW* structures_box = newwin(BOARD_ROW_BYTES + 2, BOARD_ROW_BYTES + 2, 0, BOARD_ROW_BYTES + 3); + WINDOW* structures_win = derwin(structures_box, BOARD_ROW_BYTES, BOARD_ROW_BYTES, 1, 1); + box(structures_box, 0, 0); + wrefresh(structures_box); + + /* create input window */ + WINDOW* input_box = newwin(3, 22, BOARD_ROW_BYTES + 3, 0); + WINDOW* input_win = derwin(input_box, 1, 20, 1, 1); + box(input_box, 0, 0); + wrefresh(input_box); + + /* create messages window */ + WINDOW* messages_box = newwin(40, 60, BOARD_ROW_BYTES + 6, 0); + WINDOW* messages_win = derwin(messages_box, 40 - 2, 60 - 2, 1, 1); + box(messages_box, 0, 0); + wrefresh(messages_box); + char board[BOARD_BYTES]; char structures[BOARD_BYTES]; initialize_board(board); place_tile("RFRCR", translate_coordinate(24), board, 1); - write_board(board, "board.txt"); - initialize_board(structures); - create_structure_mask(board, structures); + /* main loop */ + char tile[5]; + char position[3]; + while (1) { + /* prepare */ + initialize_board(structures); + create_structure_mask(board, structures); + wclear(input_win); + + /* draw onto the screen */ + draw_board(board, board_win); + draw_board(structures, structures_win); + wrefresh(board_win); + wrefresh(structures_win); + + /* user input */ + box(input_box, 0, 0); + mvwaddstr(input_box, 0, 0, "Enter tile:"); + wrefresh(input_box); + wgetstr(input_win, tile); + wclear(input_win); + if (tile[0] == 'q') break; + + box(input_box, 0, 0); + mvwaddstr(input_box, 0, 0, "Enter position:"); + wrefresh(input_box); + wgetstr(input_win, position); - print_board(board); - printf("\n\n"); - print_board(structures); + int result = place_tile(tile, translate_coordinate(atoi(position)), board, 0); + if (result) wprintw(messages_win, "Placed tile %s at position %i\n", tile, atoi(position)); + else wprintw(messages_win, "Could not place tile %s at position %i\n", tile, atoi(position)); + wrefresh(messages_win); + } - evaluate_structure(translate_coordinate(24), board, structures); + endwin(); return 0; } |