1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-23 00:41:33 +00:00

Make ClearCacheFunctor a class and rename to clarify the purpose

This commit is contained in:
elsid 2024-04-20 01:51:39 +02:00
parent 79435bb9f2
commit 33ef7fc8ca
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625

View file

@ -875,35 +875,54 @@ namespace MWRender
return Mask_Static; return Mask_Static;
} }
struct ClearCacheFunctor namespace
{ {
void operator()(MWRender::ChunkId id, osg::Object* obj) class CollectIntersecting
{ {
if (intersects(id, mPosition)) public:
mToClear.insert(id); explicit CollectIntersecting(
} bool activeGridOnly, const osg::Vec3f& position, const osg::Vec2i& cell, ESM::RefId worldspace)
bool intersects(ChunkId id, osg::Vec3f pos) : mActiveGridOnly(activeGridOnly)
{ , mPosition(position)
if (mActiveGridOnly && !std::get<2>(id)) , mCell(cell)
return false; , mWorldspace(worldspace)
pos /= getCellSize(mWorldspace); {
clampToCell(pos); }
osg::Vec2f center = std::get<0>(id);
float halfSize = std::get<1>(id) / 2; void operator()(const ChunkId& id, osg::Object* /*obj*/)
return pos.x() >= center.x() - halfSize && pos.y() >= center.y() - halfSize {
&& pos.x() <= center.x() + halfSize && pos.y() <= center.y() + halfSize; if (mActiveGridOnly && !std::get<2>(id))
} return;
void clampToCell(osg::Vec3f& cellPos) if (intersects(id, mPosition))
{ mCollected.insert(id);
cellPos.x() = std::clamp<float>(cellPos.x(), mCell.x(), mCell.x() + 1); }
cellPos.y() = std::clamp<float>(cellPos.y(), mCell.y(), mCell.y() + 1);
} const std::set<ChunkId>& getCollected() const { return mCollected; }
osg::Vec3f mPosition;
osg::Vec2i mCell; private:
ESM::RefId mWorldspace; bool intersects(ChunkId id, osg::Vec3f pos) const
std::set<MWRender::ChunkId> mToClear; {
bool mActiveGridOnly = false; pos /= getCellSize(mWorldspace);
}; clampToCell(pos);
osg::Vec2f center = std::get<0>(id);
float halfSize = std::get<1>(id) / 2;
return pos.x() >= center.x() - halfSize && pos.y() >= center.y() - halfSize
&& pos.x() <= center.x() + halfSize && pos.y() <= center.y() + halfSize;
}
void clampToCell(osg::Vec3f& cellPos) const
{
cellPos.x() = std::clamp<float>(cellPos.x(), mCell.x(), mCell.x() + 1);
cellPos.y() = std::clamp<float>(cellPos.y(), mCell.y(), mCell.y() + 1);
}
bool mActiveGridOnly;
osg::Vec3f mPosition;
osg::Vec2i mCell;
ESM::RefId mWorldspace;
std::set<ChunkId> mCollected;
};
}
bool ObjectPaging::enableObject( bool ObjectPaging::enableObject(
int type, ESM::RefNum refnum, const osg::Vec3f& pos, const osg::Vec2i& cell, bool enabled) int type, ESM::RefNum refnum, const osg::Vec3f& pos, const osg::Vec2i& cell, bool enabled)
@ -921,14 +940,11 @@ namespace MWRender
return false; return false;
} }
ClearCacheFunctor ccf; CollectIntersecting ccf(false, pos, cell, mWorldspace);
ccf.mPosition = pos;
ccf.mCell = cell;
ccf.mWorldspace = mWorldspace;
mCache->call(ccf); mCache->call(ccf);
if (ccf.mToClear.empty()) if (ccf.getCollected().empty())
return false; return false;
for (const auto& chunk : ccf.mToClear) for (const ChunkId& chunk : ccf.getCollected())
mCache->removeFromObjectCache(chunk); mCache->removeFromObjectCache(chunk);
return true; return true;
} }
@ -946,15 +962,11 @@ namespace MWRender
return false; return false;
} }
ClearCacheFunctor ccf; CollectIntersecting ccf(true, pos, cell, mWorldspace);
ccf.mPosition = pos;
ccf.mCell = cell;
ccf.mActiveGridOnly = true;
ccf.mWorldspace = mWorldspace;
mCache->call(ccf); mCache->call(ccf);
if (ccf.mToClear.empty()) if (ccf.getCollected().empty())
return false; return false;
for (const auto& chunk : ccf.mToClear) for (const ChunkId& chunk : ccf.getCollected())
mCache->removeFromObjectCache(chunk); mCache->removeFromObjectCache(chunk);
return true; return true;
} }