From 460428d099105505aec2964ec7593f8d8add037c Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 22 Aug 2021 19:57:50 -0600 Subject: [PATCH] qbsp: refactor texinfo lump writing --- include/qbsp/map.hh | 3 +++ include/qbsp/writebsp.hh | 1 - qbsp/bspfile.cc | 32 +++++++++++++++++++++++++-- qbsp/qbsp.cc | 2 -- qbsp/writebsp.cc | 48 +++++++--------------------------------- 5 files changed, 41 insertions(+), 45 deletions(-) diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index 1f17ebd3..c3b02bb0 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -153,6 +153,9 @@ typedef struct mapdata_s { /* Misc other global state for the compile process */ bool leakfile; /* Flag once we've written a leak (.por/.pts) file */ + // Final, exported data + std::vector exported_texinfos; // FIXME: change to gtexinfo_t + // helpers std::string texinfoTextureName(int texinfo) const { int mt = mtexinfos.at(texinfo).miptex; diff --git a/include/qbsp/writebsp.hh b/include/qbsp/writebsp.hh index 2f46eb2e..d964837d 100644 --- a/include/qbsp/writebsp.hh +++ b/include/qbsp/writebsp.hh @@ -26,7 +26,6 @@ int ExportMapPlane(int planenum); int ExportMapTexinfo(int texinfonum); void AllocBSPPlanes(); -void AllocBSPTexinfo(); void BeginBSPFile(void); void FinishBSPFile(void); diff --git a/qbsp/bspfile.cc b/qbsp/bspfile.cc index 23a7d3bb..4d53be6d 100644 --- a/qbsp/bspfile.cc +++ b/qbsp/bspfile.cc @@ -191,6 +191,34 @@ AddLump(FILE *f, int Type) } } +// TODO: remove this once we switch to common +static void +AddLumpFromBuffer(FILE *f, int Type, void* src, size_t srcbytes) +{ + lump_t *lump; + size_t ret; + const mapentity_t *entity; + + lump = &header->lumps[Type]; + lump->fileofs = ftell(f); + + if (srcbytes) { + ret = fwrite(src, 1, srcbytes, f); + if (ret != srcbytes) + Error("Failure writing to file"); + } + + lump->filelen = srcbytes; + + // Pad to 4-byte boundary + if (srcbytes % 4 != 0) { + size_t pad = 4 - (srcbytes % 4); + ret = fwrite(" ", 1, pad, f); + if (ret != pad) + Error("Failure writing to file"); + } +} + static void GenLump(const char *bspxlump, int Type, size_t sz) { @@ -269,7 +297,7 @@ WriteBSPFile(void) AddLump(f, LUMP_LEAFS); AddLump(f, LUMP_VERTEXES); AddLump(f, LUMP_NODES); - AddLump(f, LUMP_TEXINFO); + AddLumpFromBuffer(f, LUMP_TEXINFO, map.exported_texinfos.data(), map.exported_texinfos.size() * sizeof(map.exported_texinfos[0])); AddLump(f, LUMP_FACES); AddLump(f, LUMP_CLIPNODES); AddLump(f, LUMP_MARKSURFACES); @@ -356,7 +384,7 @@ PrintBSPFileSizes(void) Message(msgStat, "%8d planes %10d", map.cTotal[LUMP_PLANES], map.cTotal[LUMP_PLANES] * MemSize[BSP_PLANE]); Message(msgStat, "%8d vertexes %10d", map.cTotal[LUMP_VERTEXES], map.cTotal[LUMP_VERTEXES] * MemSize[BSP_VERTEX]); Message(msgStat, "%8d nodes %10d", map.cTotal[LUMP_NODES], map.cTotal[LUMP_NODES] * MemSize[BSP_NODE]); - Message(msgStat, "%8d texinfo %10d", map.cTotal[LUMP_TEXINFO], map.cTotal[LUMP_TEXINFO] * MemSize[BSP_TEXINFO]); + Message(msgStat, "%8d texinfo %10d", static_cast(map.exported_texinfos.size()), static_cast(map.exported_texinfos.size()) * MemSize[BSP_TEXINFO]); Message(msgStat, "%8d faces %10d", map.cTotal[LUMP_FACES], map.cTotal[LUMP_FACES] * MemSize[BSP_FACE]); Message(msgStat, "%8d clipnodes %10d", map.cTotal[LUMP_CLIPNODES], map.cTotal[LUMP_CLIPNODES] * MemSize[BSP_CLIPNODE]); Message(msgStat, "%8d leafs %10d", map.cTotal[LUMP_LEAFS], map.cTotal[LUMP_LEAFS] * MemSize[BSP_LEAF]); diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 33abd4cd..046dfdea 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -179,7 +179,6 @@ ProcessEntity(mapentity_t *entity, const int hullnum) } } AllocBSPPlanes(); - AllocBSPTexinfo(); ExportClipNodes(entity, nodes, hullnum); } else { /* @@ -235,7 +234,6 @@ ProcessEntity(mapentity_t *entity, const int hullnum) DetailToSolid(nodes); AllocBSPPlanes(); - AllocBSPTexinfo(); if (options.fObjExport && entity == pWorldEnt()) { ExportObj_Nodes("pre_makefaceedges_plane_faces", nodes); diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index 2ec664e3..51bff209 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -102,14 +102,13 @@ ExportMapTexinfo(int texinfonum) if (src->outputnum != -1) return src->outputnum; - struct lumpdata *texinfo = &pWorldEnt()->lumps[LUMP_TEXINFO]; + // this will be the index of the exported texinfo in the BSP lump + const int i = static_cast(map.exported_texinfos.size()); - if (texinfo->index >= texinfo->count) - Error("Internal error: texinfo count mismatch (%s)", __func__); - - const int i = texinfo->index; - - texinfo_t *dest = &(static_cast(texinfo->data)[i]); + map.exported_texinfos.push_back({}); + texinfo_t* dest = &map.exported_texinfos.back(); + memset(dest, 0, sizeof(dest)); + dest->flags = static_cast(src->flags & TEX_SPECIAL); dest->miptex = src->miptex; for (int j=0; j<2; j++) { @@ -117,12 +116,7 @@ ExportMapTexinfo(int texinfonum) dest->vecs[j][k] = src->vecs[j][k]; } } - - texinfo->index++; - map.cTotal[LUMP_TEXINFO]++; - - Q_assert(texinfo->index == map.cTotal[LUMP_TEXINFO]); - + src->outputnum = i; return i; } @@ -150,29 +144,6 @@ AllocBSPPlanes() } } -/* -================== -AllocBSPTexinfo -================== -*/ -void -AllocBSPTexinfo() -{ - struct lumpdata *texinfo = &pWorldEnt()->lumps[LUMP_TEXINFO]; - - // OK just need one plane array, stick it in worldmodel - if (map.numtexinfo() > texinfo->count) { - int newcount = map.numtexinfo(); - struct lumpdata *newtexinfo = (struct lumpdata *)AllocMem(BSP_TEXINFO, newcount, true); - - memcpy(newtexinfo, texinfo->data, MemSize[BSP_TEXINFO] * texinfo->count); - FreeMem(texinfo->data, BSP_TEXINFO, texinfo->count); - - texinfo->count = newcount; - texinfo->data = newtexinfo; - } -} - //=========================================================================== @@ -835,7 +806,7 @@ WriteExtendedTexinfoFlags(void) fprintf(texinfofile, "%llu\n", static_cast(tx.flags)); count++; } - Q_assert(count == map.cTotal[LUMP_TEXINFO]); + Q_assert(count == static_cast(map.exported_texinfos.size())); fclose(texinfofile); } @@ -854,9 +825,6 @@ FinishBSPFile(void) // TODO: Fix this somewhere else? struct lumpdata *planes = &pWorldEnt()->lumps[LUMP_PLANES]; planes->count = map.cTotal[LUMP_PLANES]; - - struct lumpdata *texinfo = &pWorldEnt()->lumps[LUMP_TEXINFO]; - texinfo->count = map.cTotal[LUMP_TEXINFO]; WriteExtendedTexinfoFlags(); WriteBSPFile();