From ba83d78aa8cbeb9fb1109c8d5e2d8c4d6bc0bd49 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 30 Jan 2023 14:08:41 +0100 Subject: [PATCH] Removed the need for global variables for sorting leafs and faces --- world.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/world.c b/world.c index c84c627..b24632e 100644 --- a/world.c +++ b/world.c @@ -214,10 +214,7 @@ static void world_drawface_textured(const world_t *world, const ps1bsp_face_t *f static void (*world_drawface)(const world_t*, const ps1bsp_face_t*, u_long *ot) = &world_drawface_fast; -static ps1bsp_leaf_t *firstLeaf = NULL; -static ps1bsp_face_t *firstFace = NULL; - -static void world_drawFaces(const world_t *world) +static void world_drawFaces(const world_t *world, const ps1bsp_face_t *firstFace) { // Draw the faces in front-to-back order. There are two advantages to this: // 1) We don't need to do any depth calculations. The ordering table will be rendered in reverse order, i.e. back-to-front. @@ -228,8 +225,9 @@ static void world_drawFaces(const world_t *world) } } -static void world_sortFaces(const world_t *world) +static ps1bsp_face_t *world_sortFaces(const world_t *world, const ps1bsp_leaf_t *firstLeaf) { + ps1bsp_face_t *firstFace = NULL; u_long frameNum = time_getFrameNumber(); // Traverse leaves in back-to-front order @@ -251,9 +249,11 @@ static void world_sortFaces(const world_t *world) firstFace = face; } } + + return firstFace; } -static void world_sortLeafs(const world_t *world, short nodeIdx, u_char *pvs) +static void world_sortLeafs(const world_t *world, short nodeIdx, u_char *pvs, ps1bsp_leaf_t **firstLeaf) { if (nodeIdx < 0) // Leaf node { @@ -269,8 +269,8 @@ static void world_sortLeafs(const world_t *world, short nodeIdx, u_char *pvs) // Add the leaf to the sorted linked list // Since we're traversing the BSP tree front-to-back, adding each leaf at the start sorts the list in back-to-front order ps1bsp_leaf_t *leaf = (ps1bsp_leaf_t*)&world->leaves[leafIdx]; - leaf->nextLeaf = firstLeaf; - firstLeaf = leaf; + leaf->nextLeaf = *firstLeaf; + *firstLeaf = leaf; return; } @@ -282,10 +282,10 @@ static void world_sortLeafs(const world_t *world, short nodeIdx, u_char *pvs) const ps1bsp_plane_t *plane = &world->planes[node->planeId]; short dist = world_pointPlaneDist(&cam_pos, plane); - // Sort leafs in front-to-back order, so that we can draw their faces at the correct depths + // Traverse the BSP tree in front-to-back order char order = dist < 0; - world_sortLeafs(world, node->children[order], pvs); - world_sortLeafs(world, node->children[order ^ 1], pvs); + world_sortLeafs(world, node->children[order], pvs, firstLeaf); + world_sortLeafs(world, node->children[order ^ 1], pvs, firstLeaf); } // Decompress PVS data for the given leaf ID and store it in RAM at the given buffer pointer location. @@ -366,10 +366,8 @@ void world_draw(const world_t *world) else world_drawface = &world_drawface_lit; - firstLeaf = NULL; - firstFace = NULL; - - world_sortLeafs(world, 0, pvs); - world_sortFaces(world); - world_drawFaces(world); + ps1bsp_leaf_t *firstLeaf = NULL; + world_sortLeafs(world, 0, pvs, &firstLeaf); + ps1bsp_face_t *firstFace = world_sortFaces(world, firstLeaf); + world_drawFaces(world, firstFace); }