From 2f52bb309755e4866666b888619445e20899728b Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 6 Oct 2025 01:10:44 +0100 Subject: [PATCH] feat: save world written --- code/first.c | 1 + code/game/impl/world.c | 18 ++++++++++++++++++ code/game/world.h | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/code/first.c b/code/first.c index d80911f..6532887 100644 --- a/code/first.c +++ b/code/first.c @@ -75,6 +75,7 @@ int main(int argc, char **argv) game->draw.camera = camera; World *world = M_ArenaPush(arena, World); game->world = world; + world->arena = arena; world->random = Random_Seed(29237489723847); world->npcCount = 2; for(U32 i = 0; i < world->npcCount; i++) { diff --git a/code/game/impl/world.c b/code/game/impl/world.c index 3c36632..6016ace 100644 --- a/code/game/impl/world.c +++ b/code/game/impl/world.c @@ -3,6 +3,8 @@ #include "../player.h" #include "../aabb.h" #include +#include +#include void UpdateWorld(F32 delta, World *world) { @@ -30,6 +32,9 @@ void UpdateNPCs(F32 delta, World *world) void ProcessEvents(SDL_Event *event, World *world) { PlayerInput(event, &world->player); + if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F5){ + SaveWorld(world->arena, world); + } } void RenderWorld(World *world, D_Context *draw) { @@ -69,3 +74,16 @@ void G_WorldDraw(G_State *game, World *world) { } } } + +void SaveWorld(M_Arena *arena, World *world) { + printf("Saving world\n"); + World *saveWorld = M_ArenaPush(arena, World); + NavMesh *saveNavMesh = M_ArenaPush(arena, NavMesh); + M_CopySize(saveWorld, world, sizeof(World)); + M_CopySize(saveNavMesh, world->navMesh, sizeof(NavMesh)); + OS_Handle file =FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE); + FS_FileWrite(file, saveWorld, sizeof(World)+sizeof(NavMesh), 0); +} + +World *load(Str8 levelData) { +} diff --git a/code/game/world.h b/code/game/world.h index be8a8aa..33ca503 100644 --- a/code/game/world.h +++ b/code/game/world.h @@ -16,6 +16,8 @@ typedef struct World World; #include "bandit.h" struct World { + //// Utils + M_Arena *arena; //// Static stuff NavMesh *navMesh; Random random; @@ -43,4 +45,7 @@ function void UpdateNPCs(F32 delta, World *world); function void UpdateNPC(F32 delta, NPC *npc, World *world); function void UpdateBandit(F32 delta, Bandit *bandit, World *world); +function World *LoadWorld(Str8 levelData); +function void SaveWorld(M_Arena *arena, World *world); + #endif // LD_GAME_WORLD_H_