1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-27 23:45:38 +00:00

Split ESM::Land::loadData

This commit is contained in:
elsid 2023-08-12 15:04:44 +02:00
parent a027cd898b
commit 955790dc31
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
4 changed files with 35 additions and 37 deletions

View file

@ -206,7 +206,7 @@ namespace NavMeshTool
ESM::Land::DEFAULT_HEIGHT }; ESM::Land::DEFAULT_HEIGHT };
ESM::Land::LandData& landData = *landDatas.emplace_back(std::make_unique<ESM::Land::LandData>()); ESM::Land::LandData& landData = *landDatas.emplace_back(std::make_unique<ESM::Land::LandData>());
land->loadData(ESM::Land::DATA_VHGT, &landData); land->loadData(ESM::Land::DATA_VHGT, landData);
heightfields.push_back(std::vector<float>(std::begin(landData.mHeights), std::end(landData.mHeights))); heightfields.push_back(std::vector<float>(std::begin(landData.mHeights), std::end(landData.mHeights)));
HeightfieldSurface surface; HeightfieldSurface surface;
surface.mHeights = heightfields.back().data(); surface.mHeights = heightfields.back().data();

View file

@ -9,7 +9,7 @@ ESM::LandData::LandData(const ESM::Land& land, int loadFlags)
, mPlugin(land.getPlugin()) , mPlugin(land.getPlugin())
{ {
ESM::Land::LandData data; ESM::Land::LandData data;
land.loadData(loadFlags, &data); land.loadData(loadFlags, data);
mLoadFlags = data.mDataLoaded; mLoadFlags = data.mDataLoaded;
std::span<const float> heights(data.mHeights); std::span<const float> heights(data.mHeights);
mHeights = std::vector(heights.begin(), heights.end()); mHeights = std::vector(heights.begin(), heights.end());

View file

@ -227,29 +227,29 @@ namespace ESM
mContext.filename.clear(); mContext.filename.clear();
} }
void Land::loadData(int flags, LandData* target) const void Land::loadData(int flags) const
{ {
// Create storage if nothing is loaded if (mLandData == nullptr)
if (target == nullptr && mLandData == nullptr)
mLandData = std::make_unique<LandData>(); mLandData = std::make_unique<LandData>();
if (target == nullptr) loadData(flags, *mLandData);
target = mLandData.get(); }
void Land::loadData(int flags, LandData& data) const
{
// Try to load only available data // Try to load only available data
flags = flags & mDataTypes; flags = flags & mDataTypes;
// Return if all required data is loaded // Return if all required data is loaded
if ((target->mDataLoaded & flags) == flags) if ((data.mDataLoaded & flags) == flags)
{ {
return; return;
} }
// Copy data to target if no file
if (mContext.filename.empty()) if (mContext.filename.empty())
{ {
// Make sure there is data, and that it doesn't point to the same object. // Make sure there is data, and that it doesn't point to the same object.
if (mLandData != nullptr && mLandData.get() != target) if (mLandData != nullptr && mLandData.get() != &data)
*target = *mLandData; data = *mLandData;
return; return;
} }
@ -259,41 +259,41 @@ namespace ESM
if (reader.isNextSub("VNML")) 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")) if (reader.isNextSub("VHGT"))
{ {
VHGT 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<float>::max(); data.mMinHeight = std::numeric_limits<float>::max();
target->mMaxHeight = -std::numeric_limits<float>::max(); data.mMaxHeight = -std::numeric_limits<float>::max();
float rowOffset = vhgt.mHeightOffset; float rowOffset = vhgt.mHeightOffset;
for (int y = 0; y < LAND_SIZE; y++) for (int y = 0; y < LAND_SIZE; y++)
{ {
rowOffset += vhgt.mHeightData[y * LAND_SIZE]; rowOffset += vhgt.mHeightData[y * LAND_SIZE];
target->mHeights[y * LAND_SIZE] = rowOffset * HEIGHT_SCALE; data.mHeights[y * LAND_SIZE] = rowOffset * HEIGHT_SCALE;
if (rowOffset * HEIGHT_SCALE > target->mMaxHeight) if (rowOffset * HEIGHT_SCALE > data.mMaxHeight)
target->mMaxHeight = rowOffset * HEIGHT_SCALE; data.mMaxHeight = rowOffset * HEIGHT_SCALE;
if (rowOffset * HEIGHT_SCALE < target->mMinHeight) if (rowOffset * HEIGHT_SCALE < data.mMinHeight)
target->mMinHeight = rowOffset * HEIGHT_SCALE; data.mMinHeight = rowOffset * HEIGHT_SCALE;
float colOffset = rowOffset; float colOffset = rowOffset;
for (int x = 1; x < LAND_SIZE; x++) for (int x = 1; x < LAND_SIZE; x++)
{ {
colOffset += vhgt.mHeightData[y * 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) if (colOffset * HEIGHT_SCALE > data.mMaxHeight)
target->mMaxHeight = colOffset * HEIGHT_SCALE; data.mMaxHeight = colOffset * HEIGHT_SCALE;
if (colOffset * HEIGHT_SCALE < target->mMinHeight) if (colOffset * HEIGHT_SCALE < data.mMinHeight)
target->mMinHeight = colOffset * HEIGHT_SCALE; data.mMinHeight = colOffset * HEIGHT_SCALE;
} }
} }
target->mUnk1 = vhgt.mUnk1; data.mUnk1 = vhgt.mUnk1;
target->mUnk2 = vhgt.mUnk2; data.mUnk2 = vhgt.mUnk2;
} }
} }
@ -301,18 +301,18 @@ namespace ESM
reader.skipHSub(); reader.skipHSub();
if (reader.isNextSub("VCLR")) 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")) if (reader.isNextSub("VTEX"))
{ {
uint16_t vtex[LAND_NUM_TEXTURES]; 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; mLandData = nullptr;
} }

View file

@ -133,16 +133,14 @@ namespace ESM
void blank(); void blank();
/** void loadData(int flags) const;
* Actually loads data into target
* If target is nullptr, assumed target is mLandData void loadData(int flags, LandData& data) const;
*/
void loadData(int flags, LandData* target = nullptr) const;
/** /**
* Frees memory allocated for mLandData * Frees memory allocated for mLandData
*/ */
void unloadData() const; void unloadData();
/// Check if given data type is loaded /// Check if given data type is loaded
bool isDataLoaded(int flags) const; bool isDataLoaded(int flags) const;