feat: World loading that's broken
This commit is contained in:
@@ -34,6 +34,9 @@ void ProcessEvents(SDL_Event *event, World *world)
|
||||
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F5){
|
||||
SaveWorld(world->arena, world);
|
||||
}
|
||||
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F6){
|
||||
LoadWorld(world->arena, world);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderWorld(World *world, D_Context *draw) {
|
||||
@@ -42,7 +45,7 @@ void RenderWorld(World *world, D_Context *draw) {
|
||||
D_Rect(
|
||||
draw,
|
||||
(F32) (i % 96), (F32) (i / 96),
|
||||
.texture = world->tileTypes[world->map[i]].tile,
|
||||
.texture = D_ImageHandle(draw,world->tileTypes[world->map[i]].tag),
|
||||
.angle = (F32) world->tileTypes[world->map[i]].rotation,
|
||||
);
|
||||
}
|
||||
@@ -53,7 +56,7 @@ void RenderWorld(World *world, D_Context *draw) {
|
||||
draw,
|
||||
world->props[i].pos.x,
|
||||
world->props[i].pos.y,
|
||||
.texture = world->propTypes[world->props[i].propType].assetHandle,
|
||||
.texture = D_ImageHandle(draw,world->propTypes[world->props[i].propType].tag),
|
||||
.scale = world->propTypes[world->props[i].propType].scale,
|
||||
);
|
||||
}
|
||||
@@ -75,23 +78,90 @@ void RenderWorld(World *world, D_Context *draw) {
|
||||
void SaveWorld(M_Arena *arena, World *world) {
|
||||
printf("Saving world\n");
|
||||
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE);
|
||||
FS_FileWrite(file, world, sizeof(World)+sizeof(NavMesh), 0);
|
||||
FS_FileWrite(file, world->navMesh, sizeof(World)+sizeof(NavMesh), sizeof(World));
|
||||
U32 offset = 0;
|
||||
for(int i = 0; i < WORLD_TILE_TYPE_MAX; i++) {
|
||||
FS_FileWrite(file, &world->tileTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
FS_FileWrite(file, world->tileTypes[i].tag.data, world->tileTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->tileTypes[i].tag.count;
|
||||
FS_FileWrite(file, &world->tileTypes[i].rotation, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
for(int i = 0; i < WORLD_PROP_TYPE_MAX; i++) {
|
||||
FS_FileWrite(file, &world->propTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
FS_FileWrite(file, world->propTypes[i].tag.data, world->propTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->propTypes[i].tag.count;
|
||||
FS_FileWrite(file, &world->propTypes[i].scale, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
|
||||
FS_FileWrite(file, world->propTypes, sizeof(World_PropType)*WORLD_PROP_TYPE_MAX, offset);
|
||||
offset += sizeof(World_PropType)*WORLD_PROP_TYPE_MAX;
|
||||
|
||||
FS_FileWrite(file, &world->propCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
FS_FileWrite(file, world->props, sizeof(World_Prop)*WORLD_PROP_MAX, offset);
|
||||
offset += sizeof(World_Prop)*WORLD_PROP_MAX;
|
||||
|
||||
FS_FileWrite(file, &world->hitboxCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
FS_FileWrite(file, world->hitboxes, sizeof(AABB)*WORLD_HITBOX_MAX, offset);
|
||||
offset += sizeof(AABB)*WORLD_HITBOX_MAX;
|
||||
|
||||
FS_FileWrite(file, world->map, sizeof(U32)*WORLD_MAP_MAX, offset);
|
||||
offset += sizeof(U32)*WORLD_MAP_MAX;
|
||||
|
||||
FS_FileClose(file);
|
||||
printf("Saved world :)\n");
|
||||
}
|
||||
|
||||
World *LoadWorld(M_Arena *arena) {
|
||||
void LoadWorld(M_Arena *arena, World *world) {
|
||||
printf("loading world\n");
|
||||
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_READ);
|
||||
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));
|
||||
U32 offset = 0;
|
||||
|
||||
world->tileTypes = M_ArenaPush(arena, World_Tile, .count=WORLD_TILE_TYPE_MAX);
|
||||
for(int i = 0; i < WORLD_TILE_TYPE_MAX; i++) {
|
||||
FS_FileRead(file, &world->tileTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
world->tileTypes[i].tag.data = M_ArenaPush(arena, U8, .count=world->tileTypes[i].tag.count);
|
||||
FS_FileRead(file, world->tileTypes[i].tag.data, world->tileTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->tileTypes[i].tag.count;
|
||||
FS_FileRead(file, &world->tileTypes[i].rotation, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
world->propTypes = M_ArenaPush(arena, World_PropType, .count=WORLD_PROP_TYPE_MAX);
|
||||
for(int i = 0; i < WORLD_PROP_TYPE_MAX; i++) {
|
||||
FS_FileRead(file, &world->propTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
world->propTypes[i].tag.data = M_ArenaPush(arena, U8, .count=world->propTypes[i].tag.count);
|
||||
FS_FileRead(file, world->propTypes[i].tag.data, world->propTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->propTypes[i].tag.count;
|
||||
FS_FileRead(file, &world->propTypes[i].scale, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
|
||||
FS_FileRead(file, &world->propCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
world->props = M_ArenaPush(arena, World_Prop, .count=WORLD_PROP_MAX);
|
||||
FS_FileRead(file, world->props, sizeof(World_Prop)*WORLD_PROP_MAX, offset);
|
||||
offset += sizeof(World_Prop)*world->propCount;
|
||||
|
||||
FS_FileRead(file, &world->hitboxCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
world->hitboxes = M_ArenaPush(arena, AABB, .count=WORLD_HITBOX_MAX);
|
||||
FS_FileRead(file, world->hitboxes, sizeof(AABB)*WORLD_HITBOX_MAX, offset);
|
||||
offset += sizeof(AABB)*world->hitboxCount;
|
||||
|
||||
world->map = M_ArenaPush(arena, U32, .count=WORLD_MAP_MAX);
|
||||
FS_FileRead(file, world->map, sizeof(U32)*WORLD_MAP_MAX, offset);
|
||||
offset += sizeof(U32)*WORLD_MAP_MAX;
|
||||
|
||||
FS_FileClose(file);
|
||||
world->navMesh = navMesh;
|
||||
world->arena = arena;
|
||||
world->player.world = world;
|
||||
printf("loaded world\n");
|
||||
return world;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user