Fix issue with uninitialized memory
This commit is contained in:
		
							parent
							
								
									900ffecae8
								
							
						
					
					
						commit
						349dbfcab9
					
				| @ -1,10 +1,43 @@ | |||||||
|  | #include <stdio.h> | ||||||
|  | #include <string.h> | ||||||
|  | #include <stdlib.h> | ||||||
| #include "game.h" | #include "game.h" | ||||||
| 
 | 
 | ||||||
| void gs_handle_command(GameState *gs, const char *command) { | Game *game_create(void) { | ||||||
|   Input *input = gs->input; |   Game *g = malloc(sizeof(Game)); | ||||||
|  |   g->should_close = false; | ||||||
|  |   g->rooms.count = 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void game_handle_command(Game *g, const char *command) { | ||||||
|  |   Input *input = g->input; | ||||||
|   if (strcmp(input->input_buffer, "QUIT") == 0) { |   if (strcmp(input->input_buffer, "QUIT") == 0) { | ||||||
|     *(gs->should_close) = true; |     g->should_close = true; | ||||||
|   } else if (strcmp(input->input_buffer, "LOOK") == 0) { |   } else if (strcmp(input->input_buffer, "LOOK") == 0) { | ||||||
|     push_line_to_log(input->log, gs->rooms[0].description); |     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); | ||||||
|  | } | ||||||
|  | |||||||
| @ -2,17 +2,19 @@ | |||||||
| #define _FD_GAME_ | #define _FD_GAME_ | ||||||
| 
 | 
 | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| typedef struct GameState GameState; | typedef struct Game Game; | ||||||
| #include "input.h" | #include "input.h" | ||||||
| #include "room.h" | #include "room.h" | ||||||
| 
 | 
 | ||||||
| struct GameState { | struct Game { | ||||||
|   bool *should_close; |   bool should_close; | ||||||
|   Input *input; |   Input *input; | ||||||
|   Room *rooms; |   Rooms rooms; | ||||||
|   int rooms_count; |   Room *current_room; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| void gs_handle_command(GameState *gs, const char *command); | Game *game_create(void); | ||||||
|  | void game_handle_command(Game *g, const char *command); | ||||||
|  | void game_load_rooms(Game *g); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -39,7 +39,7 @@ void handleKeyPress(Input *input, int c) { | |||||||
|     pop_character(input); |     pop_character(input); | ||||||
|   } else if (c == ENTER) { |   } else if (c == ENTER) { | ||||||
|     push_command_to_log(input); |     push_command_to_log(input); | ||||||
|     gs_handle_command(input->gs, input->input_buffer); |     game_handle_command(input->g, input->input_buffer); | ||||||
|     clear_input_buffer(input); |     clear_input_buffer(input); | ||||||
|   } else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ') { |   } else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ') { | ||||||
|     push_character(input, (char) c); |     push_character(input, (char) c); | ||||||
|  | |||||||
| @ -14,7 +14,7 @@ struct Input { | |||||||
|   int input_length; |   int input_length; | ||||||
|   Vector2 position; |   Vector2 position; | ||||||
|   Log *log; |   Log *log; | ||||||
|   GameState *gs; |   Game *g; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| void handle_pressed_keys(Input*); | void handle_pressed_keys(Input*); | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ | |||||||
| #include "input.h" | #include "input.h" | ||||||
| 
 | 
 | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | #include <stdio.h> | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| 
 | 
 | ||||||
| #define TARGET_FPS 60 | #define TARGET_FPS 60 | ||||||
| @ -12,24 +13,20 @@ int main(void) { | |||||||
|     InitWindow(800, 450, "Text Adventure"); |     InitWindow(800, 450, "Text Adventure"); | ||||||
|     SetTargetFPS(TARGET_FPS); |     SetTargetFPS(TARGET_FPS); | ||||||
| 
 | 
 | ||||||
|     GameState *gs = malloc(sizeof(GameState)); |     Game *g = game_create(); | ||||||
|     gs->should_close = malloc(1); |  | ||||||
|     *(gs->should_close) = false; |  | ||||||
| 
 | 
 | ||||||
|     Log *log = create_log(); |     Log *log = create_log(); | ||||||
|     Vector2 input_position = { 190, 200 }; |     Vector2 input_position = { 190, 200 }; | ||||||
|     Input *input = create_input(input_position); |     Input *input = create_input(input_position); | ||||||
|     input->log = log; |     input->log = log; | ||||||
|     input->gs = gs; |     input->g = g; | ||||||
|     input->command = '>'; // Don't change this
 |     input->command = '>'; // Don't change this
 | ||||||
|     gs->input = input; |     g->input = input; | ||||||
| 
 | 
 | ||||||
|     gs->rooms = malloc(sizeof(Room)); |     game_load_rooms(g); | ||||||
|     Room *room = &gs->rooms[0]; |     g->current_room = &g->rooms.rooms[0]; | ||||||
|     room->name = "First room"; |  | ||||||
|     room->description = "You are in an enormous room. It is big but empty."; |  | ||||||
| 
 | 
 | ||||||
|     while (!WindowShouldClose() && !*(gs->should_close)) |     while (!WindowShouldClose() && !g->should_close) | ||||||
|       { |       { | ||||||
| 	BeginDrawing(); | 	BeginDrawing(); | ||||||
| 	ClearBackground(BLACK); | 	ClearBackground(BLACK); | ||||||
|  | |||||||
| @ -2,10 +2,16 @@ | |||||||
| #define _FD_ROOM_ | #define _FD_ROOM_ | ||||||
| 
 | 
 | ||||||
| typedef struct Room Room; | typedef struct Room Room; | ||||||
|  | typedef struct Rooms Rooms; | ||||||
| 
 | 
 | ||||||
| struct Room { | struct Room { | ||||||
|   char *name; |   char *name; | ||||||
|   char *description; |   char *description; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | struct Rooms { | ||||||
|  |   Room rooms[100]; | ||||||
|  |   int count; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
							
								
								
									
										3
									
								
								01_text_adventure/rooms.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								01_text_adventure/rooms.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | |||||||
|  | FIRST_ROOM|This is the initial room in the game. Nothing special about it. | ||||||
|  | SECOND_ROOM|This is another room. This one is painted blue. | ||||||
|  | LAST_ROOM|Another unremarkable room. | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user