From cb3ef7b19f2d995a206e1925188893ccbc32b5d1 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 22 Jun 2022 17:03:00 -0400 Subject: [PATCH] allow radlights to specify an entity dictionary instead (if the input is a .ent, it is an entity dict) which is more flexible --- common/entdata.cc | 23 +++++++++++++++-------- include/common/entdata.h | 1 + include/light/light.hh | 2 +- light/entities.cc | 19 ++++++++++++++++--- light/light.cc | 14 ++++++-------- light/ltface.cc | 2 +- 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/common/entdata.cc b/common/entdata.cc index d647c5b2..0c64b212 100644 --- a/common/entdata.cc +++ b/common/entdata.cc @@ -173,14 +173,9 @@ void entdict_t::parse(parser_base_t &parser) } } -/* - * ================== - * EntData_Parse - * ================== - */ -std::vector EntData_Parse(const std::string &entdata) + +void EntData_ParseInto(const std::string &entdata, std::vector &vector) { - std::vector result; parser_t parser(entdata); /* go through all the entities */ @@ -190,8 +185,20 @@ std::vector EntData_Parse(const std::string &entdata) break; // emplace a new entdict_t out of the parser - result.emplace_back(parser); + vector.emplace_back(parser); } +} + +/* + * ================== + * EntData_Parse + * ================== + */ +std::vector EntData_Parse(const std::string &entdata) +{ + std::vector result; + + EntData_ParseInto(entdata, result); return result; } diff --git a/include/common/entdata.h b/include/common/entdata.h index fb512d63..9323d54d 100644 --- a/include/common/entdata.h +++ b/include/common/entdata.h @@ -69,5 +69,6 @@ public: void parse(parser_base_t &parser); }; +void EntData_ParseInto(const std::string &entdata, std::vector &vector); std::vector EntData_Parse(const std::string &entdata); std::string EntData_Write(const std::vector &ents); diff --git a/include/light/light.hh b/include/light/light.hh index b3358814..29960090 100644 --- a/include/light/light.hh +++ b/include/light/light.hh @@ -536,7 +536,7 @@ public: extern settings::light_settings options; -extern uint8_t *filebase; +extern std::vector filebase; extern std::vector lit_filebase; extern std::vector lux_filebase; diff --git a/light/entities.cc b/light/entities.cc index 42923307..7523afc8 100644 --- a/light/entities.cc +++ b/light/entities.cc @@ -1297,16 +1297,29 @@ static void GL_SubdivideSurface(const mface_t *face, const modelinfo_t *face_mod SubdividePolygon(face, face_modelinfo, bsp, face->numedges, verts, options.surflight_subdivide.value()); } +static bool ParseEntityLights(std::ifstream &f) +{ + std::string str{std::istreambuf_iterator(f), std::istreambuf_iterator()}; + parser_t p(str); + + EntData_ParseInto(str, radlights); + return true; +} + bool ParseLightsFile(const fs::path &fname) { - // note: this creates dupes. super bright light! (and super slow, too) - std::string buf; std::ifstream f(fname); if (!f) return false; + // use entity-style format + if (fname.extension() == ".ent") { + return ParseEntityLights(f); + } + while (!f.eof()) { + std::string buf; std::getline(f, buf); parser_t parser(buf); @@ -1404,7 +1417,7 @@ static void MakeSurfaceLights(const mbsp_t *bsp) } } - if (surflights_dump_file) { + if (surflights_dump_file.is_open()) { surflights_dump_file.close(); fmt::print("wrote surface lights to '{}'\n", surflights_dump_filename); } diff --git a/light/light.cc b/light/light.cc index 234f9acb..737681c7 100644 --- a/light/light.cc +++ b/light/light.cc @@ -62,7 +62,7 @@ bool dirt_in_use = false; static facesup_t *faces_sup; // lit2/bspx stuff /// start of lightmap data -uint8_t *filebase; +std::vector filebase; /// offset of start of free space after data (should be kept a multiple of 4) static int file_p; /// offset of end of free space for lightmap data @@ -223,7 +223,7 @@ void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata, { light_mutex.lock(); - *lightdata = filebase + file_p; + *lightdata = filebase.data() + file_p; *colordata = lit_filebase.data() + lit_file_p; *deluxdata = lux_filebase.data() + lux_file_p; @@ -255,7 +255,7 @@ void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata, { Q_assert(lightofs >= 0); - *lightdata = filebase + lightofs; + *lightdata = filebase.data() + lightofs; if (colordata) { *colordata = lit_filebase.data() + (lightofs * 3); @@ -428,14 +428,12 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) mbsp_t &bsp = std::get(bspdata->bsp); - delete[] filebase; + filebase.clear(); lit_filebase.clear(); lux_filebase.clear(); /* greyscale data stored in a separate buffer */ - filebase = new uint8_t[MAX_MAP_LIGHTING]{}; - if (!filebase) - FError("allocation of {} bytes failed.", MAX_MAP_LIGHTING); + filebase.resize(MAX_MAP_LIGHTING); file_p = 0; file_end = MAX_MAP_LIGHTING; @@ -515,7 +513,7 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale) memcpy(bsp.dlightdata.data(), lit_filebase.data(), bsp.dlightdata.size()); } else { bsp.dlightdata.resize(file_p); - memcpy(bsp.dlightdata.data(), filebase, bsp.dlightdata.size()); + memcpy(bsp.dlightdata.data(), filebase.data(), bsp.dlightdata.size()); } } else { // NOTE: bsp.lightdatasize is already valid in the -litonly case diff --git a/light/ltface.cc b/light/ltface.cc index 0130e918..9a556250 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -3240,7 +3240,7 @@ static void WriteLightmaps( if (bsp->loadversion->game->has_rgb_lightmap) { lightofs = lit - lit_filebase.data(); } else { - lightofs = out - filebase; + lightofs = out - filebase.data(); } if (facesup) {