From 27d0fe2f7d4c3d7b2a650f869a9a7d566c46b2d7 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 30 Jun 2018 11:09:00 +0500 Subject: Tree-based asset management --- src/AssetManager.cpp | 32 +++++++++++++++++++++++++++++--- src/AssetManager.hpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index f5284a4..28d6050 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -11,7 +11,7 @@ namespace fs = std::experimental::filesystem::v1; -//const fs::path pathToAssets = "./assets/"; +const fs::path pathToAssets = "./assets/"; //const fs::path pathToAssetsList = "./items.json"; //const fs::path pathToTextureIndex = "./textures.json"; const std::string pathToAssetsList = "./items.json"; @@ -20,10 +20,11 @@ const std::string pathToTextureIndex = "./textures.json"; const fs::path pathToModels = "./assets/minecraft/models/"; AssetManager::AssetManager() { + LoadAssets(); LoadIds(); LoadTextureResources(); LoadBlockModels(); - ParseBlockModels(); + ParseBlockModels(); } void AssetManager::LoadIds() { @@ -536,4 +537,29 @@ void AssetManager::ParseBlockModels() { } } } -} \ No newline at end of file +} + +std::unique_ptr ParseAsset(const fs::path &file) { + return std::unique_ptr(); +} + +void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { + for (auto &file : fs::directory_iterator(dirEntry)) { + node->childs.push_back(std::make_unique()); + AssetTreeNode *fileNode = node->childs.back().get(); + fileNode->parent = node; + fileNode->name = file.path().stem().string(); + if (fs::is_directory(file)) { + WalkDirEntry(file, fileNode); + } + else { + fileNode->asset = ParseAsset(file); + } + } +} + +void AssetManager::LoadAssets() { + assetTree = std::make_unique(); + assetTree->name = "/"; + WalkDirEntry(fs::directory_entry(pathToAssets), assetTree.get()); +} diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index c386f7c..5273a2a 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -142,6 +142,17 @@ inline bool operator==(const BlockModel::ElementData::FaceData::Uv &lhs, return lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1 && lhs.x2 == rhs.x2 && lhs.y2 == rhs.y2; } +struct Asset { + virtual ~Asset(); +}; + +struct AssetTreeNode { + std::vector> childs; + std::string name; + AssetTreeNode *parent; + std::unique_ptr asset; +}; + class AssetManager { Texture *textureAtlas; std::map assetIds; @@ -149,6 +160,7 @@ class AssetManager { std::map textureAtlasIndexes; std::map models; std::map blockIdToBlockName; + std::unique_ptr assetTree; public: AssetManager(); @@ -177,4 +189,27 @@ public: std::string GetAssetNameByBlockId(BlockId block); void ParseBlockModels(); + + void LoadAssets(); + + template + T *GetAsset(const std::string &assetName) { + AssetTreeNode *node = assetTree.get(); + unsigned int pos = 1; + unsigned int prevPos = 1; + size_t x = assetName.size(); + while (pos < assetName.size()) { + for (; assetName[pos] != '/' && pos < assetName.size(); pos++); + std::string dirName = assetName.substr(prevPos, pos - prevPos); + for (const auto &asset : node->childs) { + if (asset->name == dirName) { + node = asset.get(); + break; + } + } + pos++; + prevPos = pos; + } + return dynamic_cast(node->asset.get()); + } }; -- cgit v1.2.3