Files
Seele/src/Engine/Asset/AssetRegistry.cpp
T

152 lines
3.8 KiB
C++
Raw Normal View History

2020-06-02 11:46:18 +02:00
#include "AssetRegistry.h"
#include "MeshAsset.h"
#include "TextureAsset.h"
#include "TextureLoader.h"
#include "MaterialLoader.h"
#include "MeshLoader.h"
2021-10-19 23:04:38 +02:00
#include "Material/MaterialAsset.h"
#include "Graphics/Mesh.h"
2020-06-02 11:46:18 +02:00
#include "Graphics/Graphics.h"
2021-01-19 15:30:00 +01:00
#include "Window/WindowManager.h"
2021-04-01 16:40:14 +02:00
#include "MeshAsset.h"
2020-08-11 22:38:19 +02:00
#include <iostream>
2020-06-02 11:46:18 +02:00
using namespace Seele;
AssetRegistry::~AssetRegistry()
{
}
void AssetRegistry::init(const std::string& rootFolder)
{
get().init(rootFolder, WindowManager::getGraphics());
}
void AssetRegistry::importFile(const std::string &filePath)
{
2020-06-08 01:44:47 +02:00
std::filesystem::path fsPath = std::filesystem::path(filePath);
std::string extension = fsPath.extension().string();
if (extension.compare(".fbx") == 0
2022-03-19 22:45:30 +01:00
|| extension.compare(".obj") == 0
|| extension.compare(".blend") == 0)
2020-06-02 11:46:18 +02:00
{
2020-09-19 14:36:50 +02:00
get().importMesh(fsPath);
2020-06-02 11:46:18 +02:00
}
2020-06-08 01:44:47 +02:00
if (extension.compare(".png") == 0
|| extension.compare(".jpg") == 0)
2020-06-02 11:46:18 +02:00
{
2020-09-19 14:36:50 +02:00
get().importTexture(fsPath);
2020-06-02 11:46:18 +02:00
}
2021-06-12 18:51:29 +02:00
if (extension.compare(".asset") == 0)
2020-06-02 11:46:18 +02:00
{
2020-09-19 14:36:50 +02:00
get().importMaterial(fsPath);
2020-06-02 11:46:18 +02:00
}
}
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
2021-04-13 23:09:16 +02:00
auto it = get().meshes.find(filePath);
assert(it != get().meshes.end());
2021-04-25 23:43:40 +02:00
return it->second;
2020-06-02 11:46:18 +02:00
}
2020-10-03 11:00:10 +02:00
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
2021-04-25 23:43:40 +02:00
return get().textures[filePath];
2020-10-03 11:00:10 +02:00
}
2020-06-02 11:46:18 +02:00
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
{
return get().materials[filePath];
}
2020-09-19 14:36:50 +02:00
std::ofstream AssetRegistry::createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return get().internalCreateWriteStream(relativePath, openmode);
}
std::ifstream AssetRegistry::createReadStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
return get().internalCreateReadStream(relativePath, openmode);
}
2020-06-02 11:46:18 +02:00
AssetRegistry &AssetRegistry::get()
{
static AssetRegistry instance;
return instance;
}
AssetRegistry::AssetRegistry()
{
}
2020-06-08 01:44:47 +02:00
void AssetRegistry::init(const std::filesystem::path &rootFolder, Gfx::PGraphics graphics)
2020-06-02 11:46:18 +02:00
{
AssetRegistry &reg = get();
reg.rootFolder = rootFolder;
reg.meshLoader = new MeshLoader(graphics);
reg.textureLoader = new TextureLoader(graphics);
reg.materialLoader = new MaterialLoader(graphics);
}
2020-09-19 14:36:50 +02:00
std::string AssetRegistry::getRootFolder()
{
return get().rootFolder.generic_string();
}
void AssetRegistry::importMesh(const std::filesystem::path &filePath)
2020-06-02 11:46:18 +02:00
{
meshLoader->importAsset(filePath);
}
2020-09-19 14:36:50 +02:00
void AssetRegistry::importTexture(const std::filesystem::path &filePath)
2020-06-02 11:46:18 +02:00
{
textureLoader->importAsset(filePath);
}
2020-09-19 14:36:50 +02:00
void AssetRegistry::importMaterial(const std::filesystem::path &filePath)
2020-06-02 11:46:18 +02:00
{
2021-11-11 20:12:50 +01:00
materialLoader->importAsset(filePath);
2020-09-19 14:36:50 +02:00
}
void AssetRegistry::registerMesh(PMeshAsset mesh)
{
PMeshAsset existingMesh = meshes[mesh->getFileName()];
if(existingMesh != nullptr)
{
auto newMeshes = mesh->getMeshes();
for(uint32 i = 0; i < newMeshes.size(); ++i)
{
existingMesh->addMesh(newMeshes[i]);
}
}
else
{
meshes[mesh->getFileName()] = mesh;
}
}
2021-10-15 23:12:29 +02:00
void AssetRegistry::registerTexture(PTextureAsset texture)
{
textures[texture->getFileName()] = texture;
}
2020-10-03 11:00:10 +02:00
void AssetRegistry::registerMaterial(PMaterialAsset material)
{
materials[material->getFileName()] = material;
}
2020-09-19 14:36:50 +02:00
std::ofstream AssetRegistry::internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
2021-01-19 15:30:00 +01:00
auto fullPath = rootFolder;
fullPath.append(relativePath);
return std::ofstream(fullPath.string(), openmode);
2020-09-19 14:36:50 +02:00
}
std::ifstream AssetRegistry::internalCreateReadStream(const std::string& relativePath, std::ios_base::openmode openmode)
{
2021-01-19 15:30:00 +01:00
auto fullPath = rootFolder;
fullPath.append(relativePath);
return std::ifstream(fullPath.string(), openmode);
2020-09-19 14:36:50 +02:00
}