diff options
| author | tech kaczynski <tk@techkaczynski.dev> | 2026-10-22 13:27:04 +1000 |
|---|---|---|
| committer | tech kaczynski <tk@techkaczynski.dev> | 2026-10-22 13:27:04 +1000 |
| commit | 038cb9646b920936f8f497f9a4ee5b873853424e (patch) | |
| tree | a5df82a2e705c7f671ac9f2450dc88a68bbd25b0 /save.c | |
| parent | f5259b6c44b5902a0ca35de113fe16b9275ea13e (diff) | |
started work on command menu, added time of day editor.
Diffstat (limited to 'save.c')
| -rw-r--r-- | save.c | 51 |
1 files changed, 48 insertions, 3 deletions
@@ -1,5 +1,6 @@ #include "save.h" #include "ent.h" +#include "env.h" typedef struct { i64 size_pos; @@ -8,6 +9,7 @@ typedef struct { typedef struct { u64 next_pid; + f32 day_cur, day_len; } SaveLoadContext; static bool @@ -1082,6 +1084,37 @@ write_physics_chunk(FILE *fp) { return chunk_end(fp, &chunk); } + +static bool +read_environment_chunk(FILE *fp, SaveLoadContext *ctx) { + f32 day_cur, day_len; + + if (!read_f32_le(fp, &day_cur) || + !read_f32_le(fp, &day_len)) { + return false; + } + + if (!isfinite(day_cur) || + day_cur < 0.0f || day_cur >= 1.0f || + !isfinite(day_len) || day_len <= 0.0f) { + return false; + } + + ctx->day_cur = day_cur; + ctx->day_len = day_len; + return true; +} + +static bool +write_environment_chunk(FILE *fp) { + SaveChunk chunk; + + return chunk_begin(fp, "ENVR", &chunk) && + write_f32_le(fp, env.day_cur) && + write_f32_le(fp, env.day_len) && + chunk_end(fp, &chunk); +} + bool save_file_read(cstr path) { FILE *fp = fopen(path, "rb"); @@ -1090,7 +1123,10 @@ save_file_read(cstr path) { return false; } - SaveLoadContext ctx = {0}; + SaveLoadContext ctx = { + .day_cur = env.day_cur, + .day_len = env.day_len, + }; bool success = read_header(fp); bool read_entt = false; @@ -1122,13 +1158,15 @@ save_file_read(cstr path) { u32 end_pos = data_pos + chunk_size; - if (memcmp(label, "ENTT", 4) == 0) { + if (memcmp(label, "ENVR", 4) == 0) { + success = chunk_size >= 8 && + read_environment_chunk(fp, &ctx); + } else if (memcmp(label, "ENTT", 4) == 0) { if (read_entt) { TraceLog(LOG_ERROR, "duplicate ENTT chunk."); success = false; break; } - success = read_entity_chunk(fp, &ctx); read_entt = success; } else if (memcmp(label, "TRFM", 4) == 0) { @@ -1195,6 +1233,12 @@ save_file_read(cstr path) { success = false; } + if (success) { + env.day_cur = ctx.day_cur; + env.day_len = ctx.day_len; + env.cycle = env.day_cur; + } + return success; } @@ -1207,6 +1251,7 @@ save_file_write(cstr path) { bool success = write_header(fp) && + write_environment_chunk(fp) && write_entity_chunk(fp) && write_transform_chunk(fp) && write_model_chunk(fp) && |
