feat(gui): add toolbar widget

feat(gui): add grid_select, toggle, panel, status_bar, image widgets
fix(bsp): fill in exterior cells
This commit is contained in:
asrael 2026-02-27 06:50:49 -06:00
parent 5a565844dd
commit 8d491612ab
63 changed files with 3150 additions and 1686 deletions

View file

@ -14,15 +14,31 @@
#include "pxl8_gfx3d.h"
#include "pxl8_log.h"
#include "pxl8_mem.h"
#include "pxl8_protocol.h"
#include "pxl8_sim.h"
#define PXL8_VIS_MAX_NODES (PXL8_WORLD_MAX_LOADED_CHUNKS * 512)
#define PXL8_VIS_MAX_QUEUE (PXL8_VIS_MAX_NODES * 4)
#define PXL8_VIS_BYTES ((PXL8_VIS_MAX_NODES + 7) / 8)
#define PXL8_WORLD_ENTITY_CAPACITY 256
typedef struct {
u16 chunk_idx;
u16 leaf_idx;
pxl8_rect window;
} world_vis_node;
struct pxl8_world {
pxl8_loaded_chunk loaded[PXL8_WORLD_MAX_LOADED_CHUNKS];
u32 loaded_count;
pxl8_world_chunk* active_chunk;
pxl8_bsp_render_state* active_render_state;
pxl8_gfx_material shared_materials[16];
bool shared_material_set[16];
pxl8_world_chunk_cache* chunk_cache;
pxl8_entity_pool* entities;
pxl8_bsp_render_state* bsp_render_state;
pxl8_sim_entity local_player;
u64 client_tick;
@ -30,6 +46,12 @@ struct pxl8_world {
pxl8_vec2 pointer_motion;
pxl8_sim_config sim_config;
u8 vis_bits[PXL8_VIS_BYTES];
u8* vis_ptrs[PXL8_WORLD_MAX_LOADED_CHUNKS];
pxl8_rect vis_windows[PXL8_VIS_MAX_NODES];
pxl8_rect* vis_win_ptrs[PXL8_WORLD_MAX_LOADED_CHUNKS];
world_vis_node vis_queue[PXL8_VIS_MAX_QUEUE];
#ifdef PXL8_ASYNC_THREADS
pxl8_sim_entity render_state[2];
atomic_uint active_buffer;
@ -72,9 +94,11 @@ pxl8_world* pxl8_world_create(void) {
void pxl8_world_destroy(pxl8_world* world) {
if (!world) return;
for (u32 i = 0; i < world->loaded_count; i++) {
pxl8_bsp_render_state_destroy(world->loaded[i].render_state);
}
pxl8_world_chunk_cache_destroy(world->chunk_cache);
pxl8_entity_pool_destroy(world->entities);
pxl8_bsp_render_state_destroy(world->bsp_render_state);
pxl8_free(world);
}
@ -88,27 +112,26 @@ pxl8_world_chunk* pxl8_world_active_chunk(pxl8_world* world) {
return world->active_chunk;
}
void pxl8_world_set_active_chunk(pxl8_world* world, pxl8_world_chunk* chunk) {
if (!world) return;
world->active_chunk = chunk;
}
pxl8_entity_pool* pxl8_world_entities(pxl8_world* world) {
if (!world) return NULL;
return world->entities;
}
pxl8_entity pxl8_world_spawn(pxl8_world* world) {
if (!world || !world->entities) return PXL8_ENTITY_INVALID;
return pxl8_entity_spawn(world->entities);
}
pxl8_sim_world pxl8_world_sim_world(const pxl8_world* world, pxl8_vec3 pos) {
(void)pos;
pxl8_sim_world sim = {0};
if (world->active_chunk && world->active_chunk->bsp) {
sim.bsp = world->active_chunk->bsp;
const f32 chunk_size = 16.0f * 64.0f;
sim.chunk_size = chunk_size;
i32 pcx = (i32)floorf(pos.x / chunk_size);
i32 pcz = (i32)floorf(pos.z / chunk_size);
sim.center_cx = pcx;
sim.center_cz = pcz;
for (u32 i = 0; i < world->loaded_count; i++) {
const pxl8_loaded_chunk* lc = &world->loaded[i];
if (!lc->chunk || !lc->chunk->bsp) continue;
i32 dx = lc->cx - pcx + 1;
i32 dz = lc->cz - pcz + 1;
if (dx >= 0 && dx <= 2 && dz >= 0 && dz <= 2) {
sim.chunks[dz * 3 + dx] = lc->chunk->bsp;
}
}
return sim;
}
@ -244,62 +267,452 @@ void pxl8_world_update(pxl8_world* world, f32 dt) {
pxl8_world_chunk_cache_tick(world->chunk_cache);
}
static inline bool vr_valid(pxl8_rect r) {
return r.x0 < r.x1 && r.y0 < r.y1;
}
static inline pxl8_rect vr_intersect(pxl8_rect a, pxl8_rect b) {
return (pxl8_rect){
.x0 = a.x0 > b.x0 ? a.x0 : b.x0,
.y0 = a.y0 > b.y0 ? a.y0 : b.y0,
.x1 = a.x1 < b.x1 ? a.x1 : b.x1,
.y1 = a.y1 < b.y1 ? a.y1 : b.y1,
};
}
static pxl8_rect project_portal(f32 px0, f32 pz0, f32 px1, f32 pz1,
f32 y_lo, f32 y_hi, const pxl8_mat4* vp) {
pxl8_vec3 corners[4] = {
{px0, y_lo, pz0}, {px1, y_lo, pz1},
{px1, y_hi, pz1}, {px0, y_hi, pz0},
};
const f32 NEAR_W = 0.001f;
pxl8_vec4 clip[4];
bool front[4];
i32 fc = 0;
for (i32 i = 0; i < 4; i++) {
clip[i] = pxl8_mat4_multiply_vec4(*vp, (pxl8_vec4){
corners[i].x, corners[i].y, corners[i].z, 1.0f});
front[i] = clip[i].w > NEAR_W;
if (front[i]) fc++;
}
if (fc == 0) return (pxl8_rect){0, 0, 0, 0};
if (fc < 4) return (pxl8_rect){-1.0f, -1.0f, 1.0f, 1.0f};
pxl8_rect r = {1e30f, 1e30f, -1e30f, -1e30f};
for (i32 i = 0; i < 4; i++) {
f32 iw = 1.0f / clip[i].w;
f32 nx = clip[i].x * iw, ny = clip[i].y * iw;
if (nx < r.x0) r.x0 = nx; if (nx > r.x1) r.x1 = nx;
if (ny < r.y0) r.y0 = ny; if (ny > r.y1) r.y1 = ny;
}
if (r.x0 < -1.0f) r.x0 = -1.0f;
if (r.y0 < -1.0f) r.y0 = -1.0f;
if (r.x1 > 1.0f) r.x1 = 1.0f;
if (r.y1 > 1.0f) r.y1 = 1.0f;
return r;
}
static void compute_edge_leafs(pxl8_loaded_chunk* lc) {
const f32 CHUNK_SIZE = 16.0f * 64.0f;
const pxl8_bsp* bsp = lc->chunk->bsp;
f32 cx0 = lc->cx * CHUNK_SIZE;
f32 cz0 = lc->cz * CHUNK_SIZE;
f32 cx1 = cx0 + CHUNK_SIZE;
f32 cz1 = cz0 + CHUNK_SIZE;
memset(lc->edges, 0, sizeof(lc->edges));
for (u32 i = 0; i < bsp->num_leafs; i++) {
const pxl8_bsp_leaf* leaf = &bsp->leafs[i];
if (bsp->leafs[i].contents == -1) continue;
if ((f32)leaf->mins[2] <= (f32)((i16)cz0) + 1.0f && lc->edges[0].count < 16)
lc->edges[0].leafs[lc->edges[0].count++] = (u16)i;
if ((f32)leaf->maxs[2] >= (f32)((i16)cz1) - 1.0f && lc->edges[1].count < 16)
lc->edges[1].leafs[lc->edges[1].count++] = (u16)i;
if ((f32)leaf->mins[0] <= (f32)((i16)cx0) + 1.0f && lc->edges[2].count < 16)
lc->edges[2].leafs[lc->edges[2].count++] = (u16)i;
if ((f32)leaf->maxs[0] >= (f32)((i16)cx1) - 1.0f && lc->edges[3].count < 16)
lc->edges[3].leafs[lc->edges[3].count++] = (u16)i;
}
}
static i32 world_find_chunk(const pxl8_world* world, i32 cx, i32 cz) {
for (u32 i = 0; i < world->loaded_count; i++) {
if (world->loaded[i].cx == cx && world->loaded[i].cz == cz &&
world->loaded[i].chunk && world->loaded[i].chunk->bsp)
return (i32)i;
}
return -1;
}
static void world_mark_leaf_faces(const pxl8_bsp* bsp, pxl8_bsp_render_state* rs, u32 leaf_idx) {
const pxl8_bsp_leaf* leaf = &bsp->leafs[leaf_idx];
for (u32 i = 0; i < leaf->num_marksurfaces; i++) {
u32 si = leaf->first_marksurface + i;
if (si < bsp->num_marksurfaces) {
u32 fi = bsp->marksurfaces[si];
if (fi < bsp->num_faces && rs)
rs->render_face_flags[fi] = 1;
}
}
}
static bool vis_try_enqueue(u8** vis, pxl8_rect** windows, world_vis_node* queue,
u32* tail, u32 max_queue,
u16 ci, u16 li, pxl8_rect nw) {
if (!vis[ci] || !windows[ci]) return false;
u32 byte = li >> 3;
u32 bit = 1 << (li & 7);
if (vis[ci][byte] & bit) {
pxl8_rect* ex = &windows[ci][li];
bool expanded = false;
if (nw.x0 < ex->x0) { ex->x0 = nw.x0; expanded = true; }
if (nw.y0 < ex->y0) { ex->y0 = nw.y0; expanded = true; }
if (nw.x1 > ex->x1) { ex->x1 = nw.x1; expanded = true; }
if (nw.y1 > ex->y1) { ex->y1 = nw.y1; expanded = true; }
if (expanded && *tail < max_queue)
queue[(*tail)++] = (world_vis_node){ci, li, *ex};
return expanded;
}
vis[ci][byte] |= bit;
windows[ci][li] = nw;
if (*tail < max_queue)
queue[(*tail)++] = (world_vis_node){ci, li, nw};
return true;
}
static void world_compute_visibility(pxl8_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos) {
const f32 CHUNK_SIZE = 16.0f * 64.0f;
const f32 PORTAL_Y_HI = 192.0f;
const pxl8_mat4* vp = pxl8_3d_get_view_proj(gfx);
i32 cam_ci = -1, cam_li = -1;
for (u32 i = 0; i < world->loaded_count; i++) {
pxl8_loaded_chunk* lc = &world->loaded[i];
if (!lc->chunk || !lc->chunk->bsp) continue;
const pxl8_bsp* bsp = lc->chunk->bsp;
f32 cx0 = lc->cx * CHUNK_SIZE;
f32 cz0 = lc->cz * CHUNK_SIZE;
if (camera_pos.x < cx0 || camera_pos.x >= cx0 + CHUNK_SIZE ||
camera_pos.z < cz0 || camera_pos.z >= cz0 + CHUNK_SIZE) continue;
if (bsp->bounds_max_x > bsp->bounds_min_x &&
(camera_pos.x < bsp->bounds_min_x || camera_pos.x >= bsp->bounds_max_x ||
camera_pos.z < bsp->bounds_min_z || camera_pos.z >= bsp->bounds_max_z))
continue;
i32 leaf = pxl8_bsp_find_leaf(bsp, camera_pos);
if (leaf >= 0 && (u32)leaf < bsp->num_leafs &&
bsp->leafs[leaf].contents != -1 &&
(!(bsp->bounds_max_x > bsp->bounds_min_x) || bsp->leafs[leaf].contents == -2)) {
cam_ci = (i32)i;
cam_li = leaf;
break;
}
}
if (cam_ci < 0) {
for (u32 i = 0; i < world->loaded_count; i++) {
pxl8_loaded_chunk* lc = &world->loaded[i];
if (!lc->chunk || !lc->chunk->bsp) continue;
const pxl8_bsp* bsp = lc->chunk->bsp;
if (bsp->bounds_max_x > bsp->bounds_min_x) continue;
i32 leaf = pxl8_bsp_find_leaf(bsp, camera_pos);
if (leaf < 0 || (u32)leaf >= bsp->num_leafs) continue;
const pxl8_bsp_leaf* l = &bsp->leafs[leaf];
if (l->contents == -1) continue;
if (camera_pos.x < (f32)l->mins[0] || camera_pos.x > (f32)l->maxs[0] ||
camera_pos.z < (f32)l->mins[2] || camera_pos.z > (f32)l->maxs[2]) continue;
cam_ci = (i32)i;
cam_li = leaf;
break;
}
}
if (cam_ci < 0 || !vp) {
for (u32 i = 0; i < world->loaded_count; i++) {
pxl8_bsp_render_state* rs = world->loaded[i].render_state;
if (!rs) continue;
if (rs->render_face_flags)
memset(rs->render_face_flags, 1, rs->num_faces);
rs->exterior = true;
}
return;
}
memset(world->vis_bits, 0, sizeof(world->vis_bits));
memset(world->vis_ptrs, 0, sizeof(world->vis_ptrs));
memset(world->vis_win_ptrs, 0, sizeof(world->vis_win_ptrs));
u32 voff = 0, woff = 0;
for (u32 i = 0; i < world->loaded_count; i++) {
if (world->loaded[i].chunk && world->loaded[i].chunk->bsp) {
u32 nl = world->loaded[i].chunk->bsp->num_leafs;
u32 vbytes = (nl + 7) / 8;
if (voff + vbytes > PXL8_VIS_BYTES || woff + nl > PXL8_VIS_MAX_NODES)
continue;
world->vis_ptrs[i] = world->vis_bits + voff;
voff += vbytes;
world->vis_win_ptrs[i] = world->vis_windows + woff;
woff += nl;
}
}
if (!world->vis_ptrs[cam_ci] || !world->vis_win_ptrs[cam_ci]) return;
for (u32 i = 0; i < world->loaded_count; i++) {
pxl8_bsp_render_state* rs = world->loaded[i].render_state;
if (!rs) continue;
if (rs->render_face_flags)
memset(rs->render_face_flags, 0, rs->num_faces);
rs->exterior = false;
}
u32 head = 0, tail = 0;
pxl8_rect full_screen = {-1.0f, -1.0f, 1.0f, 1.0f};
const pxl8_bsp* cam_bsp = world->loaded[cam_ci].chunk->bsp;
bool cam_exterior = cam_bsp->leafs[cam_li].contents == 0 &&
!(cam_bsp->bounds_max_x > cam_bsp->bounds_min_x);
world->vis_ptrs[cam_ci][cam_li >> 3] |= (1 << (cam_li & 7));
world->vis_win_ptrs[cam_ci][cam_li] = full_screen;
world->vis_queue[tail++] = (world_vis_node){(u16)cam_ci, (u16)cam_li, full_screen};
while (head < tail) {
world_vis_node cur = world->vis_queue[head++];
pxl8_loaded_chunk* lc = &world->loaded[cur.chunk_idx];
const pxl8_bsp* bsp = lc->chunk->bsp;
world_mark_leaf_faces(bsp, lc->render_state, cur.leaf_idx);
if (bsp->cell_portals && cur.leaf_idx < bsp->num_cell_portals) {
bool is_cam_leaf = (cur.chunk_idx == (u16)cam_ci && cur.leaf_idx == (u16)cam_li);
bool is_cam_chunk = (cur.chunk_idx == (u16)cam_ci);
const pxl8_bsp_cell_portals* cp = &bsp->cell_portals[cur.leaf_idx];
for (u8 pi = 0; pi < cp->num_portals; pi++) {
const pxl8_bsp_portal* p = &cp->portals[pi];
u32 target = p->target_leaf;
if (target >= bsp->num_leafs) continue;
if (bsp->leafs[target].contents == -1) continue;
if (is_cam_chunk && !pxl8_bsp_is_leaf_visible(bsp, cam_li, (i32)target)) continue;
if (is_cam_leaf || cam_exterior) {
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
world->vis_queue, &tail, PXL8_VIS_MAX_QUEUE,
cur.chunk_idx, (u16)target, full_screen);
continue;
}
pxl8_rect psr = project_portal(p->x0, p->z0, p->x1, p->z1,
0.0f, PORTAL_Y_HI, vp);
if (!vr_valid(psr)) continue;
pxl8_rect nw = vr_intersect(cur.window, psr);
if (!vr_valid(nw)) continue;
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
world->vis_queue, &tail, PXL8_VIS_MAX_QUEUE,
cur.chunk_idx, (u16)target, nw);
}
}
const pxl8_bsp_leaf* leaf = &bsp->leafs[cur.leaf_idx];
f32 chunk_x0 = lc->cx * CHUNK_SIZE;
f32 chunk_z0 = lc->cz * CHUNK_SIZE;
f32 chunk_x1 = chunk_x0 + CHUNK_SIZE;
f32 chunk_z1 = chunk_z0 + CHUNK_SIZE;
struct { i32 dx, dz; bool at_edge; f32 bnd; } dirs[4] = {
{ 0, -1, (f32)leaf->mins[2] <= (f32)((i16)chunk_z0) + 1.0f, chunk_z0 },
{ 0, 1, (f32)leaf->maxs[2] >= (f32)((i16)chunk_z1) - 1.0f, chunk_z1 },
{-1, 0, (f32)leaf->mins[0] <= (f32)((i16)chunk_x0) + 1.0f, chunk_x0 },
{ 1, 0, (f32)leaf->maxs[0] >= (f32)((i16)chunk_x1) - 1.0f, chunk_x1 },
};
static const u8 opposite_edge[4] = {1, 0, 3, 2};
for (u32 d = 0; d < 4; d++) {
if (!dirs[d].at_edge) continue;
i32 nci = world_find_chunk(world, lc->cx + dirs[d].dx, lc->cz + dirs[d].dz);
if (nci < 0) continue;
const pxl8_bsp* nbsp = world->loaded[nci].chunk->bsp;
const pxl8_edge_leafs* nedge = &world->loaded[nci].edges[opposite_edge[d]];
for (u8 k = 0; k < nedge->count; k++) {
u16 nl = nedge->leafs[k];
const pxl8_bsp_leaf* nleaf = &nbsp->leafs[nl];
bool overlaps = false;
if (dirs[d].dx != 0) {
overlaps = leaf->maxs[2] > nleaf->mins[2] && leaf->mins[2] < nleaf->maxs[2];
} else {
overlaps = leaf->maxs[0] > nleaf->mins[0] && leaf->mins[0] < nleaf->maxs[0];
}
if (!overlaps) continue;
f32 bpx0, bpz0, bpx1, bpz1;
if (dirs[d].dx != 0) {
bpx0 = dirs[d].bnd; bpx1 = dirs[d].bnd;
i16 zlo = leaf->mins[2] > nleaf->mins[2] ? leaf->mins[2] : nleaf->mins[2];
i16 zhi = leaf->maxs[2] < nleaf->maxs[2] ? leaf->maxs[2] : nleaf->maxs[2];
bpz0 = (f32)zlo; bpz1 = (f32)zhi;
} else {
bpz0 = dirs[d].bnd; bpz1 = dirs[d].bnd;
i16 xlo = leaf->mins[0] > nleaf->mins[0] ? leaf->mins[0] : nleaf->mins[0];
i16 xhi = leaf->maxs[0] < nleaf->maxs[0] ? leaf->maxs[0] : nleaf->maxs[0];
bpx0 = (f32)xlo; bpx1 = (f32)xhi;
}
if (cam_exterior) {
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
world->vis_queue, &tail, PXL8_VIS_MAX_QUEUE,
(u16)nci, (u16)nl, full_screen);
continue;
}
pxl8_rect psr = project_portal(bpx0, bpz0, bpx1, bpz1,
0.0f, PORTAL_Y_HI, vp);
if (!vr_valid(psr)) continue;
pxl8_rect nw = vr_intersect(cur.window, psr);
if (!vr_valid(nw)) continue;
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
world->vis_queue, &tail, PXL8_VIS_MAX_QUEUE,
(u16)nci, (u16)nl, nw);
}
}
}
}
void pxl8_world_render(pxl8_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos) {
if (!world || !gfx) return;
if (world->active_chunk && world->active_chunk->bsp) {
if (world->active_chunk && world->active_chunk->bsp)
pxl8_3d_set_bsp(gfx, world->active_chunk->bsp);
pxl8_bsp_render(gfx, world->active_chunk->bsp,
world->bsp_render_state, camera_pos);
} else {
else
pxl8_3d_set_bsp(gfx, NULL);
world_compute_visibility(world, gfx, camera_pos);
for (u32 i = 0; i < world->loaded_count; i++) {
pxl8_loaded_chunk* lc = &world->loaded[i];
if (!lc->chunk || !lc->chunk->bsp || !lc->render_state) continue;
pxl8_bsp_render(gfx, lc->chunk->bsp, lc->render_state, NULL);
}
}
static void apply_shared_materials(pxl8_world* world, pxl8_bsp_render_state* rs) {
for (u16 i = 0; i < 16; i++) {
if (world->shared_material_set[i]) {
pxl8_bsp_set_material(rs, i, &world->shared_materials[i]);
}
}
}
void pxl8_world_sync(pxl8_world* world, pxl8_net* net) {
if (!world || !net) return;
u32 chunk_id = pxl8_net_chunk_id(net);
if (!pxl8_net_has_chunk(net)) {
u32 old_count = world->loaded_count;
world->loaded_count = 0;
world->active_chunk = NULL;
world->active_render_state = NULL;
for (u32 i = 0; i < old_count; i++) {
pxl8_bsp_render_state_destroy(world->loaded[i].render_state);
}
return;
}
if (chunk_id != 0) {
pxl8_world_chunk* chunk = pxl8_world_chunk_cache_get_bsp(world->chunk_cache, chunk_id);
if (chunk && chunk->bsp) {
if (world->active_chunk != chunk) {
world->active_chunk = chunk;
pxl8_debug("[CLIENT] Synced BSP chunk id=%u as active (verts=%u faces=%u)",
chunk_id, chunk->bsp->num_vertices, chunk->bsp->num_faces);
i32 center_cx = pxl8_net_chunk_cx(net);
i32 center_cz = pxl8_net_chunk_cz(net);
if (world->bsp_render_state) {
pxl8_bsp_render_state_destroy(world->bsp_render_state);
world->bsp_render_state = NULL;
pxl8_loaded_chunk old_loaded[PXL8_WORLD_MAX_LOADED_CHUNKS];
u32 old_count = world->loaded_count;
memcpy(old_loaded, world->loaded, sizeof(old_loaded));
pxl8_loaded_chunk new_loaded[PXL8_WORLD_MAX_LOADED_CHUNKS];
u32 new_count = 0;
pxl8_world_chunk* new_active_chunk = NULL;
pxl8_bsp_render_state* new_active_rs = NULL;
for (i32 dz = -2; dz <= 2; dz++) {
for (i32 dx = -2; dx <= 2; dx++) {
i32 cx = center_cx + dx;
i32 cz = center_cz + dz;
u32 id = pxl8_chunk_hash(cx, cz);
pxl8_world_chunk* chunk = pxl8_world_chunk_cache_get_bsp(world->chunk_cache, id);
if (!chunk || !chunk->bsp) continue;
pxl8_bsp_render_state* rs = NULL;
for (u32 j = 0; j < old_count; j++) {
if (old_loaded[j].active && old_loaded[j].cx == cx && old_loaded[j].cz == cz &&
old_loaded[j].chunk == chunk) {
rs = old_loaded[j].render_state;
old_loaded[j].active = false;
break;
}
world->bsp_render_state = pxl8_bsp_render_state_create(chunk->bsp->num_faces);
}
if (!rs) {
rs = pxl8_bsp_render_state_create(chunk->bsp->num_faces);
if (rs && rs->render_face_flags)
memset(rs->render_face_flags, 1, rs->num_faces);
apply_shared_materials(world, rs);
}
u32 idx = new_count++;
new_loaded[idx] = (pxl8_loaded_chunk){
.chunk = chunk,
.render_state = rs,
.cx = cx,
.cz = cz,
.active = true,
};
compute_edge_leafs(&new_loaded[idx]);
if (dx == 0 && dz == 0) {
new_active_chunk = chunk;
new_active_rs = rs;
}
}
} else if (world->active_chunk != NULL) {
world->active_chunk = NULL;
if (world->bsp_render_state) {
pxl8_bsp_render_state_destroy(world->bsp_render_state);
world->bsp_render_state = NULL;
}
memcpy(world->loaded, new_loaded, sizeof(new_loaded));
world->active_chunk = new_active_chunk;
world->active_render_state = new_active_rs;
world->loaded_count = new_count;
for (u32 j = 0; j < old_count; j++) {
if (old_loaded[j].active) {
pxl8_bsp_render_state_destroy(old_loaded[j].render_state);
}
}
}
static void ensure_bsp_render_state(pxl8_world* world) {
if (!world || world->bsp_render_state) return;
if (!world->active_chunk) return;
if (!world->active_chunk->bsp) return;
world->bsp_render_state = pxl8_bsp_render_state_create(world->active_chunk->bsp->num_faces);
}
void pxl8_world_set_bsp_material(pxl8_world* world, u16 material_id, const pxl8_gfx_material* material) {
if (!world || !material) return;
if (!world || !material || material_id >= 16) return;
ensure_bsp_render_state(world);
if (!world->bsp_render_state) return;
world->shared_materials[material_id] = *material;
world->shared_material_set[material_id] = true;
pxl8_bsp_set_material(world->bsp_render_state, material_id, material);
for (u32 i = 0; i < world->loaded_count; i++) {
if (world->loaded[i].render_state) {
pxl8_bsp_set_material(world->loaded[i].render_state, material_id, material);
}
}
}
void pxl8_world_set_sim_config(pxl8_world* world, const pxl8_sim_config* config) {
@ -316,6 +729,7 @@ void pxl8_world_init_local_player(pxl8_world* world, f32 x, f32 y, f32 z) {
world->local_player.flags = PXL8_SIM_FLAG_ALIVE | PXL8_SIM_FLAG_PLAYER | PXL8_SIM_FLAG_GROUNDED;
world->local_player.kind = 0;
world->client_tick = 0;
world->pointer_motion = (pxl8_vec2){0};
#ifdef PXL8_ASYNC_THREADS
world->render_state[0] = world->local_player;
@ -323,6 +737,20 @@ void pxl8_world_init_local_player(pxl8_world* world, f32 x, f32 y, f32 z) {
#endif
}
void pxl8_world_set_look(pxl8_world* world, f32 yaw, f32 pitch) {
if (!world) return;
world->local_player.yaw = yaw;
world->local_player.pitch = pitch;
world->pointer_motion = (pxl8_vec2){.yaw = yaw, .pitch = pitch};
#ifdef PXL8_ASYNC_THREADS
world->render_state[0].yaw = yaw;
world->render_state[0].pitch = pitch;
world->render_state[1].yaw = yaw;
world->render_state[1].pitch = pitch;
#endif
}
pxl8_sim_entity* pxl8_world_local_player(pxl8_world* world) {
if (!world) return NULL;
#ifdef PXL8_ASYNC_THREADS