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

147 lines
3.6 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"
#include "Material/Material.h"
#include "Graphics/Graphics.h"
2021-01-19 15:30:00 +01:00
#include "Window/WindowManager.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
|| extension.compare(".obj") == 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
}
2020-06-08 01:44:47 +02:00
if (extension.compare(".semat") == 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)
{
return get().meshes[filePath];
}
2020-10-03 11:00:10 +02:00
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
PTextureAsset result = get().textures[filePath];
if(result == nullptr)
{
return get().textureLoader->getPlaceholderTexture();
}
return result;
}
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
{
materialLoader->queueAsset(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;
}
}
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
}