techkaczynski.dev / code

summaryrefslogtreecommitdiff
path: root/save.c
diff options
context:
space:
mode:
Diffstat (limited to 'save.c')
-rw-r--r--save.c51
1 files changed, 48 insertions, 3 deletions
diff --git a/save.c b/save.c
index 23f2c1e..427ad3c 100644
--- a/save.c
+++ b/save.c
@@ -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) &&