#include #include #include #include "main.h" #include "util.h" #include "util.h" void *player_new(struct entities *entities); int player_property(void *const restrict entity, char const *const restrict property, char const *const restrict value); int game_save(FILE *file) { fwrite(game_level, 1, strlen(game_level) + 1, file); char buf[12] = {0}; snprintf(buf, sizeof (buf) - 1, "%i", from_fixed(entities.player[0].x)); fwrite(buf, 1, strlen(buf) + 1, file); snprintf(buf, sizeof (buf) - 1, "%i", from_fixed(entities.player[0].y)); fwrite(buf, 1, strlen(buf) + 1, file); fclose(file); return 0; } int game_load(FILE *file) { size_t filesize, len; char *filedata = util_loadFile(file, &filesize); // hack: we leak a tiny bit of memory here fclose(file); char *level = filedata; len = strnlen(filedata, filesize); if (len == filesize) { fputs("invalid save format\n", stderr); return 1; } filedata += len + 1; len = strnlen(filedata, filesize); if (len == filesize) { fputs("invalid save format\n", stderr); return 1; } player_property(next_entities.player, "x", filedata); filedata += len + 1; len = strnlen(filedata, filesize); if (len == filesize) { fputs("invalid save format\n", stderr); return 1; } player_property(next_entities.player, "y", filedata); level = realloc(level, strlen(level) + 1); return game_load_level(level); }