add a version to map files

This commit is contained in:
Sven Balzer 2025-09-27 08:38:46 +02:00
parent 434abdc9a7
commit a5c0db824b
2 changed files with 19 additions and 0 deletions

Binary file not shown.

View File

@ -176,6 +176,8 @@ struct Instance {
static Instance player_instance = {{ 0.0f, 0.0f }}; static Instance player_instance = {{ 0.0f, 0.0f }};
struct Map { struct Map {
Uint32 version;
Sint32 width; Sint32 width;
Sint32 height; Sint32 height;
@ -281,6 +283,8 @@ static WGPUBuffer create_buffer(WGPUBufferUsage usage, Uint32 num_bytes, void *d
return buffer; return buffer;
} }
#define MAP_FILE_VERSION (1u)
static bool save_map(Map map) { static bool save_map(Map map) {
char path[256] = ASSETS_PATH "maps/"; char path[256] = ASSETS_PATH "maps/";
SDL_strlcat(path, map.name, SDL_arraysize(path)); SDL_strlcat(path, map.name, SDL_arraysize(path));
@ -292,6 +296,11 @@ static bool save_map(Map map) {
} }
defer(SDL_CloseIO(file)); defer(SDL_CloseIO(file));
if (!SDL_WriteU32LE(file, MAP_FILE_VERSION)) {
log_error("Failed to write version to map file.");
return false;
}
if (!SDL_WriteS32LE(file, map.width)) { if (!SDL_WriteS32LE(file, map.width)) {
log_error("Failed to write width to map file."); log_error("Failed to write width to map file.");
return false; return false;
@ -334,6 +343,16 @@ static bool load_map(const char *name, Map *result) {
defer(SDL_CloseIO(file)); defer(SDL_CloseIO(file));
result->name = SDL_strdup(name); result->name = SDL_strdup(name);
if (!SDL_ReadU32LE(file, &result->version)) {
log_error("Failed read version from map file.");
return false;
}
if (result->version > MAP_FILE_VERSION) {
log_error("Map file version (%u) is higher than the highest supported.", result->version);
return false;
}
if (!SDL_ReadS32LE(file, &result->width)) { if (!SDL_ReadS32LE(file, &result->width)) {
log_error("Failed read width from map file."); log_error("Failed read width from map file.");
return false; return false;