This commit is contained in:
Roman Khaliavka 2025-05-01 16:00:32 +00:00
parent 0ae98e192a
commit 03cb0c7ea2

70
a4/world.c Normal file
View File

@ -0,0 +1,70 @@
#include "world.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
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;
}