From 955790dc312a1c32a2bb2440b9d59a1163954de2 Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 12 Aug 2023 15:04:44 +0200 Subject: [PATCH] Split ESM::Land::loadData --- apps/navmeshtool/worldspacedata.cpp | 2 +- components/esm/esmterrain.cpp | 2 +- components/esm3/loadland.cpp | 58 ++++++++++++++--------------- components/esm3/loadland.hpp | 10 ++--- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/apps/navmeshtool/worldspacedata.cpp b/apps/navmeshtool/worldspacedata.cpp index 9712b80b6f..8d2ac7382b 100644 --- a/apps/navmeshtool/worldspacedata.cpp +++ b/apps/navmeshtool/worldspacedata.cpp @@ -206,7 +206,7 @@ namespace NavMeshTool ESM::Land::DEFAULT_HEIGHT }; ESM::Land::LandData& landData = *landDatas.emplace_back(std::make_unique()); - land->loadData(ESM::Land::DATA_VHGT, &landData); + land->loadData(ESM::Land::DATA_VHGT, landData); heightfields.push_back(std::vector(std::begin(landData.mHeights), std::end(landData.mHeights))); HeightfieldSurface surface; surface.mHeights = heightfields.back().data(); diff --git a/components/esm/esmterrain.cpp b/components/esm/esmterrain.cpp index d59d69ec46..18a267d51c 100644 --- a/components/esm/esmterrain.cpp +++ b/components/esm/esmterrain.cpp @@ -9,7 +9,7 @@ ESM::LandData::LandData(const ESM::Land& land, int loadFlags) , mPlugin(land.getPlugin()) { ESM::Land::LandData data; - land.loadData(loadFlags, &data); + land.loadData(loadFlags, data); mLoadFlags = data.mDataLoaded; std::span heights(data.mHeights); mHeights = std::vector(heights.begin(), heights.end()); diff --git a/components/esm3/loadland.cpp b/components/esm3/loadland.cpp index 092ac0a60d..98e07b530f 100644 --- a/components/esm3/loadland.cpp +++ b/components/esm3/loadland.cpp @@ -227,29 +227,29 @@ namespace ESM mContext.filename.clear(); } - void Land::loadData(int flags, LandData* target) const + void Land::loadData(int flags) const { - // Create storage if nothing is loaded - if (target == nullptr && mLandData == nullptr) + if (mLandData == nullptr) mLandData = std::make_unique(); - if (target == nullptr) - target = mLandData.get(); + loadData(flags, *mLandData); + } + void Land::loadData(int flags, LandData& data) const + { // Try to load only available data flags = flags & mDataTypes; // Return if all required data is loaded - if ((target->mDataLoaded & flags) == flags) + if ((data.mDataLoaded & flags) == flags) { return; } - // Copy data to target if no file if (mContext.filename.empty()) { // Make sure there is data, and that it doesn't point to the same object. - if (mLandData != nullptr && mLandData.get() != target) - *target = *mLandData; + if (mLandData != nullptr && mLandData.get() != &data) + data = *mLandData; return; } @@ -259,41 +259,41 @@ namespace ESM if (reader.isNextSub("VNML")) { - condLoad(reader, flags, target->mDataLoaded, DATA_VNML, target->mNormals, sizeof(target->mNormals)); + condLoad(reader, flags, data.mDataLoaded, DATA_VNML, data.mNormals, sizeof(data.mNormals)); } if (reader.isNextSub("VHGT")) { VHGT vhgt; - if (condLoad(reader, flags, target->mDataLoaded, DATA_VHGT, &vhgt, sizeof(vhgt))) + if (condLoad(reader, flags, data.mDataLoaded, DATA_VHGT, &vhgt, sizeof(vhgt))) { - target->mMinHeight = std::numeric_limits::max(); - target->mMaxHeight = -std::numeric_limits::max(); + data.mMinHeight = std::numeric_limits::max(); + data.mMaxHeight = -std::numeric_limits::max(); float rowOffset = vhgt.mHeightOffset; for (int y = 0; y < LAND_SIZE; y++) { rowOffset += vhgt.mHeightData[y * LAND_SIZE]; - target->mHeights[y * LAND_SIZE] = rowOffset * HEIGHT_SCALE; - if (rowOffset * HEIGHT_SCALE > target->mMaxHeight) - target->mMaxHeight = rowOffset * HEIGHT_SCALE; - if (rowOffset * HEIGHT_SCALE < target->mMinHeight) - target->mMinHeight = rowOffset * HEIGHT_SCALE; + data.mHeights[y * LAND_SIZE] = rowOffset * HEIGHT_SCALE; + if (rowOffset * HEIGHT_SCALE > data.mMaxHeight) + data.mMaxHeight = rowOffset * HEIGHT_SCALE; + if (rowOffset * HEIGHT_SCALE < data.mMinHeight) + data.mMinHeight = rowOffset * HEIGHT_SCALE; float colOffset = rowOffset; for (int x = 1; x < LAND_SIZE; x++) { colOffset += vhgt.mHeightData[y * LAND_SIZE + x]; - target->mHeights[x + y * LAND_SIZE] = colOffset * HEIGHT_SCALE; + data.mHeights[x + y * LAND_SIZE] = colOffset * HEIGHT_SCALE; - if (colOffset * HEIGHT_SCALE > target->mMaxHeight) - target->mMaxHeight = colOffset * HEIGHT_SCALE; - if (colOffset * HEIGHT_SCALE < target->mMinHeight) - target->mMinHeight = colOffset * HEIGHT_SCALE; + if (colOffset * HEIGHT_SCALE > data.mMaxHeight) + data.mMaxHeight = colOffset * HEIGHT_SCALE; + if (colOffset * HEIGHT_SCALE < data.mMinHeight) + data.mMinHeight = colOffset * HEIGHT_SCALE; } } - target->mUnk1 = vhgt.mUnk1; - target->mUnk2 = vhgt.mUnk2; + data.mUnk1 = vhgt.mUnk1; + data.mUnk2 = vhgt.mUnk2; } } @@ -301,18 +301,18 @@ namespace ESM reader.skipHSub(); if (reader.isNextSub("VCLR")) - condLoad(reader, flags, target->mDataLoaded, DATA_VCLR, target->mColours, 3 * LAND_NUM_VERTS); + condLoad(reader, flags, data.mDataLoaded, DATA_VCLR, data.mColours, 3 * LAND_NUM_VERTS); if (reader.isNextSub("VTEX")) { uint16_t vtex[LAND_NUM_TEXTURES]; - if (condLoad(reader, flags, target->mDataLoaded, DATA_VTEX, vtex, sizeof(vtex))) + if (condLoad(reader, flags, data.mDataLoaded, DATA_VTEX, vtex, sizeof(vtex))) { - transposeTextureData(vtex, target->mTextures); + transposeTextureData(vtex, data.mTextures); } } } - void Land::unloadData() const + void Land::unloadData() { mLandData = nullptr; } diff --git a/components/esm3/loadland.hpp b/components/esm3/loadland.hpp index f5f4ef7a0f..0aef70a476 100644 --- a/components/esm3/loadland.hpp +++ b/components/esm3/loadland.hpp @@ -133,16 +133,14 @@ namespace ESM void blank(); - /** - * Actually loads data into target - * If target is nullptr, assumed target is mLandData - */ - void loadData(int flags, LandData* target = nullptr) const; + void loadData(int flags) const; + + void loadData(int flags, LandData& data) const; /** * Frees memory allocated for mLandData */ - void unloadData() const; + void unloadData(); /// Check if given data type is loaded bool isDataLoaded(int flags) const;