From 3f0a656c7065f4167ad4d9d5fcbfbe6baa569aee Mon Sep 17 00:00:00 2001 From: Joilnen Date: Tue, 12 Sep 2023 18:35:36 -0300 Subject: [PATCH] Terrain: range loop refactoring (#2940) --- Components/Terrain/src/OgreTerrainGroup.cpp | 130 +++++++++----------- 1 file changed, 55 insertions(+), 75 deletions(-) diff --git a/Components/Terrain/src/OgreTerrainGroup.cpp b/Components/Terrain/src/OgreTerrainGroup.cpp index 4d905ff217b..23c374ce576 100644 --- a/Components/Terrain/src/OgreTerrainGroup.cpp +++ b/Components/Terrain/src/OgreTerrainGroup.cpp @@ -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) @@ -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) @@ -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); @@ -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); @@ -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? @@ -261,10 +255,8 @@ namespace Ogre filename = generateFilename(slot->x, slot->y); t->save(filename); - } } - } } @@ -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 @@ -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); } } @@ -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 @@ -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); @@ -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: @@ -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); } } @@ -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 @@ -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()); } @@ -793,7 +783,6 @@ namespace Ogre slot->y <<") with the error '" << res->getMessages() << "'"; freeTerrainSlotInstance(slot); } - } //--------------------------------------------------------------------- void TerrainGroup::connectNeighbour(TerrainSlot* slot, long offsetx, long offsety) @@ -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); } } //--------------------------------------------------------------------- @@ -821,8 +809,6 @@ namespace Ogre key = (x16 << 16) | y16; return key; - - } //--------------------------------------------------------------------- void TerrainGroup::unpackIndex(uint32 key, long *x, long *y) @@ -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(); } @@ -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() @@ -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); @@ -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); @@ -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)); } } } @@ -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); } } } @@ -1099,6 +1081,4 @@ namespace Ogre OGRE_DELETE instance; instance = 0; } - } -