Skip to content

Commit

Permalink
Terrain: range loop refactoring (#2940)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joilnen authored Sep 12, 2023
1 parent 64bc666 commit 3f0a656
Showing 1 changed file with 55 additions and 75 deletions.
130 changes: 55 additions & 75 deletions Components/Terrain/src/OgreTerrainGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Ogre
const uint16 TerrainGroup::CHUNK_VERSION = 1;

//---------------------------------------------------------------------
TerrainGroup::TerrainGroup(SceneManager* sm, Terrain::Alignment align,
TerrainGroup::TerrainGroup(SceneManager* sm, Terrain::Alignment align,
uint16 terrainSize, Real terrainWorldSize)
: mSceneManager(sm)
, mAlignment(align)
Expand Down Expand Up @@ -103,16 +103,14 @@ namespace Ogre
if (pos != mOrigin)
{
mOrigin = pos;
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& t : mTerrainSlots)
{
TerrainSlot* slot = i->second;
if (slot->instance)
{
TerrainSlot* slot = t.second;
if (slot->instance) {
slot->instance->setPosition(getTerrainSlotPosition(slot->x, slot->y));
}
}
}

}
//---------------------------------------------------------------------
void TerrainGroup::setFilenameConvention(const String& prefix, const String& extension)
Expand Down Expand Up @@ -164,7 +162,7 @@ namespace Ogre

}
//---------------------------------------------------------------------
void TerrainGroup::defineTerrain(long x, long y, const Image* img,
void TerrainGroup::defineTerrain(long x, long y, const Image* img,
const Terrain::LayerInstanceList* layers /*= 0*/)
{
TerrainSlot* slot = getTerrainSlot(x, y, true);
Expand All @@ -188,7 +186,7 @@ namespace Ogre

}
//---------------------------------------------------------------------
void TerrainGroup::defineTerrain(long x, long y, const float* pFloat /*= 0*/,
void TerrainGroup::defineTerrain(long x, long y, const float* pFloat /*= 0*/,
const Terrain::LayerInstanceList* layers /*= 0*/)
{
TerrainSlot* slot = getTerrainSlot(x, y, true);
Expand Down Expand Up @@ -229,25 +227,21 @@ namespace Ogre
//---------------------------------------------------------------------
void TerrainGroup::loadAllTerrains(bool synchronous /*= false*/)
{
// Just a straight iteration - for the numbers involved not worth
// Just a straight iteration - for the numbers involved not worth
// keeping a loaded / unloaded list
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
{
TerrainSlot* slot = i->second;
loadTerrainImpl(slot, synchronous);
}

for (const auto& t : mTerrainSlots)
loadTerrainImpl(t.second, synchronous);
}
//---------------------------------------------------------------------
void TerrainGroup::saveAllTerrains(bool onlyIfModified, bool replaceFilenames)
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
TerrainSlot* slot = i->second;
TerrainSlot* slot = s.second;
if (slot->instance)
{
Terrain* t = slot->instance;
if (t->isLoaded() &&
if (t->isLoaded() &&
(!onlyIfModified || t->isModified()))
{
// Overwrite the file names?
Expand All @@ -261,10 +255,8 @@ namespace Ogre
filename = generateFilename(slot->x, slot->y);

t->save(filename);

}
}

}

}
Expand Down Expand Up @@ -347,7 +339,7 @@ namespace Ogre
//---------------------------------------------------------------------
void TerrainGroup::loadTerrainImpl(TerrainSlot* slot, bool synchronous)
{
if (!slot->instance &&
if (!slot->instance &&
(!slot->def.filename.empty() || slot->def.importData))
{
// Allocate in main thread so no race conditions
Expand Down Expand Up @@ -424,9 +416,9 @@ namespace Ogre
{
if(mAutoUpdateLod)
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
TerrainSlot* slot = i->second;
TerrainSlot* slot = s.second;
mAutoUpdateLod->autoUpdateLod(slot->instance, synchronous, data);
}
}
Expand Down Expand Up @@ -456,9 +448,8 @@ namespace Ogre
//---------------------------------------------------------------------
void TerrainGroup::removeAllTerrains()
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
{
OGRE_DELETE i->second;
for (const auto& s : mTerrainSlots) {
OGRE_DELETE s.second;
}
mTerrainSlots.clear();
// Also clear buffer pools, if we're clearing completely may not be representative
Expand Down Expand Up @@ -509,7 +500,7 @@ namespace Ogre
}
}
//---------------------------------------------------------------------
TerrainGroup::RayResult TerrainGroup::rayIntersects(const Ray& ray, Real distanceLimit /* = 0*/) const
TerrainGroup::RayResult TerrainGroup::rayIntersects(const Ray& ray, Real distanceLimit /* = 0*/) const
{
long curr_x, curr_z;
convertWorldPositionToTerrainSlot(ray.getOrigin(), &curr_x, &curr_z);
Expand All @@ -529,13 +520,13 @@ namespace Ogre
break;
case Terrain::ALIGN_Y_Z:
// x = z, z = y, y = -x
tmp.x = localRayDir.z;
tmp.z = localRayDir.y;
tmp.y = -localRayDir.x;
tmp.x = localRayDir.z;
tmp.z = localRayDir.y;
tmp.y = -localRayDir.x;
localRayDir = tmp;
tmp.x = offset.z;
tmp.z = offset.y;
tmp.y = -offset.x;
tmp.x = offset.z;
tmp.z = offset.y;
tmp.y = -offset.x;
offset = tmp;
break;
case Terrain::ALIGN_X_Z:
Expand Down Expand Up @@ -650,10 +641,10 @@ namespace Ogre
{
resultList->clear();
// Much simpler test
for (TerrainSlotMap::const_iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (auto const& s : mTerrainSlots)
{
if (i->second->instance && box.intersects(i->second->instance->getWorldAABB()))
resultList->push_back(i->second->instance);
if (s.second->instance && box.intersects(s.second->instance->getWorldAABB()))
resultList->push_back(s.second->instance);
}

}
Expand All @@ -662,12 +653,11 @@ namespace Ogre
{
resultList->clear();
// Much simpler test
for (TerrainSlotMap::const_iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (auto const& s : mTerrainSlots)
{
if (i->second->instance && sphere.intersects(i->second->instance->getWorldAABB()))
resultList->push_back(i->second->instance);
if (s.second->instance && sphere.intersects(s.second->instance->getWorldAABB()))
resultList->push_back(s.second->instance);
}

}
//---------------------------------------------------------------------
void TerrainGroup::convertWorldPositionToTerrainSlot(const Vector3& pos, long *x, long *y) const
Expand Down Expand Up @@ -731,7 +721,7 @@ namespace Ogre
catch (Exception& e)
{
// oops
response = OGRE_NEW WorkQueue::Response(req, false, Any(),
response = OGRE_NEW WorkQueue::Response(req, false, Any(),
e.getFullDescription());
}

Expand Down Expand Up @@ -793,7 +783,6 @@ namespace Ogre
slot->y <<") with the error '" << res->getMessages() << "'";
freeTerrainSlotInstance(slot);
}

}
//---------------------------------------------------------------------
void TerrainGroup::connectNeighbour(TerrainSlot* slot, long offsetx, long offsety)
Expand All @@ -802,8 +791,7 @@ namespace Ogre
if (neighbourSlot && neighbourSlot->instance && neighbourSlot->instance->isLoaded())
{
// reclaculate if imported
slot->instance->setNeighbour(Terrain::getNeighbourIndex(offsetx, offsety), neighbourSlot->instance,
slot->def.importData != 0);
slot->instance->setNeighbour(Terrain::getNeighbourIndex(offsetx, offsety), neighbourSlot->instance, slot->def.importData != 0);
}
}
//---------------------------------------------------------------------
Expand All @@ -821,8 +809,6 @@ namespace Ogre
key = (x16 << 16) | y16;

return key;


}
//---------------------------------------------------------------------
void TerrainGroup::unpackIndex(uint32 key, long *x, long *y)
Expand All @@ -841,8 +827,8 @@ namespace Ogre
String TerrainGroup::generateFilename(long x, long y) const
{
StringStream str;
str << mFilenamePrefix << "_" <<
std::setw(8) << std::setfill('0') << std::hex << packIndex(x, y) <<
str << mFilenamePrefix << "_" <<
std::setw(8) << std::setfill('0') << std::hex << packIndex(x, y) <<
"." << mFilenameExtension;
return str.str();
}
Expand Down Expand Up @@ -900,42 +886,40 @@ namespace Ogre
//---------------------------------------------------------------------
void TerrainGroup::freeTemporaryResources()
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
if (i->second->instance)
i->second->instance->freeTemporaryResources();
if (s.second->instance)
s.second->instance->freeTemporaryResources();
}

}
//---------------------------------------------------------------------
void TerrainGroup::update(bool synchronous)
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
if (i->second->instance)
i->second->instance->update();
if (s.second->instance)
s.second->instance->update();
}

}
//---------------------------------------------------------------------
void TerrainGroup::updateGeometry()
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
if (i->second->instance)
i->second->instance->updateGeometry();
if (s.second->instance)
s.second->instance->updateGeometry();
}

}
//---------------------------------------------------------------------
void TerrainGroup::updateDerivedData(bool synchronous, uint8 typeMask)
{
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
if (i->second->instance)
i->second->instance->updateDerivedData();
if (s.second->instance)
s.second->instance->updateDerivedData();
}

}
//---------------------------------------------------------------------
TerrainGroup::TerrainIterator TerrainGroup::getTerrainIterator()
Expand All @@ -950,7 +934,7 @@ namespace Ogre
//---------------------------------------------------------------------
void TerrainGroup::saveGroupDefinition(const String& filename)
{
DataStreamPtr stream = Root::getSingleton().createFileStream(filename,
DataStreamPtr stream = Root::getSingleton().createFileStream(filename,
getResourceGroup(), true);
StreamSerialiser ser(stream);
saveGroupDefinition(ser);
Expand Down Expand Up @@ -986,7 +970,7 @@ namespace Ogre
//---------------------------------------------------------------------
void TerrainGroup::loadGroupDefinition(const String& filename)
{
DataStreamPtr stream = Root::getSingleton().openFileStream(filename,
DataStreamPtr stream = Root::getSingleton().openFileStream(filename,
getResourceGroup());
StreamSerialiser ser(stream);
loadGroupDefinition(ser);
Expand Down Expand Up @@ -1035,12 +1019,12 @@ namespace Ogre
if (newWorldSize != mTerrainWorldSize)
{
mTerrainWorldSize = newWorldSize;
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
if (i->second->instance)
if (s.second->instance)
{
i->second->instance->setWorldSize(newWorldSize);
i->second->instance->setPosition(getTerrainSlotPosition(i->second->x, i->second->y));
s.second->instance->setWorldSize(newWorldSize);
s.second->instance->setPosition(getTerrainSlotPosition(s.second->x, s.second->y));
}
}
}
Expand All @@ -1051,12 +1035,10 @@ namespace Ogre
if (newTerrainSize != mTerrainSize)
{
mTerrainSize = newTerrainSize;
for (TerrainSlotMap::iterator i = mTerrainSlots.begin(); i != mTerrainSlots.end(); ++i)
for (const auto& s : mTerrainSlots)
{
if (i->second->instance)
{
i->second->instance->setSize(newTerrainSize);
}
if (s.second->instance)
s.second->instance->setSize(newTerrainSize);
}
}
}
Expand Down Expand Up @@ -1099,6 +1081,4 @@ namespace Ogre
OGRE_DELETE instance;
instance = 0;
}

}

0 comments on commit 3f0a656

Please sign in to comment.