From e8ae429ac0d4f12ecdad6088605d81de6cf67cce Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 9 Mar 2018 02:16:21 +0500 Subject: Some parsing optimizations --- src/AssetManager.cpp | 7 ++++--- src/AssetManager.hpp | 2 +- src/RendererSectionData.cpp | 13 ++++++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 0561992..8403700 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -69,9 +69,10 @@ void AssetManager::LoadTextureResources() { LOG(INFO) << "Texture atlas id is " << textureAtlas->texture; } -TextureCoordinates AssetManager::GetTextureByAssetName(std::string AssetName) { - if (assetTextures.find(AssetName) != assetTextures.end()) - return assetTextures[AssetName]; +TextureCoordinates AssetManager::GetTextureByAssetName(const std::string &AssetName) { + auto it = assetTextures.find(AssetName); + if (it != assetTextures.end()) + return it->second; else return TextureCoordinates{-1, -1, -1, -1}; } diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index b828ee1..37031be 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -145,7 +145,7 @@ public: void LoadTextureResources(); - TextureCoordinates GetTextureByAssetName(std::string AssetName); + TextureCoordinates GetTextureByAssetName(const std::string &AssetName); std::string GetTextureAssetNameByBlockId(BlockTextureId block); diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index b233522..1ed1099 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -16,7 +16,7 @@ inline const BlockId& GetBlockId(int x, int y, int z, const std::array §ionsList, World *world, Vector blockPos, const BlockModel &model, glm::mat4 transform, unsigned char light, unsigned char skyLight, const std::array& visibility, RendererSectionData &data) { +void AddFacesByBlockModel(const std::vector §ionsList, World *world, Vector blockPos, const BlockModel &model, glm::mat4 transform, unsigned char light, unsigned char skyLight, const std::array& visibility, std::string &textureName, RendererSectionData &data) { glm::mat4 elementTransform, faceTransform; for (const auto& element : model.Elements) { Vector t = element.to - element.from; @@ -114,11 +114,13 @@ void AddFacesByBlockModel(const std::vector §ionsList, World *world, break; } data.models.push_back(faceTransform); - std::string textureName = face.second.texture; + textureName = face.second.texture; while (textureName[0] == '#') { - textureName = model.Textures.find(std::string(textureName.begin() + 1, textureName.end()))->second; + textureName.erase(0, 1); + textureName = model.Textures.find(textureName)->second; } - glm::vec4 texture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/" + textureName); + textureName.insert(0, "minecraft/textures/"); + glm::vec4 texture = AssetManager::Instance().GetTextureByAssetName(textureName); if (!(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,16,0,16 }) && !(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,0,0,0 }) && !(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,0,16,16 })) { @@ -262,6 +264,7 @@ RendererSectionData ParseSection(World * world, Vector sectionPosition) std::vector> idModels; std::array blockIdData = SetBlockIdData(world, sectionPosition); std::array blockVisibility = GetBlockVisibilityData(world, sectionPosition, blockIdData, idModels); + std::string textureName; const std::map &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes(); const Section §ion = world->GetSection(sectionPosition); @@ -286,7 +289,7 @@ RendererSectionData ParseSection(World * world, Vector sectionPosition) const BlockModel* model = GetInternalBlockModel(block, idModels); if (model) { - AddFacesByBlockModel(sectionsList, world, Vector(x, y, z), *model, transform, section.GetBlockLight(Vector(x, y, z)), section.GetBlockSkyLight(Vector(x, y, z)), blockVisibility, data); + AddFacesByBlockModel(sectionsList, world, Vector(x, y, z), *model, transform, section.GetBlockLight(Vector(x, y, z)), section.GetBlockSkyLight(Vector(x, y, z)), blockVisibility, textureName, data); } else { transform = glm::translate(transform, glm::vec3(0, 1, 0)); -- cgit v1.2.3