X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;ds=sidebyside;f=src%2Fclient%2Fio.c;h=c0cf977bf0d802ec24f9cb3207ab2154ebe59b0d;hb=9281cb7dd7656ff5d19e0747f819051a884649ec;hp=d0b9dc022b303f1e61de8a12d64e140c2273c8af;hpb=1cb57a35a3b3cc4ec8870531ca254a655c0bdda2;p=plomrogue diff --git a/src/client/io.c b/src/client/io.c index d0b9dc0..c0cf977 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -30,11 +30,11 @@ */ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file); -/* Read the next characters in "file" into world.map.cells. In detail: Read +/* Read the next characters in "file" into "map". In detail: Read * world.map.length times world.map.length characters, followed by one ignored * character (that we assume is a newline). */ -static void read_map_cells(FILE * file); +static void read_map_cells(FILE * file, char ** map); /* Repeatedly use try_fgets() with given arguments to read the remaining lines * of "file" into the world.log string. @@ -116,17 +116,22 @@ static void read_inventory(char * read_buf, uint32_t linemax, FILE * file) -static void read_map_cells(FILE * file) +static void read_map_cells(FILE * file, char ** map) { - free(world.map.cells); - world.map.cells = try_malloc(world.map.length * world.map.length, __func__); + if (*map) + { + free(*map); + *map = NULL; + } + *map = try_malloc(world.map.length * world.map.length, __func__); + char * map_cells = *map; uint16_t y, x; for (y = 0; y < world.map.length; y++) { for (x = 0; x < world.map.length; x++) { char c = try_fgetc(file, __func__); - world.map.cells[(y * world.map.length) + x] = c; + map_cells[y * world.map.length + x] = c; } try_fgetc(file, __func__); } @@ -213,7 +218,8 @@ static uint8_t read_world() first_read = 0; } world.map.length = read_value_from_line(read_buf, linemax, file); - read_map_cells(file); + read_map_cells(file, &world.map.cells); + read_map_cells(file, &world.mem_map); read_log(read_buf, linemax, file); free(read_buf); try_fclose(file, __func__);