reading directories recusively when loading assets

This commit is contained in:
devZoGok
2023-01-02 08:44:17 +02:00
parent b6a6ed1005
commit 300d28bab6
7 changed files with 73 additions and 48 deletions
+3
View File
@@ -13,3 +13,6 @@
[submodule "external/tinyxml2"] [submodule "external/tinyxml2"]
path = external/tinyxml2 path = external/tinyxml2
url = https://github.com/leethomason/tinyxml2 url = https://github.com/leethomason/tinyxml2
[submodule "external/tinydir"]
path = external/tinydir
url = https://github.com/cxong/tinydir
+2
View File
@@ -27,6 +27,8 @@ endif()
set(LIB_SRC ${RENDER} ${MATH} ${UTILS} ${GUI} ${ARMATURE} ${ASSET_MANAGER} ${ASSET_READERS} ${ANIM}) set(LIB_SRC ${RENDER} ${MATH} ${UTILS} ${GUI} ${ARMATURE} ${ASSET_MANAGER} ${ASSET_READERS} ${ANIM})
include_directories(external/tinydir)
include_directories(external/glm) include_directories(external/glm)
include_directories(external/glm/glm) include_directories(external/glm/glm)
include_directories(external/glad) include_directories(external/glad)
-4
View File
@@ -1,4 +0,0 @@
#include "abstractAssetReader.h"
namespace vb01{
}
+6 -6
View File
@@ -4,12 +4,12 @@
#include "asset.h" #include "asset.h"
namespace vb01{ namespace vb01{
class AbstractAssetReader{ class AbstractAssetReader{
public: public:
virtual Asset* readAsset(std::string){return nullptr;} virtual Asset* readAsset(std::string){return nullptr;}
protected: protected:
AbstractAssetReader(){} AbstractAssetReader(){}
}; };
} }
#endif #endif
+50 -28
View File
@@ -5,49 +5,71 @@
#include "fontReader.h" #include "fontReader.h"
#include "xmlModelReader.h" #include "xmlModelReader.h"
#include <tinydir.h>
#include <algorithm> #include <algorithm>
using namespace std; using namespace std;
namespace vb01{ namespace vb01{
AssetManager *assetManager = nullptr; AssetManager *assetManager = nullptr;
AssetManager* AssetManager::getSingleton(){ AssetManager* AssetManager::getSingleton(){
if(!assetManager) if(!assetManager)
assetManager = new AssetManager(); assetManager = new AssetManager();
return assetManager; return assetManager;
}
void AssetManager::readDir(string path, vector<string> &contents, bool recursive){
tinydir_dir dir;
tinydir_open_sorted(&dir, path.c_str());
for(int i = 0; i < dir.n_files; i++){
tinydir_file file;
tinydir_readfile_n(&dir, &file, i);
if(file.is_dir && recursive)
readDir(path + file.name, contents, recursive);
else if(!file.is_dir)
contents.push_back(file.name);
} }
void AssetManager::load(string path){ tinydir_close(&dir);
if(getAsset(path)) }
return;
string format = path.substr(path.find_last_of(".") + 1, string::npos); void AssetManager::load(string path, bool recursive){
AbstractAssetReader *assetReader = nullptr; if(getAsset(path))
vector<string> imageFormats = vector<string>{"png", "jpeg", "jpg"}; return;
if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end()) vector<string> files;
assetReader = ImageReader::getSingleton(); readDir(path, files, recursive);
vector<string> fontFormats = vector<string>{"ttf"}; string format = path.substr(path.find_last_of(".") + 1, string::npos);
AbstractAssetReader *assetReader = nullptr;
vector<string> imageFormats = vector<string>{"png", "jpeg", "jpg"};
if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end()) if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end())
assetReader = FontReader::getSingleton(); assetReader = ImageReader::getSingleton();
vector<string> modelFormats = vector<string>{"xml"}; vector<string> fontFormats = vector<string>{"ttf"};
if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end()) if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end())
assetReader = XmlModelReader::getSingleton(); assetReader = FontReader::getSingleton();
assets.push_back(assetReader->readAsset(path)); vector<string> modelFormats = vector<string>{"xml"};
}
Asset* AssetManager::getAsset(string path){ if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end())
for(Asset *a : assets) assetReader = XmlModelReader::getSingleton();
if(a->path == path)
return a; assets.push_back(assetReader->readAsset(path));
}
return nullptr;
} Asset* AssetManager::getAsset(string path){
for(Asset *a : assets)
if(a->path == path)
return a;
return nullptr;
}
} }
+11 -10
View File
@@ -6,18 +6,19 @@
#include <vector> #include <vector>
namespace vb01{ namespace vb01{
class AbstractAssetReader; class AbstractAssetReader;
class AssetManager{ class AssetManager{
public: public:
static AssetManager* getSingleton(); static AssetManager* getSingleton();
void load(std::string); void load(std::string, bool = false);
Asset* getAsset(std::string); Asset* getAsset(std::string);
private: private:
AssetManager(){} AssetManager(){}
void readDir(std::string, std::vector<std::string>&, bool);
std::vector<Asset*> assets; std::vector<Asset*> assets;
}; };
} }
#endif #endif
Vendored Submodule
+1
Submodule external/tinydir added at 9f866c1ec0