From 03cb0c7ea26307dd055b99f25f4e24f1579d1a0e Mon Sep 17 00:00:00 2001 From: Roman Khaliavka Date: Thu, 1 May 2025 16:00:32 +0000 Subject: [PATCH] a4 --- a4/world.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 a4/world.c diff --git a/a4/world.c b/a4/world.c new file mode 100644 index 0000000..732a126 --- /dev/null +++ b/a4/world.c @@ -0,0 +1,70 @@ +#include "world.h" +#include +#include +#include + +static void* game_state = NULL; +static void (*cleanup_game)(void*) = NULL; +static int (*handle_event)(struct event*, void*) = NULL; + +void set_cell(int c, int x, int y){ + mvaddch(y, x, c); +} + +void clear_screen(){ + clear(); +} + +void set_message(const char* msg, int x, int y){ + mvprintw(y, x, "%s", msg); +} + +void set_color_cell(int character, int x, int y, short front_color, short back_color){ + attron(COLOR_PAIR(front_color * COLOR_COUNT + back_color)); + mvaddch(y, x, character); + attroff(COLOR_PAIR(front_color * COLOR_COUNT + back_color)); +} + +int start_world(void* (*init_game)(), int (*world_event)(struct event*, void*), void (*destroy_game)(void*)){ + initscr(); + keypad(stdscr, TRUE); + noecho(); + curs_set(FALSE); + timeout(100); + srand(time(NULL)); + + start_color(); + init_pair(1, COLOR_GREEN, COLOR_BLACK); + init_pair(2, COLOR_YELLOW, COLOR_BLACK); + init_pair(3, COLOR_WHITE, COLOR_BLACK); + for(int i = 0; i < COLOR_COUNT; i++){ + init_pair(i + 1, i, COLOR_BLACK); + } + + game_state = init_game(); + handle_event = world_event; + cleanup_game = destroy_game; + + int ch; + struct event e = {0}; + + while(1){ + ch = getch(); + + e.key = ch; + e.type = EVENT_TIMEOUT; + if (ch == 27) e.type = EVENT_ESC; + else if (ch != ERR) e.type = EVENT_KEY; + + e.width = COLS; + e.height = LINES; + + clear(); + if (handle_event(&e, game_state)) break; + refresh(); + } + + if (cleanup_game) cleanup_game(game_state); + endwin(); + return 0; +} \ No newline at end of file