From 23c8fe5833ec31d230dff2f7baa26bcaed410038 Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 6 Apr 2012 15:47:56 +0100 Subject: [PATCH] BSP Builder|Refactor: Partitioner now allocates additional Vertexes from its own store Partitioner has owership of these additional vertexes and will delete them automatically when Partitioner itself is destroyed. Todo for later: Allow the caller to acquire ownership of the vertexes. --- .../portable/include/map/bsp/partitioner.h | 12 +++- .../engine/portable/include/map/bspbuilder.h | 4 ++ doomsday/engine/portable/src/edit_bsp.cpp | 56 +++++++++++++------ doomsday/engine/portable/src/edit_map.c | 2 +- .../portable/src/map/bsp/partitioner.cpp | 33 +++++++++-- .../engine/portable/src/map/bspbuilder.cpp | 10 ++++ 6 files changed, 94 insertions(+), 23 deletions(-) diff --git a/doomsday/engine/portable/include/map/bsp/partitioner.h b/doomsday/engine/portable/include/map/bsp/partitioner.h index dfc19cb5d4..2bdbb3c510 100644 --- a/doomsday/engine/portable/include/map/bsp/partitioner.h +++ b/doomsday/engine/portable/include/map/bsp/partitioner.h @@ -127,6 +127,10 @@ class Partitioner uint numLeafs(); + uint numVertexes(); + + Vertex const& vertex(uint idx); + private: void initForMap(); @@ -393,14 +397,18 @@ class Partitioner uint* numEditableVertexes; Vertex*** editableVertexes; - /// Extra info about LineDefs in the current map. + /// Extended info about LineDefs in the current map. typedef std::vector LineDefInfos; LineDefInfos lineDefInfos; - /// Extra info about Vertexes in the current map. + /// Extended info about Vertexes in the current map (including extras). typedef std::vector VertexInfos; VertexInfos vertexInfos; + /// Extra vertexes allocated for the current map. + typedef std::vector Vertexes; + Vertexes vertexes; + /// Root node of our internal binary tree around which the final BSP data /// objects are constructed. BspTreeNode* rootNode; diff --git a/doomsday/engine/portable/include/map/bspbuilder.h b/doomsday/engine/portable/include/map/bspbuilder.h index e5ffb9044e..546add5e31 100644 --- a/doomsday/engine/portable/include/map/bspbuilder.h +++ b/doomsday/engine/portable/include/map/bspbuilder.h @@ -83,6 +83,10 @@ class BspBuilder uint numLeafs(); + uint numVertexes(); + + Vertex const& vertex(uint idx); + private: bsp::Partitioner* partitioner; }; diff --git a/doomsday/engine/portable/src/edit_bsp.cpp b/doomsday/engine/portable/src/edit_bsp.cpp index 81e605a52e..1398a398f6 100644 --- a/doomsday/engine/portable/src/edit_bsp.cpp +++ b/doomsday/engine/portable/src/edit_bsp.cpp @@ -240,22 +240,46 @@ static void hardenBSP(BspBuilder& builder, GameMap* dest) BspTreeNode::PostOrder(*rootNode, populateBspObjectLuts, &p); } -static void hardenVertexes(GameMap* dest, Vertex*** vertexes, uint* numVertexes) +static void copyVertex(Vertex& vtx, Vertex const& other) { - dest->numVertexes = *numVertexes; - dest->vertexes = static_cast(Z_Calloc(dest->numVertexes * sizeof(Vertex), PU_MAPSTATIC, 0)); + vtx.numLineOwners = other.numLineOwners; + vtx.lineOwners = other.lineOwners; - for(uint i = 0; i < dest->numVertexes; ++i) + vtx.buildData.index = other.buildData.index; + vtx.buildData.refCount = other.buildData.refCount; + vtx.buildData.equiv = other.buildData.equiv; + V2d_Copy(vtx.buildData.pos, other.buildData.pos); + + // Apply the final coordinates. + vtx.V_pos[VX] = float(vtx.buildData.pos[VX]); + vtx.V_pos[VY] = float(vtx.buildData.pos[VY]); +} + +static void hardenVertexes(BspBuilder& builder, GameMap* map, + uint* numEditableVertexes, Vertex*** editableVertexes) +{ + uint bspVertexCount = builder.numVertexes(); + + map->numVertexes = *numEditableVertexes + bspVertexCount; + map->vertexes = static_cast(Z_Calloc(map->numVertexes * sizeof(Vertex), PU_MAPSTATIC, 0)); + + uint n = 0; + for(; n < *numEditableVertexes; ++n) { - Vertex* destV = &dest->vertexes[i]; - Vertex* srcV = (*vertexes)[i]; + Vertex& dest = map->vertexes[n]; + Vertex const& src = *((*editableVertexes)[n]); - destV->header.type = DMU_VERTEX; - destV->numLineOwners = srcV->numLineOwners; - destV->lineOwners = srcV->lineOwners; + dest.header.type = DMU_VERTEX; + copyVertex(dest, src); + } - destV->V_pos[VX] = float(srcV->buildData.pos[VX]); - destV->V_pos[VY] = float(srcV->buildData.pos[VY]); + for(uint i = 0; i < bspVertexCount; ++i, ++n) + { + Vertex& dest = map->vertexes[n]; + Vertex const& src = builder.vertex(i); + + dest.header.type = DMU_VERTEX; + copyVertex(dest, src); } } @@ -278,19 +302,19 @@ static void updateVertexLinks(GameMap* map) } } -void MPE_SaveBsp(BspBuilder_c* builder_c, GameMap* map, uint* numVertexes, Vertex*** vertexes) +void MPE_SaveBsp(BspBuilder_c* builder_c, GameMap* map, uint* numEditableVertexes, Vertex*** editableVertexes) { Q_ASSERT(builder_c); - BspBuilder* builder = builder_c->inst; + BspBuilder& builder = *builder_c->inst; - BspTreeNode* rootNode = builder->root(); + BspTreeNode* rootNode = builder.root(); buildHEdgeLut(map, rootNode); - hardenVertexes(map, vertexes, numVertexes); + hardenVertexes(builder, map, numEditableVertexes, editableVertexes); updateVertexLinks(map); finishHEdges(map); - hardenBSP(*builder, map); + hardenBSP(builder, map); long rHeight = 0, lHeight = 0; if(rootNode && !rootNode->isLeaf()) diff --git a/doomsday/engine/portable/src/edit_map.c b/doomsday/engine/portable/src/edit_map.c index 1da4d2cdd9..a4037046ca 100644 --- a/doomsday/engine/portable/src/edit_map.c +++ b/doomsday/engine/portable/src/edit_map.c @@ -55,7 +55,7 @@ static usecrecord_t *unclosedSectors; static Vertex *rootVtx; // Used when sorting vertex line owners. -Vertex* createVertex(void) +static Vertex* createVertex(void) { Vertex* vtx; diff --git a/doomsday/engine/portable/src/map/bsp/partitioner.cpp b/doomsday/engine/portable/src/map/bsp/partitioner.cpp index 7cf2f06029..49fd377234 100644 --- a/doomsday/engine/portable/src/map/bsp/partitioner.cpp +++ b/doomsday/engine/portable/src/map/bsp/partitioner.cpp @@ -36,7 +36,6 @@ #include "de_base.h" #include "de_console.h" #include "de_play.h" -#include "edit_map.h" #include "p_mapdata.h" #include "m_misc.h" @@ -122,6 +121,16 @@ Partitioner::~Partitioner() deleteHEdgeTips((*editableVertexes)[i]); } + for(Vertexes::iterator it = vertexes.begin(); it != vertexes.end(); ++it) + { + Vertex* vtx = *it; + // Has ownership of this vertex been claimed? + if(!vtx) continue; + + deleteHEdgeTips(vtx); + free(vtx); + } + // We are finished with the BSP data. if(rootNode) { @@ -1587,11 +1596,27 @@ void Partitioner::addMiniHEdges(SuperBlock& rightList, SuperBlock& leftList) buildHEdgesAtIntersectionGaps(rightList, leftList); } +uint Partitioner::numVertexes() +{ + return vertexes.size(); +} + +Vertex const& Partitioner::vertex(uint idx) +{ + Q_ASSERT(idx < vertexes.size()); + return *vertexes[idx]; +} + Vertex* Partitioner::newVertex(const_pvec2d_t point) { - /// @todo Vertex should not come from the editable map but from a store - /// within our own domain. - Vertex* vtx = createVertex(); + Vertex* vtx; + + // Allocate with calloc for uniformity with the editable vertexes. + vtx = static_cast(calloc(1, sizeof *vtx)); + vtx->header.type = DMU_VERTEX; + vtx->buildData.index = *numEditableVertexes + uint(vertexes.size() + 1); // 1-based index, 0 = NIL. + vertexes.push_back(vtx); + vertexInfos.push_back(VertexInfo()); if(point) { diff --git a/doomsday/engine/portable/src/map/bspbuilder.cpp b/doomsday/engine/portable/src/map/bspbuilder.cpp index 67a93ae9a7..e7c579c312 100644 --- a/doomsday/engine/portable/src/map/bspbuilder.cpp +++ b/doomsday/engine/portable/src/map/bspbuilder.cpp @@ -59,3 +59,13 @@ uint BspBuilder::numLeafs() { return partitioner->numLeafs(); } + +uint BspBuilder::numVertexes() +{ + return partitioner->numVertexes(); +} + +Vertex const& BspBuilder::vertex(uint idx) +{ + return partitioner->vertex(idx); +}