add a byte stream: pxl8_stream

This commit is contained in:
asrael 2025-10-12 05:02:19 -05:00
parent ede16ca7de
commit 82ed6b4ea9
7 changed files with 350 additions and 273 deletions

View file

@ -4,6 +4,7 @@
#include "pxl8_bsp.h"
#include "pxl8_gfx.h"
#include "pxl8_io.h"
#include "pxl8_macros.h"
#define BSP_VERSION 29
@ -37,27 +38,20 @@ typedef struct {
pxl8_bsp_chunk chunks[CHUNK_COUNT];
} pxl8_bsp_header;
static u16 read_u16(const u8* data) {
return (u16)data[0] | ((u16)data[1] << 8);
static inline pxl8_vec3 read_vec3(pxl8_stream* stream) {
pxl8_vec3 v;
v.x = pxl8_read_f32(stream);
v.y = pxl8_read_f32(stream);
v.z = pxl8_read_f32(stream);
return v;
}
static u32 read_u32(const u8* data) {
return (u32)data[0] | ((u32)data[1] << 8) | ((u32)data[2] << 16) | ((u32)data[3] << 24);
}
static i16 read_i16(const u8* data) {
return (i16)read_u16(data);
}
static i32 read_i32(const u8* data) {
return (i32)read_u32(data);
}
static f32 read_f32(const u8* data) {
u32 val = read_u32(data);
f32 result;
memcpy(&result, &val, sizeof(f32));
return result;
static bool validate_chunk(const pxl8_bsp_chunk* chunk, u32 element_size, size_t file_size) {
if (chunk->size == 0) return true;
if (chunk->offset >= file_size) return false;
if (chunk->offset + chunk->size > file_size) return false;
if (chunk->size % element_size != 0) return false;
return true;
}
pxl8_result pxl8_bsp_load(const char* path, pxl8_bsp* bsp) {
@ -78,8 +72,10 @@ pxl8_result pxl8_bsp_load(const char* path, pxl8_bsp* bsp) {
return PXL8_ERROR_INVALID_FORMAT;
}
pxl8_stream stream = pxl8_stream_create(file_data, (u32)file_size);
pxl8_bsp_header header;
header.version = read_u32(file_data);
header.version = pxl8_read_u32(&stream);
if (header.version != BSP_VERSION) {
pxl8_error("Invalid BSP version: %u (expected %d)", header.version, BSP_VERSION);
@ -88,184 +84,169 @@ pxl8_result pxl8_bsp_load(const char* path, pxl8_bsp* bsp) {
}
for (i32 i = 0; i < CHUNK_COUNT; i++) {
header.chunks[i].offset = read_u32(file_data + 4 + i * 8);
header.chunks[i].size = read_u32(file_data + 4 + i * 8 + 4);
header.chunks[i].offset = pxl8_read_u32(&stream);
header.chunks[i].size = pxl8_read_u32(&stream);
}
pxl8_bsp_chunk* vertices_chunk = &header.chunks[CHUNK_VERTICES];
bsp->num_vertices = vertices_chunk->size / 12;
pxl8_bsp_chunk* chunk = &header.chunks[CHUNK_VERTICES];
if (!validate_chunk(chunk, 12, file_size)) goto error_cleanup;
bsp->num_vertices = chunk->size / 12;
if (bsp->num_vertices > 0) {
bsp->vertices = (pxl8_bsp_vertex*)SDL_calloc(bsp->num_vertices, sizeof(pxl8_bsp_vertex));
const u8* data = file_data + vertices_chunk->offset;
bsp->vertices = SDL_calloc(bsp->num_vertices, sizeof(pxl8_bsp_vertex));
if (!bsp->vertices) goto error_cleanup;
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_vertices; i++) {
bsp->vertices[i].position.x = read_f32(data + i * 12);
bsp->vertices[i].position.y = read_f32(data + i * 12 + 4);
bsp->vertices[i].position.z = read_f32(data + i * 12 + 8);
bsp->vertices[i].position = read_vec3(&stream);
}
}
pxl8_bsp_chunk* edges_chunk = &header.chunks[CHUNK_EDGES];
bsp->num_edges = edges_chunk->size / 4;
chunk = &header.chunks[CHUNK_EDGES];
if (!validate_chunk(chunk, 4, file_size)) goto error_cleanup;
bsp->num_edges = chunk->size / 4;
if (bsp->num_edges > 0) {
bsp->edges = (pxl8_bsp_edge*)SDL_calloc(bsp->num_edges, sizeof(pxl8_bsp_edge));
const u8* data = file_data + edges_chunk->offset;
bsp->edges = SDL_calloc(bsp->num_edges, sizeof(pxl8_bsp_edge));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_edges; i++) {
bsp->edges[i].vertex[0] = read_u16(data + i * 4);
bsp->edges[i].vertex[1] = read_u16(data + i * 4 + 2);
bsp->edges[i].vertex[0] = pxl8_read_u16(&stream);
bsp->edges[i].vertex[1] = pxl8_read_u16(&stream);
}
}
pxl8_bsp_chunk* surfedges_chunk = &header.chunks[CHUNK_SURFEDGES];
bsp->num_surfedges = surfedges_chunk->size / 4;
chunk = &header.chunks[CHUNK_SURFEDGES];
if (!validate_chunk(chunk, 4, file_size)) goto error_cleanup;
bsp->num_surfedges = chunk->size / 4;
if (bsp->num_surfedges > 0) {
bsp->surfedges = (i32*)SDL_calloc(bsp->num_surfedges, sizeof(i32));
const u8* data = file_data + surfedges_chunk->offset;
bsp->surfedges = SDL_calloc(bsp->num_surfedges, sizeof(i32));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_surfedges; i++) {
bsp->surfedges[i] = read_i32(data + i * 4);
bsp->surfedges[i] = pxl8_read_i32(&stream);
}
}
pxl8_bsp_chunk* planes_chunk = &header.chunks[CHUNK_PLANES];
bsp->num_planes = planes_chunk->size / 20;
chunk = &header.chunks[CHUNK_PLANES];
if (!validate_chunk(chunk, 20, file_size)) goto error_cleanup;
bsp->num_planes = chunk->size / 20;
if (bsp->num_planes > 0) {
bsp->planes = (pxl8_bsp_plane*)SDL_calloc(bsp->num_planes, sizeof(pxl8_bsp_plane));
const u8* data = file_data + planes_chunk->offset;
bsp->planes = SDL_calloc(bsp->num_planes, sizeof(pxl8_bsp_plane));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_planes; i++) {
bsp->planes[i].normal.x = read_f32(data + i * 20);
bsp->planes[i].normal.y = read_f32(data + i * 20 + 4);
bsp->planes[i].normal.z = read_f32(data + i * 20 + 8);
bsp->planes[i].dist = read_f32(data + i * 20 + 12);
bsp->planes[i].type = read_i32(data + i * 20 + 16);
bsp->planes[i].normal = read_vec3(&stream);
bsp->planes[i].dist = pxl8_read_f32(&stream);
bsp->planes[i].type = pxl8_read_i32(&stream);
}
}
pxl8_bsp_chunk* texinfo_chunk = &header.chunks[CHUNK_TEXINFO];
bsp->num_texinfo = texinfo_chunk->size / 40;
chunk = &header.chunks[CHUNK_TEXINFO];
if (!validate_chunk(chunk, 40, file_size)) goto error_cleanup;
bsp->num_texinfo = chunk->size / 40;
if (bsp->num_texinfo > 0) {
bsp->texinfo = (pxl8_bsp_texinfo*)SDL_calloc(bsp->num_texinfo, sizeof(pxl8_bsp_texinfo));
const u8* data = file_data + texinfo_chunk->offset;
bsp->texinfo = SDL_calloc(bsp->num_texinfo, sizeof(pxl8_bsp_texinfo));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_texinfo; i++) {
bsp->texinfo[i].u_axis.x = read_f32(data + i * 40);
bsp->texinfo[i].u_axis.y = read_f32(data + i * 40 + 4);
bsp->texinfo[i].u_axis.z = read_f32(data + i * 40 + 8);
bsp->texinfo[i].u_offset = read_f32(data + i * 40 + 12);
bsp->texinfo[i].v_axis.x = read_f32(data + i * 40 + 16);
bsp->texinfo[i].v_axis.y = read_f32(data + i * 40 + 20);
bsp->texinfo[i].v_axis.z = read_f32(data + i * 40 + 24);
bsp->texinfo[i].v_offset = read_f32(data + i * 40 + 28);
bsp->texinfo[i].miptex = read_u32(data + i * 40 + 32);
bsp->texinfo[i].u_axis = read_vec3(&stream);
bsp->texinfo[i].u_offset = pxl8_read_f32(&stream);
bsp->texinfo[i].v_axis = read_vec3(&stream);
bsp->texinfo[i].v_offset = pxl8_read_f32(&stream);
bsp->texinfo[i].miptex = pxl8_read_u32(&stream);
}
}
pxl8_bsp_chunk* faces_chunk = &header.chunks[CHUNK_FACES];
bsp->num_faces = faces_chunk->size / 20;
chunk = &header.chunks[CHUNK_FACES];
if (!validate_chunk(chunk, 20, file_size)) goto error_cleanup;
bsp->num_faces = chunk->size / 20;
if (bsp->num_faces > 0) {
bsp->faces = (pxl8_bsp_face*)SDL_calloc(bsp->num_faces, sizeof(pxl8_bsp_face));
const u8* data = file_data + faces_chunk->offset;
bsp->faces = SDL_calloc(bsp->num_faces, sizeof(pxl8_bsp_face));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_faces; i++) {
bsp->faces[i].plane_id = read_u16(data + i * 20);
bsp->faces[i].side = read_u16(data + i * 20 + 2);
bsp->faces[i].first_edge = read_u32(data + i * 20 + 4);
bsp->faces[i].num_edges = read_u16(data + i * 20 + 8);
bsp->faces[i].texinfo_id = read_u16(data + i * 20 + 10);
bsp->faces[i].styles[0] = data[i * 20 + 12];
bsp->faces[i].styles[1] = data[i * 20 + 13];
bsp->faces[i].styles[2] = data[i * 20 + 14];
bsp->faces[i].styles[3] = data[i * 20 + 15];
bsp->faces[i].lightmap_offset = read_u32(data + i * 20 + 16);
bsp->faces[i].plane_id = pxl8_read_u16(&stream);
bsp->faces[i].side = pxl8_read_u16(&stream);
bsp->faces[i].first_edge = pxl8_read_u32(&stream);
bsp->faces[i].num_edges = pxl8_read_u16(&stream);
bsp->faces[i].texinfo_id = pxl8_read_u16(&stream);
bsp->faces[i].styles[0] = pxl8_read_u8(&stream);
bsp->faces[i].styles[1] = pxl8_read_u8(&stream);
bsp->faces[i].styles[2] = pxl8_read_u8(&stream);
bsp->faces[i].styles[3] = pxl8_read_u8(&stream);
bsp->faces[i].lightmap_offset = pxl8_read_u32(&stream);
}
}
pxl8_bsp_chunk* nodes_chunk = &header.chunks[CHUNK_NODES];
bsp->num_nodes = nodes_chunk->size / 24;
chunk = &header.chunks[CHUNK_NODES];
if (!validate_chunk(chunk, 24, file_size)) goto error_cleanup;
bsp->num_nodes = chunk->size / 24;
if (bsp->num_nodes > 0) {
bsp->nodes = (pxl8_bsp_node*)SDL_calloc(bsp->num_nodes, sizeof(pxl8_bsp_node));
const u8* data = file_data + nodes_chunk->offset;
bsp->nodes = SDL_calloc(bsp->num_nodes, sizeof(pxl8_bsp_node));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_nodes; i++) {
bsp->nodes[i].plane_id = read_u32(data + i * 24);
bsp->nodes[i].children[0] = read_i16(data + i * 24 + 4);
bsp->nodes[i].children[1] = read_i16(data + i * 24 + 6);
bsp->nodes[i].mins[0] = read_i16(data + i * 24 + 8);
bsp->nodes[i].mins[1] = read_i16(data + i * 24 + 10);
bsp->nodes[i].mins[2] = read_i16(data + i * 24 + 12);
bsp->nodes[i].maxs[0] = read_i16(data + i * 24 + 14);
bsp->nodes[i].maxs[1] = read_i16(data + i * 24 + 16);
bsp->nodes[i].maxs[2] = read_i16(data + i * 24 + 18);
bsp->nodes[i].first_face = read_u16(data + i * 24 + 20);
bsp->nodes[i].num_faces = read_u16(data + i * 24 + 22);
bsp->nodes[i].plane_id = pxl8_read_u32(&stream);
bsp->nodes[i].children[0] = pxl8_read_i16(&stream);
bsp->nodes[i].children[1] = pxl8_read_i16(&stream);
for (u32 j = 0; j < 3; j++) bsp->nodes[i].mins[j] = pxl8_read_i16(&stream);
for (u32 j = 0; j < 3; j++) bsp->nodes[i].maxs[j] = pxl8_read_i16(&stream);
bsp->nodes[i].first_face = pxl8_read_u16(&stream);
bsp->nodes[i].num_faces = pxl8_read_u16(&stream);
}
}
pxl8_bsp_chunk* leafs_chunk = &header.chunks[CHUNK_LEAFS];
bsp->num_leafs = leafs_chunk->size / 28;
chunk = &header.chunks[CHUNK_LEAFS];
if (!validate_chunk(chunk, 28, file_size)) goto error_cleanup;
bsp->num_leafs = chunk->size / 28;
if (bsp->num_leafs > 0) {
bsp->leafs = (pxl8_bsp_leaf*)SDL_calloc(bsp->num_leafs, sizeof(pxl8_bsp_leaf));
const u8* data = file_data + leafs_chunk->offset;
bsp->leafs = SDL_calloc(bsp->num_leafs, sizeof(pxl8_bsp_leaf));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_leafs; i++) {
bsp->leafs[i].contents = read_i32(data + i * 28);
bsp->leafs[i].visofs = read_i32(data + i * 28 + 4);
bsp->leafs[i].mins[0] = read_i16(data + i * 28 + 8);
bsp->leafs[i].mins[1] = read_i16(data + i * 28 + 10);
bsp->leafs[i].mins[2] = read_i16(data + i * 28 + 12);
bsp->leafs[i].maxs[0] = read_i16(data + i * 28 + 14);
bsp->leafs[i].maxs[1] = read_i16(data + i * 28 + 16);
bsp->leafs[i].maxs[2] = read_i16(data + i * 28 + 18);
bsp->leafs[i].first_marksurface = read_u16(data + i * 28 + 20);
bsp->leafs[i].num_marksurfaces = read_u16(data + i * 28 + 22);
bsp->leafs[i].ambient_level[0] = data[i * 28 + 24];
bsp->leafs[i].ambient_level[1] = data[i * 28 + 25];
bsp->leafs[i].ambient_level[2] = data[i * 28 + 26];
bsp->leafs[i].ambient_level[3] = data[i * 28 + 27];
bsp->leafs[i].contents = pxl8_read_i32(&stream);
bsp->leafs[i].visofs = pxl8_read_i32(&stream);
for (u32 j = 0; j < 3; j++) bsp->leafs[i].mins[j] = pxl8_read_i16(&stream);
for (u32 j = 0; j < 3; j++) bsp->leafs[i].maxs[j] = pxl8_read_i16(&stream);
bsp->leafs[i].first_marksurface = pxl8_read_u16(&stream);
bsp->leafs[i].num_marksurfaces = pxl8_read_u16(&stream);
for (u32 j = 0; j < 4; j++) bsp->leafs[i].ambient_level[j] = pxl8_read_u8(&stream);
}
}
pxl8_bsp_chunk* marksurfaces_chunk = &header.chunks[CHUNK_MARKSURFACES];
bsp->num_marksurfaces = marksurfaces_chunk->size / 2;
chunk = &header.chunks[CHUNK_MARKSURFACES];
if (!validate_chunk(chunk, 2, file_size)) goto error_cleanup;
bsp->num_marksurfaces = chunk->size / 2;
if (bsp->num_marksurfaces > 0) {
bsp->marksurfaces = (u16*)SDL_calloc(bsp->num_marksurfaces, sizeof(u16));
const u8* data = file_data + marksurfaces_chunk->offset;
bsp->marksurfaces = SDL_calloc(bsp->num_marksurfaces, sizeof(u16));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_marksurfaces; i++) {
bsp->marksurfaces[i] = read_u16(data + i * 2);
bsp->marksurfaces[i] = pxl8_read_u16(&stream);
}
}
pxl8_bsp_chunk* models_chunk = &header.chunks[CHUNK_MODELS];
bsp->num_models = models_chunk->size / 64;
chunk = &header.chunks[CHUNK_MODELS];
if (!validate_chunk(chunk, 64, file_size)) goto error_cleanup;
bsp->num_models = chunk->size / 64;
if (bsp->num_models > 0) {
bsp->models = (pxl8_bsp_model*)SDL_calloc(bsp->num_models, sizeof(pxl8_bsp_model));
const u8* data = file_data + models_chunk->offset;
bsp->models = SDL_calloc(bsp->num_models, sizeof(pxl8_bsp_model));
pxl8_stream_seek(&stream, chunk->offset);
for (u32 i = 0; i < bsp->num_models; i++) {
bsp->models[i].mins[0] = read_f32(data + i * 64);
bsp->models[i].mins[1] = read_f32(data + i * 64 + 4);
bsp->models[i].mins[2] = read_f32(data + i * 64 + 8);
bsp->models[i].maxs[0] = read_f32(data + i * 64 + 12);
bsp->models[i].maxs[1] = read_f32(data + i * 64 + 16);
bsp->models[i].maxs[2] = read_f32(data + i * 64 + 20);
bsp->models[i].origin.x = read_f32(data + i * 64 + 24);
bsp->models[i].origin.y = read_f32(data + i * 64 + 28);
bsp->models[i].origin.z = read_f32(data + i * 64 + 32);
bsp->models[i].headnode[0] = read_i32(data + i * 64 + 36);
bsp->models[i].headnode[1] = read_i32(data + i * 64 + 40);
bsp->models[i].headnode[2] = read_i32(data + i * 64 + 44);
bsp->models[i].headnode[3] = read_i32(data + i * 64 + 48);
bsp->models[i].visleafs = read_i32(data + i * 64 + 52);
bsp->models[i].first_face = read_i32(data + i * 64 + 56);
bsp->models[i].num_faces = read_i32(data + i * 64 + 60);
for (u32 j = 0; j < 3; j++) bsp->models[i].mins[j] = pxl8_read_f32(&stream);
for (u32 j = 0; j < 3; j++) bsp->models[i].maxs[j] = pxl8_read_f32(&stream);
bsp->models[i].origin = read_vec3(&stream);
for (u32 j = 0; j < 4; j++) bsp->models[i].headnode[j] = pxl8_read_i32(&stream);
bsp->models[i].visleafs = pxl8_read_i32(&stream);
bsp->models[i].first_face = pxl8_read_i32(&stream);
bsp->models[i].num_faces = pxl8_read_i32(&stream);
}
}
pxl8_bsp_chunk* vis_chunk = &header.chunks[CHUNK_VISIBILITY];
bsp->visdata_size = vis_chunk->size;
chunk = &header.chunks[CHUNK_VISIBILITY];
if (!validate_chunk(chunk, 1, file_size)) goto error_cleanup;
bsp->visdata_size = chunk->size;
if (bsp->visdata_size > 0) {
bsp->visdata = (u8*)SDL_malloc(bsp->visdata_size);
memcpy(bsp->visdata, file_data + vis_chunk->offset, bsp->visdata_size);
bsp->visdata = SDL_malloc(bsp->visdata_size);
memcpy(bsp->visdata, file_data + chunk->offset, bsp->visdata_size);
}
pxl8_bsp_chunk* light_chunk = &header.chunks[CHUNK_LIGHTING];
bsp->lightdata_size = light_chunk->size;
chunk = &header.chunks[CHUNK_LIGHTING];
if (!validate_chunk(chunk, 1, file_size)) goto error_cleanup;
bsp->lightdata_size = chunk->size;
if (bsp->lightdata_size > 0) {
bsp->lightdata = (u8*)SDL_malloc(bsp->lightdata_size);
memcpy(bsp->lightdata, file_data + light_chunk->offset, bsp->lightdata_size);
bsp->lightdata = SDL_malloc(bsp->lightdata_size);
memcpy(bsp->lightdata, file_data + chunk->offset, bsp->lightdata_size);
}
SDL_free(file_data);
@ -274,6 +255,12 @@ pxl8_result pxl8_bsp_load(const char* path, pxl8_bsp* bsp) {
bsp->num_vertices, bsp->num_faces, bsp->num_nodes, bsp->num_leafs);
return PXL8_OK;
error_cleanup:
pxl8_error("BSP chunk validation failed: %s", path);
SDL_free(file_data);
pxl8_bsp_destroy(bsp);
return PXL8_ERROR_INVALID_FORMAT;
}
void pxl8_bsp_destroy(pxl8_bsp* bsp) {