Pull room and transition data into the binary
This commit is contained in:
		
							parent
							
								
									bdab6658b3
								
							
						
					
					
						commit
						a09f7106de
					
				
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,2 +1,4 @@ | |||||||
| raylib.h | raylib.h | ||||||
| game | game | ||||||
|  | 
 | ||||||
|  | data/*.c | ||||||
| @ -3,9 +3,14 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 | |||||||
| 
 | 
 | ||||||
| .PHONY: clean run | .PHONY: clean run | ||||||
| 
 | 
 | ||||||
| game: *.c | game: rooms.data transitions.data *.c | ||||||
| 	$(CC) *.c $(CFLAGS) -o game | 	$(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 | run: game | ||||||
| 	./game | 	./game | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ | |||||||
| #include "transition.h" | #include "transition.h" | ||||||
| #include "input.h" | #include "input.h" | ||||||
| #include "log.h" | #include "log.h" | ||||||
|  | #include "util.h" | ||||||
| 
 | 
 | ||||||
| Game *game_create(void) { | Game *game_create(void) { | ||||||
|   Game *g = malloc(sizeof(Game)); |   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) { | void game_handle_input(Game *g) { | ||||||
|   handle_pressed_keys(g->input); |   handle_pressed_keys(g->input); | ||||||
| } | } | ||||||
|  | |||||||
| @ -15,6 +15,7 @@ int main(void) { | |||||||
| 
 | 
 | ||||||
|     Game *g = game_create(); |     Game *g = game_create(); | ||||||
|     game_load_rooms(g); |     game_load_rooms(g); | ||||||
|  |     g->current_room = &g->rooms->rooms[0]; | ||||||
|     game_load_transitions(g); |     game_load_transitions(g); | ||||||
| 
 | 
 | ||||||
|     game_run_until_close(g); |     game_run_until_close(g); | ||||||
|  | |||||||
| @ -2,6 +2,26 @@ | |||||||
| #include <string.h> | #include <string.h> | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include "room.h" | #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) { | void free_room(Room r) { | ||||||
|   free(r.name); |   free(r.name); | ||||||
|  | |||||||
| @ -3,32 +3,28 @@ | |||||||
| #include <string.h> | #include <string.h> | ||||||
| #include "transition.h" | #include "transition.h" | ||||||
| #include "game.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]; |   token = strtok(NULL, "|"); | ||||||
| void game_load_transitions(Game *g) { |   g->transitions->transitions[g->transitions->count].via = command_from_string(token); | ||||||
|   FILE *transitions_file = fopen(TRANSITIONS_PATH, "r"); |  | ||||||
| 
 | 
 | ||||||
|   while ((fgets(transition_buffer, 2000, transitions_file)) != NULL) { |   token = strtok(NULL, "|"); | ||||||
|     char *token = strtok(transition_buffer, "|"); |   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, "\n"); | ||||||
| 
 |   g->transitions->transitions[g->transitions->count].description = malloc(strlen(token) + 1); | ||||||
|     token = strtok(NULL, "|"); |   strcpy(g->transitions->transitions[g->transitions->count].description, token); | ||||||
|     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); |  | ||||||
|      |      | ||||||
|     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) { | Transition *find_transition(Transitions *t, Room *from, Command via) { | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								01_text_adventure/util.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								01_text_adventure/util.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | |||||||
|  | #include <stdlib.h> | ||||||
|  | #include <string.h> | ||||||
|  | #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); | ||||||
|  | } | ||||||
							
								
								
									
										7
									
								
								01_text_adventure/util.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								01_text_adventure/util.h
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user