diff --git a/apps/opencs/view/render/terrainstorage.cpp b/apps/opencs/view/render/terrainstorage.cpp index 5d2a4874c8..53e62d3969 100644 --- a/apps/opencs/view/render/terrainstorage.cpp +++ b/apps/opencs/view/render/terrainstorage.cpp @@ -42,7 +42,7 @@ namespace CSVRender land, ESM::Land::DATA_VHGT | ESM::Land::DATA_VNML | ESM::Land::DATA_VCLR | ESM::Land::DATA_VTEX); } - const ESM::LandTexture* TerrainStorage::getLandTexture(int index, short plugin) + const ESM::LandTexture* TerrainStorage::getLandTexture(std::uint16_t index, int plugin) { const int row = mData.getLandTextures().searchId( ESM::RefId::stringRefId(CSMWorld::LandTexture::createUniqueRecordId(plugin, index))); diff --git a/apps/opencs/view/render/terrainstorage.hpp b/apps/opencs/view/render/terrainstorage.hpp index b09de55081..5775fbf3ff 100644 --- a/apps/opencs/view/render/terrainstorage.hpp +++ b/apps/opencs/view/render/terrainstorage.hpp @@ -38,7 +38,7 @@ namespace CSVRender std::array mAlteredHeight; osg::ref_ptr getLand(ESM::ExteriorCellLocation cellLocation) override; - const ESM::LandTexture* getLandTexture(int index, short plugin) override; + const ESM::LandTexture* getLandTexture(std::uint16_t index, int plugin) override; void getBounds(float& minX, float& maxX, float& minY, float& maxY, ESM::RefId worldspace) override; diff --git a/apps/openmw/mwrender/terrainstorage.cpp b/apps/openmw/mwrender/terrainstorage.cpp index a20da97f0f..757e743cfc 100644 --- a/apps/openmw/mwrender/terrainstorage.cpp +++ b/apps/openmw/mwrender/terrainstorage.cpp @@ -11,8 +11,8 @@ namespace MWRender { - TerrainStorage::TerrainStorage(Resource::ResourceSystem* resourceSystem, const std::string& normalMapPattern, - const std::string& normalHeightMapPattern, bool autoUseNormalMaps, const std::string& specularMapPattern, + TerrainStorage::TerrainStorage(Resource::ResourceSystem* resourceSystem, std::string_view normalMapPattern, + std::string_view normalHeightMapPattern, bool autoUseNormalMaps, std::string_view specularMapPattern, bool autoUseSpecularMaps) : ESMTerrain::Storage(resourceSystem->getVFS(), normalMapPattern, normalHeightMapPattern, autoUseNormalMaps, specularMapPattern, autoUseSpecularMaps) @@ -105,8 +105,9 @@ namespace MWRender return mLandManager->getLand(cellLocation); } - const ESM::LandTexture* TerrainStorage::getLandTexture(int index, short plugin) + const ESM::LandTexture* TerrainStorage::getLandTexture(std::uint16_t index, int plugin) { + assert(plugin >= 0); // Saves don't contain textures const MWWorld::ESMStore& esmStore = *MWBase::Environment::get().getESMStore(); return esmStore.get().search(index, plugin); } diff --git a/apps/openmw/mwrender/terrainstorage.hpp b/apps/openmw/mwrender/terrainstorage.hpp index 0b41c06428..6e695dd30f 100644 --- a/apps/openmw/mwrender/terrainstorage.hpp +++ b/apps/openmw/mwrender/terrainstorage.hpp @@ -16,13 +16,13 @@ namespace MWRender class TerrainStorage : public ESMTerrain::Storage { public: - TerrainStorage(Resource::ResourceSystem* resourceSystem, const std::string& normalMapPattern = "", - const std::string& normalHeightMapPattern = "", bool autoUseNormalMaps = false, - const std::string& specularMapPattern = "", bool autoUseSpecularMaps = false); + TerrainStorage(Resource::ResourceSystem* resourceSystem, std::string_view normalMapPattern = {}, + std::string_view normalHeightMapPattern = {}, bool autoUseNormalMaps = false, + std::string_view specularMapPattern = {}, bool autoUseSpecularMaps = false); ~TerrainStorage(); osg::ref_ptr getLand(ESM::ExteriorCellLocation cellLocation) override; - const ESM::LandTexture* getLandTexture(int index, short plugin) override; + const ESM::LandTexture* getLandTexture(std::uint16_t index, int plugin) override; bool hasData(ESM::ExteriorCellLocation cellLocation) override; diff --git a/components/esmterrain/storage.cpp b/components/esmterrain/storage.cpp index bb44d836de..a4ff3e6aaa 100644 --- a/components/esmterrain/storage.cpp +++ b/components/esmterrain/storage.cpp @@ -103,8 +103,8 @@ namespace ESMTerrain const float defaultHeight = ESM::Land::DEFAULT_HEIGHT; - Storage::Storage(const VFS::Manager* vfs, const std::string& normalMapPattern, - const std::string& normalHeightMapPattern, bool autoUseNormalMaps, const std::string& specularMapPattern, + Storage::Storage(const VFS::Manager* vfs, std::string_view normalMapPattern, + std::string_view normalHeightMapPattern, bool autoUseNormalMaps, std::string_view specularMapPattern, bool autoUseSpecularMaps) : mVFS(vfs) , mNormalMapPattern(normalMapPattern) @@ -368,23 +368,21 @@ namespace ESMTerrain std::string Storage::getTextureName(UniqueTextureId id) { - static constexpr char defaultTexture[] = "textures\\_land_default.dds"; - if (id.first == 0) - return defaultTexture; // Not sure if the default texture really is hardcoded? - - // NB: All vtex ids are +1 compared to the ltex ids - const ESM::LandTexture* ltex = getLandTexture(id.first - 1, id.second); - if (!ltex) + std::string_view texture = "_land_default.dds"; + if (id.first != 0) { - Log(Debug::Warning) << "Warning: Unable to find land texture index " << id.first - 1 << " in plugin " - << id.second << ", using default texture instead"; - return defaultTexture; + // NB: All vtex ids are +1 compared to the ltex ids + const ESM::LandTexture* ltex = getLandTexture(id.first - 1, id.second); + if (ltex) + texture = ltex->mTexture; + else + { + Log(Debug::Warning) << "Warning: Unable to find land texture index " << id.first - 1 << " in plugin " + << id.second << ", using default texture instead"; + } } - // this is needed due to MWs messed up texture handling - std::string texture = Misc::ResourceHelpers::correctTexturePath(ltex->mTexture, mVFS); - - return texture; + return Misc::ResourceHelpers::correctTexturePath(texture, mVFS); } void Storage::getBlendmaps(float chunkSize, const osg::Vec2f& chunkCenter, ImageVector& blendmaps, @@ -417,21 +415,21 @@ namespace ESMTerrain sampleBlendmaps(chunkSize, origin.x(), origin.y(), ESM::Land::LAND_TEXTURE_SIZE, handleSample); - std::map textureIndicesMap; + std::map textureIndicesMap; for (std::size_t y = 0; y < blendmapSize; ++y) { for (std::size_t x = 0; x < blendmapSize; ++x) { const UniqueTextureId id = textureIds[y * blendmapSize + x]; - std::map::iterator found = textureIndicesMap.find(id); + auto found = textureIndicesMap.find(id); if (found == textureIndicesMap.end()) { - unsigned int layerIndex = layerList.size(); + std::size_t layerIndex = layerList.size(); Terrain::LayerInfo info = getLayerInfo(getTextureName(id)); // look for existing diffuse map, which may be present when several plugins use the same texture - for (unsigned int i = 0; i < layerList.size(); ++i) + for (std::size_t i = 0; i < layerList.size(); ++i) { if (layerList[i].mDiffuseMap == info.mDiffuseMap) { @@ -452,7 +450,7 @@ namespace ESMTerrain layerList.push_back(std::move(info)); } } - const unsigned int layerIndex = found->second; + const std::size_t layerIndex = found->second; unsigned char* const data = blendmaps[layerIndex]->data(); const std::size_t realY = (blendmapSize - y - 1) * imageScaleFactor; const std::size_t realX = x * imageScaleFactor; diff --git a/components/esmterrain/storage.hpp b/components/esmterrain/storage.hpp index 5ebd0a9d37..6d388e6e8c 100644 --- a/components/esmterrain/storage.hpp +++ b/components/esmterrain/storage.hpp @@ -67,13 +67,13 @@ namespace ESMTerrain class Storage : public Terrain::Storage { public: - Storage(const VFS::Manager* vfs, const std::string& normalMapPattern = "", - const std::string& normalHeightMapPattern = "", bool autoUseNormalMaps = false, - const std::string& specularMapPattern = "", bool autoUseSpecularMaps = false); + Storage(const VFS::Manager* vfs, std::string_view normalMapPattern = {}, + std::string_view normalHeightMapPattern = {}, bool autoUseNormalMaps = false, + std::string_view specularMapPattern = {}, bool autoUseSpecularMaps = false); // Not implemented in this class, because we need different Store implementations for game and editor virtual osg::ref_ptr getLand(ESM::ExteriorCellLocation cellLocation) = 0; - virtual const ESM::LandTexture* getLandTexture(int index, short plugin) = 0; + virtual const ESM::LandTexture* getLandTexture(std::uint16_t index, int plugin) = 0; /// Get bounds of the whole terrain in cell units void getBounds(float& minX, float& maxX, float& minY, float& maxY, ESM::RefId worldspace) override = 0;