Compare commits

...

3 Commits

Author SHA1 Message Date
ab8301f475 feat (BROKEN): Save world code 2025-10-06 01:49:10 +01:00
08cbfaeb71 Merge remote-tracking branch 'origin' 2025-10-06 01:12:08 +01:00
2f52bb3097 feat: save world written 2025-10-06 01:10:44 +01:00
3 changed files with 35 additions and 28 deletions

View File

@@ -95,8 +95,9 @@ int main(int argc, char **argv)
camera->farp = 1000.0f; camera->farp = 1000.0f;
game->draw.camera = camera; game->draw.camera = camera;
World *world = M_ArenaPush(arena, World); World *world = LoadWorld(arena);
game->world = world; game->world = world;
world->arena = arena;
world->random = Random_Seed(29237489723847); world->random = Random_Seed(29237489723847);
world->npcCount = 2; world->npcCount = 2;
for(U32 i = 0; i < world->npcCount; i++) { for(U32 i = 0; i < world->npcCount; i++) {
@@ -126,7 +127,6 @@ int main(int argc, char **argv)
badman->pointsOfInterest[0] = 937; badman->pointsOfInterest[0] = 937;
badman->pointsOfInterest[1] = 12; badman->pointsOfInterest[1] = 12;
world->navMesh = &TestNavMesh;
world->npcPOI[0] = 100; world->npcPOI[0] = 100;
world->player.world = world; world->player.world = world;
world->player.pos.x = 0; world->player.pos.x = 0;
@@ -213,8 +213,6 @@ int main(int argc, char **argv)
// D_Rect(&game->draw, -8.0f, 0.0f, .texture = 2, .scale = 2.0f); // D_Rect(&game->draw, -8.0f, 0.0f, .texture = 2, .scale = 2.0f);
// D_Rect(&game->draw, 6.0f, 0.0f, .texture = 3); // D_Rect(&game->draw, 6.0f, 0.0f, .texture = 3);
G_WorldDraw(game, game->world);
R2f aframe = D_AnimationFrame(&animation); R2f aframe = D_AnimationFrame(&animation);
D_Rect(&game->draw, 0, 0, .texture = animation.id, .uv = aframe, .flags = D_RECT_UV_ASPECT); D_Rect(&game->draw, 0, 0, .texture = animation.id, .uv = aframe, .flags = D_RECT_UV_ASPECT);

View File

@@ -3,6 +3,8 @@
#include "../player.h" #include "../player.h"
#include "../aabb.h" #include "../aabb.h"
#include <SDL3/SDL_events.h> #include <SDL3/SDL_events.h>
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_oldnames.h>
#include "../map.h" #include "../map.h"
void UpdateWorld(F32 delta, World *world) void UpdateWorld(F32 delta, World *world)
@@ -29,6 +31,9 @@ void UpdateNPCs(F32 delta, World *world)
void ProcessEvents(SDL_Event *event, World *world) void ProcessEvents(SDL_Event *event, World *world)
{ {
PlayerInput(event, &world->player); 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) { void RenderWorld(World *world, D_Context *draw) {
@@ -42,36 +47,35 @@ void RenderWorld(World *world, D_Context *draw) {
if(npc.currentArea == world->player.currentArea) { if(npc.currentArea == world->player.currentArea) {
V2f drawPos = AABB_Centre(npc.collision); V2f drawPos = AABB_Centre(npc.collision);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 1); D_Rect(draw, drawPos.x, drawPos.y, .texture = 1);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = npc.collision.size, .flags = D_RECT_IGNORE_ASPECT);
} }
} }
if(world->bandit.currentArea == world->player.currentArea) { if(world->bandit.currentArea == world->player.currentArea) {
V2f drawPos = AABB_Centre(world->bandit.collision); V2f drawPos = AABB_Centre(world->bandit.collision);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 9); D_Rect(draw, drawPos.x, drawPos.y, .texture = 9);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = world->bandit.collision.size, .flags = D_RECT_IGNORE_ASPECT);
} }
D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1); D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1);
} }
void G_WorldDraw(G_State *game, World *world) { void SaveWorld(M_Arena *arena, World *world) {
D_Context *draw = &game->draw; printf("Saving world\n");
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE);
(void) world; FS_FileWrite(file, world, sizeof(World)+sizeof(NavMesh), 0);
FS_FileWrite(file, world->navMesh, sizeof(World)+sizeof(NavMesh), sizeof(World));
U32 id = D_ImageHandle(&game->draw, S("tile_dirt_0")); FS_FileClose(file);
U32 alt_id = D_ImageHandle(&game->draw, S("tile_dirt_1")); printf("Saved world :)\n");
for (F32 y = -128; y < 128; y += 1.0f) {
for (F32 x = -128; x < 128; x += 1.0f) {
U32 ux = (U32) x;
U32 uy = (U32) y;
U32 tid = id;
if ((ux % 11) == 0 || ((uy % 7) == 0)) {
tid = alt_id;
} }
D_Rect(draw, x, y, .texture = tid); World *LoadWorld(M_Arena *arena) {
} printf("loading world\n");
} OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE);
World *world = M_ArenaPush(arena, World);
NavMesh *navMesh = M_ArenaPush(arena, NavMesh);
FS_FileRead(file, world, sizeof(World), 0);
FS_FileRead(file, navMesh, sizeof(NavMesh), sizeof(World));
FS_FileClose(file);
world->navMesh = navMesh;
world->arena = arena;
world->player.world = world;
printf("loaded world\n");
return world;
} }

View File

@@ -29,6 +29,8 @@ typedef struct World World;
#include "bandit.h" #include "bandit.h"
struct World { struct World {
//// Utils
M_Arena *arena;
//// Static stuff //// Static stuff
NavMesh *navMesh; NavMesh *navMesh;
Random random; Random random;
@@ -56,4 +58,7 @@ function void UpdateNPCs(F32 delta, World *world);
function void UpdateNPC(F32 delta, NPC *npc, World *world); function void UpdateNPC(F32 delta, NPC *npc, World *world);
function void UpdateBandit(F32 delta, Bandit *bandit, World *world); function void UpdateBandit(F32 delta, Bandit *bandit, World *world);
function World *LoadWorld(M_Arena *arena);
function void SaveWorld(M_Arena *arena, World *world);
#endif // LD_GAME_WORLD_H_ #endif // LD_GAME_WORLD_H_