light/modules/engine/src/utils/resource_manager.cpp

88 lines
2.1 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/graphics/graphics_context.hpp>
#include <engine/graphics/shader.hpp>
#include <engine/graphics/texture.hpp>
#include <engine/utils/file_manager.hpp>
#include <engine/utils/resource_manager.hpp>
namespace Light {
ResourceManager *ResourceManager::s_Context = nullptr;
Scope<ResourceManager> ResourceManager::Create()
{
return MakeScope(new ResourceManager());
}
ResourceManager::ResourceManager(): m_shaders {}, m_textures {}
2025-07-05 13:28:41 +03:30
{
ASSERT(!s_Context, "Repeated singleton construction");
s_Context = this;
}
void ResourceManager::LoadShaderImpl(
const std::string &name,
const std::string &vertexPath,
const std::string &pixelPath
)
{
// check
ASSERT(s_Context, "Uninitliazed singleton");
ASSERT(!vertexPath.empty(), "Empty 'vertexPath'");
ASSERT(!pixelPath.empty(), "Empty 'pixelPath'");
// load files
BasicFileHandle vertexFile = FileManager::ReadTextFile(vertexPath);
BasicFileHandle pixelFile = FileManager::ReadTextFile(pixelPath);
// check
ASSERT(vertexFile.IsValid(), "Failed to read vertex file: {}", vertexPath);
ASSERT(pixelFile.IsValid(), "Failed to read vertex file: {}", pixelPath);
// create shader
m_shaders[name] = Ref<Shader>(
2025-07-05 13:28:41 +03:30
Shader::Create(vertexFile, pixelFile, GraphicsContext::GetSharedContext())
);
// free file
vertexFile.Release();
pixelFile.Release();
}
void ResourceManager::LoadTextureImpl(
const std::string &name,
const std::string &path,
unsigned int desiredComponents /* = 4u */
)
{
ASSERT(s_Context, "Uninitliazed singleton");
// load file
ImageFileHandle imgFile = FileManager::ReadImageFile(path, desiredComponents);
// create texture
m_textures[name] = Ref<Texture>(Texture::Create(
2025-07-05 13:28:41 +03:30
imgFile.GetWidth(),
imgFile.GetHeight(),
imgFile.GetComponents(),
imgFile.GetData(),
GraphicsContext::GetSharedContext(),
path
));
// free file
imgFile.Release();
}
void ResourceManager::ReleaseTextureImpl(const std::string &name)
{
if (!m_textures[name])
2025-07-05 13:28:41 +03:30
{
LOG(warn, "Failed to find texture named: {}", name);
return;
}
m_textures[name] = nullptr;
2025-07-05 13:28:41 +03:30
}
} // namespace Light