diff --git a/include/qbsp/brush.hh b/include/qbsp/brush.hh index ad0d58dc..ae24fca8 100644 --- a/include/qbsp/brush.hh +++ b/include/qbsp/brush.hh @@ -28,7 +28,7 @@ struct brush_t { brush_t *next; aabb3d bounds; - face_t *faces; + std::vector faces; contentflags_t contents; /* BSP contents */ short lmshift; /* lightmap scaling (qu/lightmap pixel), passed to the light util */ }; diff --git a/qbsp/brush.cc b/qbsp/brush.cc index 426cbd15..34f7e7ef 100644 --- a/qbsp/brush.cc +++ b/qbsp/brush.cc @@ -297,14 +297,13 @@ static bool DiscardHintSkipFace_Q2(const mtexinfo_t &texinfo) CreateBrushFaces ================= */ -static face_t *CreateBrushFaces(const mapentity_t *src, hullbrush_t *hullbrush, const int hullnum, - const rotation_t rottype = rotation_t::none, const qvec3d &rotate_offset = {}) +static std::vector CreateBrushFaces(const mapentity_t *src, hullbrush_t *hullbrush, const int hullnum, + const rotation_t rottype = rotation_t::none, const qvec3d &rotate_offset = {}) { vec_t r; - face_t *f; std::optional w; qbsp_plane_t plane; - face_t *facelist = NULL; + std::vector facelist; qvec3d point; vec_t max, min; @@ -342,31 +341,31 @@ static face_t *CreateBrushFaces(const mapentity_t *src, hullbrush_t *hullbrush, if (!w) continue; // overconstrained plane - // this face is a keeper - f = new face_t{}; - f->planenum = PLANENUM_LEAF; - if (w->size() > MAXEDGES) FError("face->numpoints > MAXEDGES ({}), source face on line {}", MAXEDGES, mapface.linenum); + + // this face is a keeper + face_t &f = facelist.emplace_back(); + f.planenum = PLANENUM_LEAF; - f->w.resize(w->size()); + f.w.resize(w->size()); for (size_t j = 0; j < w->size(); j++) { for (size_t k = 0; k < 3; k++) { point[k] = w->at(j)[k] - rotate_offset[k]; r = Q_rint(point[k]); if (fabs(point[k] - r) < ZERO_EPSILON) - f->w[j][k] = r; + f.w[j][k] = r; else - f->w[j][k] = point[k]; + f.w[j][k] = point[k]; - if (f->w[j][k] < min) - min = f->w[j][k]; - if (f->w[j][k] > max) - max = f->w[j][k]; + if (f.w[j][k] < min) + min = f.w[j][k]; + if (f.w[j][k] > max) + max = f.w[j][k]; } - hullbrush->bounds += f->w[j]; + hullbrush->bounds += f.w[j]; } // account for texture offset, from txqbsp-xt @@ -385,14 +384,12 @@ static face_t *CreateBrushFaces(const mapentity_t *src, hullbrush_t *hullbrush, point -= rotate_offset; plane.dist = qv::dot(plane.normal, point); - f->texinfo = hullnum > 0 ? 0 : mapface.texinfo; - f->planenum = FindPlane(plane, &f->planeside); - f->src_entity = const_cast(src); // FIXME: get rid of consts on src in the callers? + f.texinfo = hullnum > 0 ? 0 : mapface.texinfo; + f.planenum = FindPlane(plane, &f.planeside); + f.src_entity = const_cast(src); // FIXME: get rid of consts on src in the callers? - f->next = facelist; - facelist = f; - CheckFace(f, mapface); - UpdateFaceSphere(f); + CheckFace(&f, mapface); + UpdateFaceSphere(&f); } // Rotatable objects must have a bounding box big enough to @@ -458,7 +455,6 @@ FreeBrush */ void FreeBrush(brush_t *brush) { - FreeBrushFaces(brush->faces); delete brush; } @@ -640,10 +636,9 @@ static void AddHullEdge(hullbrush_t *hullbrush, const qvec3d &p1, const qvec3d & ExpandBrush ============= */ -static void ExpandBrush(hullbrush_t *hullbrush, const aabb3d &hull_size, const face_t *facelist) +static void ExpandBrush(hullbrush_t *hullbrush, const aabb3d &hull_size, std::vector &facelist) { int x, s; - const face_t *f; qbsp_plane_t plane; int cBevEdge = 0; @@ -652,9 +647,9 @@ static void ExpandBrush(hullbrush_t *hullbrush, const aabb3d &hull_size, const f hullbrush->edges.clear(); // create all the hull points - for (f = facelist; f; f = f->next) - for (size_t i = 0; i < f->w.size(); i++) { - AddHullPoint(hullbrush, f->w[i], hull_size); + for (auto &f : facelist) + for (size_t i = 0; i < f.w.size(); i++) { + AddHullPoint(hullbrush, f.w[i], hull_size); cBevEdge++; } @@ -686,9 +681,9 @@ static void ExpandBrush(hullbrush_t *hullbrush, const aabb3d &hull_size, const f } // add all of the edge bevels - for (f = facelist; f; f = f->next) - for (size_t i = 0; i < f->w.size(); i++) - AddHullEdge(hullbrush, f->w[i], f->w[(i + 1) % f->w.size()], hull_size); + for (auto &f : facelist) + for (size_t i = 0; i < f.w.size(); i++) + AddHullEdge(hullbrush, f.w[i], f.w[(i + 1) % f.w.size()], hull_size); } //============================================================================ @@ -755,7 +750,7 @@ brush_t *LoadBrush(const mapentity_t *src, const mapbrush_t *mapbrush, const con { hullbrush_t hullbrush; brush_t *brush; - face_t *facelist; + std::vector facelist; // create the faces @@ -778,7 +773,7 @@ brush_t *LoadBrush(const mapentity_t *src, const mapbrush_t *mapbrush, const con facelist = CreateBrushFaces(src, &hullbrush, hullnum); } - if (!facelist) { + if (facelist.empty()) { LogPrint("WARNING: Couldn't create brush faces\n"); LogPrint("^ brush at line {} of .map file\n", hullbrush.linenum); return NULL; @@ -788,7 +783,6 @@ brush_t *LoadBrush(const mapentity_t *src, const mapbrush_t *mapbrush, const con auto &hulls = options.target_game->get_hull_sizes(); Q_assert(hullnum < hulls.size()); ExpandBrush(&hullbrush, *(hulls.begin() + hullnum), facelist); - FreeBrushFaces(facelist); facelist = CreateBrushFaces(src, &hullbrush, hullnum, rottype, rotate_offset); } @@ -833,17 +827,9 @@ int Brush_ListCount(const brush_t *brush) return Brush_ListCountWithCFlags(brush, 0); } -static int FaceListCount(const face_t *facelist) -{ - if (facelist) - return 1 + FaceListCount(facelist->next); - else - return 0; -} - int Brush_NumFaces(const brush_t *brush) { - return FaceListCount(brush->faces); + return brush->faces.size(); } void Entity_SortBrushes(mapentity_t *dst) diff --git a/qbsp/csg4.cc b/qbsp/csg4.cc index 3dc97e47..2577f1ec 100644 --- a/qbsp/csg4.cc +++ b/qbsp/csg4.cc @@ -208,9 +208,9 @@ static void RemoveOutsideFaces(const brush_t *brush, face_t **inside, face_t **o while (face) { next = face->next; std::optional w = face->w; - for (const face_t *clipface = brush->faces; clipface; clipface = clipface->next) { - qbsp_plane_t clipplane = map.planes[clipface->planenum]; - if (!clipface->planeside) { + for (auto &clipface : brush->faces) { + qbsp_plane_t clipplane = map.planes[clipface.planenum]; + if (!clipface.planeside) { clipplane = -clipplane; } w = w->clip(clipplane, ON_EPSILON, true)[SIDE_FRONT]; @@ -500,12 +500,12 @@ CopyBrushFaces */ static face_t *CopyBrushFaces(const brush_t *brush) { - face_t *facelist, *face, *newface; + face_t *facelist, *newface; facelist = NULL; - for (face = brush->faces; face; face = face->next) { + for (auto &face : brush->faces) { brushfaces++; - newface = new face_t(*face); + newface = new face_t(face); newface->contents[0] = options.target_game->create_empty_contents(); newface->contents[1] = brush->contents; newface->lmshift[0] = brush->lmshift; @@ -614,9 +614,8 @@ surface_t *CSGFaces(const mapentity_t *entity) outside = NULL; RemoveOutsideFaces(clipbrush, &inside, &outside); - const face_t *clipface = clipbrush->faces; - for (; clipface; clipface = clipface->next) - ClipInside(clipface, overwrite, &inside, &outside); + for (auto &clipface : clipbrush->faces) + ClipInside(&clipface, overwrite, &inside, &outside); // inside = parts of `brush` that are inside `clipbrush` // outside = parts of `brush` that are outside `clipbrush` diff --git a/qbsp/exportobj.cc b/qbsp/exportobj.cc index 30b39fc4..456f76bc 100644 --- a/qbsp/exportobj.cc +++ b/qbsp/exportobj.cc @@ -121,8 +121,8 @@ void ExportObj_Brushes(const std::string &filesuffix, const std::vector faces; for (const brush_t *brush : brushes) - for (const face_t *face = brush->faces; face; face = face->next) - faces.push_back(face); + for (auto &face : brush->faces) + faces.push_back(&face); ExportObj_Faces(filesuffix, faces); } diff --git a/qbsp/map.cc b/qbsp/map.cc index c150042f..11edcc4d 100644 --- a/qbsp/map.cc +++ b/qbsp/map.cc @@ -2199,11 +2199,11 @@ void WriteBspBrushMap(const std::filesystem::path &name, const std::vectorfaces; face; face = face->next) { + for (auto &face : brush->faces) { // FIXME: Factor out this mess - qbsp_plane_t plane = map.planes.at(face->planenum); + qbsp_plane_t plane = map.planes.at(face.planenum); - if (face->planeside) { + if (face.planeside) { plane = -plane; } diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 7bbe5459..28f551eb 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -96,20 +96,20 @@ Adds any additional planes necessary to allow the brush to be expanded against axial bounding boxes ================= */ -static std::vector> AddBrushBevels(const brush_t *b) +static std::vector> AddBrushBevels(const brush_t *b) { // add already-present planes - std::vector> planes; + std::vector> planes; - for (face_t *f = b->faces; f; f = f->next) { - int32_t planenum = f->planenum; + for (auto &f : b->faces) { + int32_t planenum = f.planenum; - if (f->planeside) { - planenum = FindPlane(-map.planes[f->planenum], nullptr); + if (f.planeside) { + planenum = FindPlane(-map.planes[f.planenum], nullptr); } int32_t outputplanenum = ExportMapPlane(planenum); - planes.emplace_back(outputplanenum, f); + planes.emplace_back(outputplanenum, &f); } // @@ -136,7 +136,7 @@ static std::vector> AddBrushBevels(const brush_t *b int32_t planenum = FindPlane(new_plane, nullptr); int32_t outputplanenum = ExportMapPlane(planenum); - planes.emplace_back(outputplanenum, b->faces); + planes.emplace_back(outputplanenum, &b->faces.front()); } // if the plane is not in it canonical order, swap it @@ -184,19 +184,20 @@ static std::vector> AddBrushBevels(const brush_t *b continue; current.dist = qv::dot(w[j], current.normal); - face_t *f; + auto it = b->faces.begin(); // if all the points on all the sides are // behind this plane, it is a proper edge bevel - for (f = b->faces; f; f = f->next) { - auto &plane = map.planes[f->planenum]; - qplane3d temp = f->planeside ? -plane : plane; + for (; it != b->faces.end(); it++) { + auto &f = *it; + auto &plane = map.planes[f.planenum]; + qplane3d temp = f.planeside ? -plane : plane; // if this plane has allready been used, skip it if (qv::epsilonEqual(current, temp)) break; - auto &w2 = f->w; + auto &w2 = f.w; if (!w2.size()) continue; size_t l; @@ -209,13 +210,13 @@ static std::vector> AddBrushBevels(const brush_t *b break; } - if (f) + if (it == b->faces.end()) continue; // wasn't part of the outer hull // add this plane int32_t planenum = FindPlane(current, nullptr); int32_t outputplanenum = ExportMapPlane(planenum); - planes.emplace_back(outputplanenum, b->faces); + planes.emplace_back(outputplanenum, &b->faces.front()); } } } @@ -789,10 +790,10 @@ void BSPX_Brushes_AddModel(struct bspxbrushes_s *ctx, int modelnum, brush_t *bru for (brush_t *b = brushes; b; b = b->next) { permodel.numbrushes++; - for (face_t *f = b->faces; f; f = f->next) { + for (auto &f : b->faces) { /*skip axial*/ - if (fabs(map.planes[f->planenum].normal[0]) == 1 || fabs(map.planes[f->planenum].normal[1]) == 1 || - fabs(map.planes[f->planenum].normal[2]) == 1) + if (fabs(map.planes[f.planenum].normal[0]) == 1 || fabs(map.planes[f.planenum].normal[1]) == 1 || + fabs(map.planes[f.planenum].normal[2]) == 1) continue; permodel.numfaces++; } @@ -810,10 +811,10 @@ void BSPX_Brushes_AddModel(struct bspxbrushes_s *ctx, int modelnum, brush_t *bru for (brush_t *b = brushes; b; b = b->next) { bspxbrushes_perbrush perbrush {}; - for (face_t *f = b->faces; f; f = f->next) { + for (auto &f : b->faces) { /*skip axial*/ - if (fabs(map.planes[f->planenum].normal[0]) == 1 || fabs(map.planes[f->planenum].normal[1]) == 1 || - fabs(map.planes[f->planenum].normal[2]) == 1) + if (fabs(map.planes[f.planenum].normal[0]) == 1 || fabs(map.planes[f.planenum].normal[1]) == 1 || + fabs(map.planes[f.planenum].normal[2]) == 1) continue; perbrush.numfaces++; } @@ -851,18 +852,18 @@ void BSPX_Brushes_AddModel(struct bspxbrushes_s *ctx, int modelnum, brush_t *bru str <= perbrush; - for (face_t *f = b->faces; f; f = f->next) { + for (auto &f : b->faces) { /*skip axial*/ - if (fabs(map.planes[f->planenum].normal[0]) == 1 || fabs(map.planes[f->planenum].normal[1]) == 1 || - fabs(map.planes[f->planenum].normal[2]) == 1) + if (fabs(map.planes[f.planenum].normal[0]) == 1 || fabs(map.planes[f.planenum].normal[1]) == 1 || + fabs(map.planes[f.planenum].normal[2]) == 1) continue; bspxbrushes_perface perface; - if (f->planeside) { - perface = -map.planes[f->planenum]; + if (f.planeside) { + perface = -map.planes[f.planenum]; } else { - perface = map.planes[f->planenum]; + perface = map.planes[f.planenum]; } str <= std::tie(perface.normal, perface.dist); diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index 8a129e41..1e58dd5d 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -106,8 +106,6 @@ ExportClipNodes */ static size_t ExportClipNodes(mapentity_t *entity, node_t *node) { - face_t *face, *next; - if (node->planenum == PLANENUM_LEAF) { return node->contents.native; }