diff --git a/include/world.h b/include/world.h index 93c562fd0..2e56655af 100644 --- a/include/world.h +++ b/include/world.h @@ -140,5 +140,6 @@ typedef struct nodeleaf_s { } nodeleaf_t; nodeleaf_t *MOD_BuildBrushes (hull_t *hull); +void MOD_FreeBrushes (hull_t *hull); #endif // __world_h diff --git a/libs/models/portal.c b/libs/models/portal.c index d7170b0d1..661827f18 100644 --- a/libs/models/portal.c +++ b/libs/models/portal.c @@ -47,18 +47,20 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$"; #include "world.h" -static clipleaf_t * -alloc_leaf (void) -{ - return calloc (1, sizeof (clipleaf_t)); -} - static clipport_t * alloc_portal (void) { return calloc (1, sizeof (clipport_t)); } +static void +free_portal (clipport_t *portal) +{ + FreeWinding (portal->winding); + FreeWinding (portal->edges); + free (portal); +} + static void remove_portal (clipport_t *portal, clipleaf_t *leaf) { @@ -75,6 +77,27 @@ remove_portal (clipport_t *portal, clipleaf_t *leaf) } } +static clipleaf_t * +alloc_leaf (void) +{ + return calloc (1, sizeof (clipleaf_t)); +} + +static void +free_leaf (clipleaf_t *leaf) +{ + if (!leaf) + return; + while (leaf->portals) { + clipport_t *portal = leaf->portals; + int side = portal->leafs[1] == leaf; + leaf->portals = portal->next[side]; + remove_portal (portal, portal->leafs[side ^ 1]); + free_portal (portal); + } + free (leaf); +} + static void add_portal (clipport_t *portal, clipleaf_t *front, clipleaf_t *back) { @@ -195,3 +218,20 @@ MOD_BuildBrushes (hull_t *hull) } return nodeleafs; } + +void +MOD_FreeBrushes (hull_t *hull) +{ + int i, j; + int numnodes; + + if (!hull || !hull->nodeleafs) + return; + numnodes = hull->lastclipnode + 1; + for (i = 0; i < numnodes; i++) { + for (j = 0; j < 2; j++) + free_leaf (hull->nodeleafs[i].leafs[j]); + } + free (hull->nodeleafs); + hull->nodeleafs = 0; +}