mirror of
https://github.com/ApfelTeeSaft/vb01.git
synced 2026-08-26 19:33:45 +00:00
Corrected individual asset path reading
AssetManagerTest class
This commit is contained in:
+2
-2
@@ -27,7 +27,6 @@ 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/tinydir)
|
||||||
include_directories(external/glm)
|
include_directories(external/glm)
|
||||||
include_directories(external/glm/glm)
|
include_directories(external/glm/glm)
|
||||||
@@ -88,7 +87,8 @@ if(BUILD_TESTS)
|
|||||||
set(RENDER_TEST nodeTest.cpp cameraTest.cpp shaderTest.cpp particleEmitterTest.cpp)
|
set(RENDER_TEST nodeTest.cpp cameraTest.cpp shaderTest.cpp particleEmitterTest.cpp)
|
||||||
set(ARMATURE_TEST boneTest.cpp ikSolverTest.cpp)
|
set(ARMATURE_TEST boneTest.cpp ikSolverTest.cpp)
|
||||||
set(ANIMATION_TEST animationChannelTest.cpp)
|
set(ANIMATION_TEST animationChannelTest.cpp)
|
||||||
set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST})
|
set(ASSET_MANAGER_TEST assetManagerTest.cpp)
|
||||||
|
set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST} ${ASSET_MANAGER_TEST})
|
||||||
|
|
||||||
add_executable(vb01Tests ${TEST_SRC})
|
add_executable(vb01Tests ${TEST_SRC})
|
||||||
target_link_libraries(vb01Tests ${LIB_NAME} ${DEPS} cppunit)
|
target_link_libraries(vb01Tests ${LIB_NAME} ${DEPS} cppunit)
|
||||||
|
|||||||
+31
-18
@@ -8,6 +8,7 @@
|
|||||||
#include <tinydir.h>
|
#include <tinydir.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -23,46 +24,58 @@ namespace vb01{
|
|||||||
|
|
||||||
void AssetManager::readDir(string path, vector<string> &contents, bool recursive){
|
void AssetManager::readDir(string path, vector<string> &contents, bool recursive){
|
||||||
tinydir_dir dir;
|
tinydir_dir dir;
|
||||||
tinydir_open_sorted(&dir, path.c_str());
|
|
||||||
|
if(tinydir_open_sorted(&dir, path.c_str()) == -1){
|
||||||
|
contents.push_back(path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < dir.n_files; i++){
|
for(int i = 0; i < dir.n_files; i++){
|
||||||
tinydir_file file;
|
tinydir_file file;
|
||||||
tinydir_readfile_n(&dir, &file, i);
|
tinydir_readfile_n(&dir, &file, i);
|
||||||
|
|
||||||
|
if(file.name[0] == '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
if(file.is_dir && recursive)
|
if(file.is_dir && recursive)
|
||||||
readDir(path + file.name, contents, recursive);
|
readDir(path + file.name + "/", contents, recursive);
|
||||||
else if(!file.is_dir)
|
else if(!file.is_dir)
|
||||||
contents.push_back(file.name);
|
contents.push_back(path + file.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
tinydir_close(&dir);
|
tinydir_close(&dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::load(string path, bool recursive){
|
void AssetManager::load(string path, bool recursive){
|
||||||
if(getAsset(path))
|
|
||||||
return;
|
|
||||||
|
|
||||||
vector<string> files;
|
vector<string> files;
|
||||||
readDir(path, files, recursive);
|
readDir(path, files, recursive);
|
||||||
|
|
||||||
string format = path.substr(path.find_last_of(".") + 1, string::npos);
|
for(string file : files){
|
||||||
AbstractAssetReader *assetReader = nullptr;
|
if(getAsset(file))
|
||||||
vector<string> imageFormats = vector<string>{"png", "jpeg", "jpg"};
|
continue;
|
||||||
|
|
||||||
if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end())
|
string format = file.substr(file.find_last_of(".") + 1, string::npos);
|
||||||
assetReader = ImageReader::getSingleton();
|
AbstractAssetReader *assetReader = nullptr;
|
||||||
|
vector<string> imageFormats = vector<string>{"png", "jpeg", "jpg"};
|
||||||
|
|
||||||
vector<string> fontFormats = vector<string>{"ttf"};
|
if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end())
|
||||||
|
assetReader = ImageReader::getSingleton();
|
||||||
|
|
||||||
if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end())
|
vector<string> fontFormats = vector<string>{"ttf"};
|
||||||
assetReader = FontReader::getSingleton();
|
|
||||||
|
|
||||||
vector<string> modelFormats = vector<string>{"xml"};
|
if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end())
|
||||||
|
assetReader = FontReader::getSingleton();
|
||||||
|
|
||||||
if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end())
|
vector<string> modelFormats = vector<string>{"xml"};
|
||||||
assetReader = XmlModelReader::getSingleton();
|
|
||||||
|
|
||||||
assets.push_back(assetReader->readAsset(path));
|
if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end())
|
||||||
|
assetReader = XmlModelReader::getSingleton();
|
||||||
|
|
||||||
|
if(assetReader)
|
||||||
|
assets.push_back(assetReader->readAsset(file));
|
||||||
|
else
|
||||||
|
cout << "No asset reader for " << file << endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Asset* AssetManager::getAsset(string path){
|
Asset* AssetManager::getAsset(string path){
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace vb01{
|
|||||||
static AssetManager* getSingleton();
|
static AssetManager* getSingleton();
|
||||||
void load(std::string, bool = false);
|
void load(std::string, bool = false);
|
||||||
Asset* getAsset(std::string);
|
Asset* getAsset(std::string);
|
||||||
|
inline std::vector<Asset*> getAssets(){return assets;}
|
||||||
private:
|
private:
|
||||||
AssetManager(){}
|
AssetManager(){}
|
||||||
void readDir(std::string, std::vector<std::string>&, bool);
|
void readDir(std::string, std::vector<std::string>&, bool);
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#include "assetManagerTest.h"
|
||||||
|
#include "assetManager.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace vb01{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void AssetManagerTest::setUp(){
|
||||||
|
assetManager = AssetManager::getSingleton();
|
||||||
|
|
||||||
|
string basePath = "../tests/";
|
||||||
|
recursiveLoadPath = basePath + "recursiveLoad/";
|
||||||
|
fileLoadPath = basePath + "fileLoad/";
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssetManagerTest::tearDown(){
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssetManagerTest::testLoad(){
|
||||||
|
vector<string> recursiveLoadFiles = vector<string>{"folderA/a.png", "folderB/b.png"};
|
||||||
|
assetManager->load(recursiveLoadPath, true);
|
||||||
|
|
||||||
|
for(string file : recursiveLoadFiles){
|
||||||
|
Asset *asset = assetManager->getAsset(recursiveLoadPath + file);
|
||||||
|
CPPUNIT_ASSERT(asset != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
string file = fileLoadPath + "a.png";
|
||||||
|
assetManager->load(file);
|
||||||
|
CPPUNIT_ASSERT(assetManager->getAsset(file) != nullptr);
|
||||||
|
|
||||||
|
assetManager->load(file);
|
||||||
|
vector<Asset*> assets = assetManager->getAssets();
|
||||||
|
int numSameAssets = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < assets.size(); i++)
|
||||||
|
if(assets[i]->path == file)
|
||||||
|
numSameAssets++;
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT(numSameAssets == 1);
|
||||||
|
|
||||||
|
string nonReadableFile = fileLoadPath + "b.abc";
|
||||||
|
assetManager->load(nonReadableFile);
|
||||||
|
CPPUNIT_ASSERT(!assetManager->getAsset(nonReadableFile));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef ASSET_MANAGER_TEST_H
|
||||||
|
#define ASSET_MANAGER_TEST_H
|
||||||
|
|
||||||
|
#include <cppunit/TestFixture.h>
|
||||||
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
namespace vb01{
|
||||||
|
class AssetManager;
|
||||||
|
|
||||||
|
class AssetManagerTest : public CppUnit::TestFixture{
|
||||||
|
CPPUNIT_TEST_SUITE(AssetManagerTest);
|
||||||
|
CPPUNIT_TEST(testLoad);
|
||||||
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
|
public:
|
||||||
|
AssetManagerTest(){}
|
||||||
|
void setUp();
|
||||||
|
void tearDown();
|
||||||
|
void testLoad();
|
||||||
|
private:
|
||||||
|
AssetManager *assetManager = nullptr;
|
||||||
|
std::string recursiveLoadPath, fileLoadPath;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "shaderTest.h"
|
#include "shaderTest.h"
|
||||||
#include "particleEmitterTest.h"
|
#include "particleEmitterTest.h"
|
||||||
#include "ikSolverTest.h"
|
#include "ikSolverTest.h"
|
||||||
|
#include "assetManagerTest.h"
|
||||||
|
|
||||||
using namespace CppUnit;
|
using namespace CppUnit;
|
||||||
using namespace vb01;
|
using namespace vb01;
|
||||||
@@ -23,6 +24,7 @@ int main(){
|
|||||||
runner.addTest(ShaderTest::suite());
|
runner.addTest(ShaderTest::suite());
|
||||||
runner.addTest(ParticleEmitterTest::suite());
|
runner.addTest(ParticleEmitterTest::suite());
|
||||||
runner.addTest(IkSolverTest::suite());
|
runner.addTest(IkSolverTest::suite());
|
||||||
|
runner.addTest(AssetManagerTest::suite());
|
||||||
runner.run();
|
runner.run();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user