#include #include #include #include "game.h" #include "input.h" #include "log.h" Game *game_create(void) { Game *g = malloc(sizeof(Game)); g->should_close = false; g->rooms.count = 0; Log *log = create_log(); g->log = log; Vector2 input_position = { 190, 200 }; Input *input = create_input(input_position); input->log = log; input->g = g; input->command = '>'; // Don't change this g->input = input; return g; } void free_game(Game *g) { free_rooms(g->rooms); free_input(g->input); free_log(g->log); free(g); } void game_handle_command(Game *g, const char *command) { Input *input = g->input; if (strcmp(input->input_buffer, "QUIT") == 0) { g->should_close = true; } else if (strcmp(input->input_buffer, "LOOK") == 0) { push_line_to_log(input->log, g->current_room->description); } } #define ROOMS_PATH "./rooms.txt" #define MAX_ROOM_COUNT 100 char room_buffer[2001]; void game_load_rooms(Game *g) { FILE *rooms_file = fopen(ROOMS_PATH, "r"); while ((fgets(room_buffer, 2000, rooms_file)) != NULL) { char *token = strtok(room_buffer, "|"); g->rooms.rooms[g->rooms.count].name = malloc(strlen(token) + 1); strcpy(g->rooms.rooms[g->rooms.count].name, token); token = strtok(NULL, "|"); g->rooms.rooms[g->rooms.count].description = malloc(strlen(token) + 1); strcpy(g->rooms.rooms[g->rooms.count].description, token); g->rooms.count++; } fclose(rooms_file); g->current_room = &g->rooms.rooms[0]; } void game_handle_input(Game *g) { handle_pressed_keys(g->input); } void game_draw(Game *g) { BeginDrawing(); ClearBackground(BLACK); draw_log(g->log); draw_text(g->input); EndDrawing(); } void game_run_until_close(Game *g) { while (!WindowShouldClose() && !g->should_close) { game_handle_input(g); game_draw(g); } }