From a7fd04af12a6cff6eb13bb1876682c6b5820bdf3 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Mon, 23 Aug 2021 22:44:22 -0600 Subject: [PATCH] qbsp: refactor texdata writing --- include/qbsp/map.hh | 1 + qbsp/bspfile.cc | 7 +++---- qbsp/wad.cc | 14 ++++++-------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index bb7c45d5..b277f2e3 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -171,6 +171,7 @@ typedef struct mapdata_s { std::vector exported_faces; std::vector exported_models; std::string exported_entities; + std::string exported_texdata; // helpers std::string texinfoTextureName(int texinfo) const { diff --git a/qbsp/bspfile.cc b/qbsp/bspfile.cc index 163b915c..e3e372ce 100644 --- a/qbsp/bspfile.cc +++ b/qbsp/bspfile.cc @@ -308,7 +308,7 @@ WriteBSPFile(void) AddLumpFromBuffer(f, LUMP_LIGHTING, nullptr, 0); AddLumpFromBuffer(f, LUMP_VISIBILITY, nullptr, 0); AddLumpFromBuffer(f, LUMP_ENTITIES, map.exported_entities.data(), map.exported_entities.size() + 1); // +1 to write the terminating null (safe in C++11) - AddLump(f, LUMP_TEXTURES); + AddLumpFromBuffer(f, LUMP_TEXTURES, map.exported_texdata.data(), map.exported_texdata.size()); GenLump("LMSHIFT", BSPX_LMSHIFT, 1); @@ -392,10 +392,9 @@ PrintBSPFileSizes(void) Message(msgStat, "%8d surfedges %10d", static_cast(map.exported_surfedges.size()), static_cast(map.exported_surfedges.size()) * MemSize[BSP_SURFEDGE]); Message(msgStat, "%8d edges %10d", static_cast(map.exported_edges.size()), static_cast(map.exported_edges.size()) * MemSize[BSP_EDGE]); - lump = &pWorldEnt()->lumps[LUMP_TEXTURES]; - if (lump->data) + if (!map.exported_texdata.empty()) Message(msgStat, "%8d textures %10d", - ((dmiptexlump_t *)lump->data)->nummiptex, lump->count); + ((dmiptexlump_t *)map.exported_texdata.data())->nummiptex, map.exported_texdata.size()); else Message(msgStat, " 0 textures 0"); diff --git a/qbsp/wad.cc b/qbsp/wad.cc index 5addbde6..0a587dd9 100644 --- a/qbsp/wad.cc +++ b/qbsp/wad.cc @@ -199,24 +199,23 @@ WADList_Process(const wad_t *wadlist) int i; lumpinfo_t *texture; dmiptexlump_t *miptexlump; - struct lumpdata *texdata = &pWorldEnt()->lumps[LUMP_TEXTURES]; - + WADList_AddAnimationFrames(wadlist); /* Count space for miptex header/offsets */ - texdata->count = offsetof(dmiptexlump_t, dataofs[0]) + (map.nummiptex() * sizeof(uint32_t)); + size_t texdatasize = offsetof(dmiptexlump_t, dataofs[0]) + (map.nummiptex() * sizeof(uint32_t)); /* Count texture size. Slower, but saves memory. */ for (i = 0; i < map.nummiptex(); i++) { texture = WADList_FindTexture(wadlist, map.miptex.at(i).c_str()); if (texture) { - texdata->count += texture->size; + texdatasize += texture->size; } } /* Default texture data to store in worldmodel */ - texdata->data = AllocMem(BSP_TEX, texdata->count, true); - miptexlump = (dmiptexlump_t *)texdata->data; + map.exported_texdata = std::string(texdatasize, '\0'); + miptexlump = (dmiptexlump_t *)map.exported_texdata.data(); miptexlump->nummiptex = map.nummiptex(); WADList_LoadTextures(wadlist, miptexlump); @@ -236,7 +235,6 @@ WADList_LoadTextures(const wad_t *wadlist, dmiptexlump_t *lump) int i, size; uint8_t *data; const wad_t *wad; - struct lumpdata *texdata = &pWorldEnt()->lumps[LUMP_TEXTURES]; data = (uint8_t *)&lump->dataofs[map.nummiptex()]; @@ -251,7 +249,7 @@ WADList_LoadTextures(const wad_t *wadlist, dmiptexlump_t *lump) } if (!size) continue; - if (data + size - (uint8_t *)texdata->data > texdata->count) + if (data + size - (uint8_t *)map.exported_texdata.data() > map.exported_texdata.size()) Error("Internal error: not enough texture memory allocated"); lump->dataofs[i] = data - (uint8_t *)lump; data += size;