diff --git a/.gitignore b/.gitignore index 8ed3858..a11aa7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ raylib.h -game \ No newline at end of file +game + +data/*.c \ No newline at end of file diff --git a/01_text_adventure/Makefile b/01_text_adventure/Makefile index 4bcb07f..59a60bc 100644 --- a/01_text_adventure/Makefile +++ b/01_text_adventure/Makefile @@ -3,9 +3,14 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 .PHONY: clean run -game: *.c +game: rooms.data transitions.data *.c $(CC) *.c $(CFLAGS) -o game +%.data: data/%.txt + echo -n "char *data_$*_txt = \"" > data/$*.c + cat data/$*.txt | perl -pe 's/\n/\\n/g' >> data/$*.c + echo "\";" >> data/$*.c + run: game ./game diff --git a/01_text_adventure/rooms.txt b/01_text_adventure/data/rooms.txt similarity index 100% rename from 01_text_adventure/rooms.txt rename to 01_text_adventure/data/rooms.txt diff --git a/01_text_adventure/transitions.txt b/01_text_adventure/data/transitions.txt similarity index 100% rename from 01_text_adventure/transitions.txt rename to 01_text_adventure/data/transitions.txt diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index 88f0a73..e85e48a 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -6,6 +6,7 @@ #include "transition.h" #include "input.h" #include "log.h" +#include "util.h" Game *game_create(void) { Game *g = malloc(sizeof(Game)); @@ -118,31 +119,6 @@ void game_handle_command(Game *g, const char *command) { } } -#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); } diff --git a/01_text_adventure/main.c b/01_text_adventure/main.c index 618899d..55560d5 100644 --- a/01_text_adventure/main.c +++ b/01_text_adventure/main.c @@ -15,6 +15,7 @@ int main(void) { Game *g = game_create(); game_load_rooms(g); + g->current_room = &g->rooms->rooms[0]; game_load_transitions(g); game_run_until_close(g); diff --git a/01_text_adventure/room.c b/01_text_adventure/room.c index 0ec57b1..eccb5bc 100644 --- a/01_text_adventure/room.c +++ b/01_text_adventure/room.c @@ -2,6 +2,26 @@ #include #include #include "room.h" +#include "game.h" +#include "util.h" + +void load_room(Game *g, char *line) { + char *token = strtok(line, "|"); + + 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++; +} + +#include "data/rooms.c" +void game_load_rooms(Game *g) { + parse_multiline_string(g, data_rooms_txt, &load_room); +} void free_room(Room r) { free(r.name); diff --git a/01_text_adventure/transition.c b/01_text_adventure/transition.c index 9656666..7350d7a 100644 --- a/01_text_adventure/transition.c +++ b/01_text_adventure/transition.c @@ -3,32 +3,28 @@ #include #include "transition.h" #include "game.h" +#include "util.h" -#define TRANSITIONS_PATH "./transitions.txt" +void load_transition(Game *g, char *line) { + char *token = strtok(line, "|"); + g->transitions->transitions[g->transitions->count].from = find_room(g->rooms, token); -char transition_buffer[2001]; -void game_load_transitions(Game *g) { - FILE *transitions_file = fopen(TRANSITIONS_PATH, "r"); + token = strtok(NULL, "|"); + g->transitions->transitions[g->transitions->count].via = command_from_string(token); - while ((fgets(transition_buffer, 2000, transitions_file)) != NULL) { - char *token = strtok(transition_buffer, "|"); + token = strtok(NULL, "|"); + g->transitions->transitions[g->transitions->count].to = find_room(g->rooms, token); - g->transitions->transitions[g->transitions->count].from = find_room(g->rooms, token); - - token = strtok(NULL, "|"); - g->transitions->transitions[g->transitions->count].via = command_from_string(token); - - token = strtok(NULL, "|"); - g->transitions->transitions[g->transitions->count].to = find_room(g->rooms, token); - - token = strtok(NULL, "|"); - g->transitions->transitions[g->transitions->count].description = malloc(strlen(token) + 1); - strcpy(g->transitions->transitions[g->transitions->count].description, token); + token = strtok(NULL, "\n"); + g->transitions->transitions[g->transitions->count].description = malloc(strlen(token) + 1); + strcpy(g->transitions->transitions[g->transitions->count].description, token); - g->transitions->count++; - } + g->transitions->count++; +} - fclose(transitions_file); +#include "data/transitions.c" +void game_load_transitions(Game *g) { + parse_multiline_string(g, data_transitions_txt, &load_transition); } Transition *find_transition(Transitions *t, Room *from, Command via) { diff --git a/01_text_adventure/util.c b/01_text_adventure/util.c new file mode 100644 index 0000000..1bd928f --- /dev/null +++ b/01_text_adventure/util.c @@ -0,0 +1,23 @@ +#include +#include +#include "util.h" +#include "game.h" + +void parse_multiline_string(Game *g, char *string, void (*parse_line)(Game *g, char *line)) { + char *buffer = malloc(0); + char *sol = string; + char *eol = strchr(sol, '\n'); + while (eol != NULL) { + int line_length = eol - sol; + buffer = realloc(buffer, line_length + 1); + memcpy(buffer, sol, line_length); + buffer[line_length] = '\0'; + + parse_line(g, buffer); + + sol = eol + 1; + eol = strchr(sol, '\n'); + } + + free(buffer); +} diff --git a/01_text_adventure/util.h b/01_text_adventure/util.h new file mode 100644 index 0000000..1a0e963 --- /dev/null +++ b/01_text_adventure/util.h @@ -0,0 +1,7 @@ +#ifndef _FD_UTIL_ +#define _FD_UTIL_ +#include "game.h" + +void parse_multiline_string(Game *g, char *string, void (*parse_line)(Game *g, char *line)); + +#endif