shithub: nanobsp

Download patch

ref: d7a9dc101661c8077bcdb17cf6534b90ca22dd4f
parent: 8fa8d85c98de9a6a2a675cfd863fcbeabcd80b86
author: Andrew Apted <ajapted@gmail.com>
date: Fri Dec 15 10:43:23 EST 2023

implemented BSP_WriteSubsector() -- write the subsector and its segs.

Also we free stuff along the way.

--- a/nano_bsp.c
+++ b/nano_bsp.c
@@ -746,45 +746,70 @@
 	}
 }
 
-void BSP_WriteSeg (seg_t * in, seg_t * out)
+void BSP_WriteSubsector (nanode_t * N)
 {
-	memcpy (out, in, sizeof(seg_t));
+	subsector_t * out = &subsectors[N->index];
 
-	out->next = NULL;
-}
+	out->numlines  = 0;
+	out->firstline = nano_seg_index;
+	out->sector    = NULL;  // determined in P_GroupLines
 
-unsigned int BSP_WriteSubsector (nanode_t * N)
-{
-	// TODO
+	seg_t * seg;
 
-	return (unsigned int) N->index | NF_SUBSECTOR;
+	while (N->segs != NULL)
+	{
+		seg = N->segs;
+
+		// unlink this seg from the list
+		N->segs = seg->next;
+		seg->next = NULL;
+
+		// copy and free it
+		memcpy (&segs[nano_seg_index], seg, sizeof(seg_t));
+
+		Z_Free (seg);
+
+		nano_seg_index += 1;
+		out->numlines  += 1;
+	}
 }
 
 unsigned int BSP_WriteNode (nanode_t * N)
 {
 	if (N->sector != NULL)
-		return BSP_WriteSubsector (N);
+	{
+		BSP_WriteSubsector (N);
+	}
+	else
+	{
+		node_t * out = &nodes[N->index];
 
-	node_t * out = &nodes[N->index];
+		out->x  = N->x;
+		out->y  = N->y;
+		out->dx = N->dx;
+		out->dy = N->dy;
 
-	out->x  = N->x;
-	out->y  = N->y;
-	out->dx = N->dx;
-	out->dy = N->dy;
+		int c;
+		for (c = 0 ; c < 2 ; c++)
+		{
+			nanode_t * child = (c == 0) ? N->right : N->left;
 
-	int c;
-	for (c = 0 ; c < 2 ; c++)
-	{
-		nanode_t * child = (c == 0) ? N->right : N->left;
+			out->children[c] = BSP_WriteNode (child);
 
-		out->children[c] = BSP_WriteNode (child);
-
-		int k;
-		for (k = 0 ; k < 4 ; k++)
-			out->bbox[c][k] = child->bbox[k];
+			int k;
+			for (k = 0 ; k < 4 ; k++)
+				out->bbox[c][k] = child->bbox[k];
+		}
 	}
 
-	return (unsigned int) N->index;
+	unsigned int index = N->index;
+
+	if (N->sector != NULL)
+		index |= NF_SUBSECTOR;
+
+	Z_Free (N);
+
+	return index;
 }
 
 void BSP_BuildNodes (void)
@@ -810,5 +835,6 @@
 
 	nano_seg_index = 0;
 
+	// this also frees stuff as it goes
 	BSP_WriteNode (root);
 }
--