#include #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); } bool string_in(const char *input, ...) { va_list argp; va_start(argp, input); char *candidate; while ((candidate = va_arg(argp, char*))) { if (strcmp(input, candidate) == 0) { va_end(argp); return true; } } return false; } Command command_from_string(const char *string) { if (string_in(string, "QUIT", "Q", "EXIT", "CLOSE", NULL)) { return COMMAND_QUIT; } else if (string_in(string, "LOOK", "L", NULL)) { return COMMAND_LOOK; } return COMMAND_UNKNOWN; } void game_handle_command(Game *g, const char *command) { Input *input = g->input; switch (command_from_string(command)) { case COMMAND_QUIT: g->should_close = true; break; case COMMAND_LOOK: push_line_to_log(input->log, g->current_room->description); break; default: break; } } #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); } }