Skip to content

Commit

Permalink
Fix loading inexistant save (#118)
Browse files Browse the repository at this point in the history
* Correct error whene trying to continue when no save

* Add a new line after no saved game message

---------

Co-authored-by: opixelum <[email protected]>
  • Loading branch information
lseillier229 and opixelum authored Nov 13, 2023
1 parent adbe667 commit 1dc8a7d
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,40 @@ void main_menu(unsigned char *is_running)
break;

case 2:
player = load_game();
MapContext *map_context = get_map_context("doomdepths.db");
if(!map_context)
{
FILE *file = fopen("doomdepths.db", "r+");

if (!file)
{
map_context = malloc(sizeof *map_context);
map_context->pos_x = 0;
map_context->pos_y = 0;
clear_screen();
printf("There's no saved game. Start a new one!\n\n");
press_any_key_to_continue();
}
map_context->player = player;

get_map(map_context);
explore_map(map_context);
else
{
fclose(file);
player = load_game();
MapContext *map_context = get_map_context("doomdepths.db");
if (!map_context) map_context = malloc(sizeof *map_context);
if (!map_context)
{
fprintf
(
stderr,
"ERROR: menus.c: main_menu(): malloc failed\n"
);
exit(EXIT_FAILURE);
}

free_character(player);
free(map_context);
map_context->player = player;
get_map(map_context);
explore_map(map_context);

free_character(player);
free(map_context);
}
break;
}

case 3:
clear_screen();
Expand Down Expand Up @@ -181,13 +197,15 @@ void new_game(void)
);

create_tables("doomdepths.db");
save_game(player);

initialize_map();
MapContext *map_context = malloc(sizeof *map_context);
map_context->player = player;
get_map(map_context);

save_game(player);
save_map_context("doomdepths.db", map_context);

printf("\nWelcome %s!\n\n", player->name);
press_any_key_to_continue();

Expand Down

0 comments on commit 1dc8a7d

Please sign in to comment.