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;
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
Scope<ResourceManager> ResourceManager::create()
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
return make_scope(new ResourceManager());
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
ResourceManager::ResourceManager(): m_shaders {}, m_textures {}
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_assert(!s_Context, "Repeated singleton construction");
|
2025-07-05 13:28:41 +03:30
|
|
|
s_Context = this;
|
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void ResourceManager::load_shader_impl(
|
2025-07-05 13:28:41 +03:30
|
|
|
const std::string &name,
|
|
|
|
const std::string &vertexPath,
|
|
|
|
const std::string &pixelPath
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// check
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_assert(s_Context, "Uninitliazed singleton");
|
|
|
|
lt_assert(!vertexPath.empty(), "Empty 'vertexPath'");
|
|
|
|
lt_assert(!pixelPath.empty(), "Empty 'pixelPath'");
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// load files
|
2025-07-05 15:36:53 +03:30
|
|
|
basic_file_handle vertexFile = FileManager::read_text_file(vertexPath);
|
|
|
|
basic_file_handle pixelFile = FileManager::read_text_file(pixelPath);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// check
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_assert(vertexFile.is_valid(), "Failed to read vertex file: {}", vertexPath);
|
|
|
|
lt_assert(pixelFile.is_valid(), "Failed to read vertex file: {}", pixelPath);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// create shader
|
2025-07-05 14:23:01 +03:30
|
|
|
m_shaders[name] = Ref<Shader>(
|
2025-07-05 15:36:53 +03:30
|
|
|
Shader::create(vertexFile, pixelFile, GraphicsContext::get_shared_context())
|
2025-07-05 13:28:41 +03:30
|
|
|
);
|
|
|
|
|
|
|
|
// free file
|
2025-07-05 15:36:53 +03:30
|
|
|
vertexFile.release();
|
|
|
|
pixelFile.release();
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void ResourceManager::load_texture_impl(
|
2025-07-05 13:28:41 +03:30
|
|
|
const std::string &name,
|
|
|
|
const std::string &path,
|
|
|
|
unsigned int desiredComponents /* = 4u */
|
|
|
|
)
|
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_assert(s_Context, "Uninitliazed singleton");
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// load file
|
2025-07-05 15:36:53 +03:30
|
|
|
image_file_handle imgFile = FileManager::read_image_file(path, desiredComponents);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
|
|
|
// create texture
|
2025-07-05 15:36:53 +03:30
|
|
|
m_textures[name] = Ref<Texture>(Texture::create(
|
|
|
|
imgFile.get_width(),
|
|
|
|
imgFile.get_height(),
|
|
|
|
imgFile.get_components(),
|
2025-07-05 13:28:41 +03:30
|
|
|
imgFile.GetData(),
|
2025-07-05 15:36:53 +03:30
|
|
|
GraphicsContext::get_shared_context(),
|
2025-07-05 13:28:41 +03:30
|
|
|
path
|
|
|
|
));
|
|
|
|
|
|
|
|
// free file
|
2025-07-05 15:36:53 +03:30
|
|
|
imgFile.release();
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void ResourceManager::release_texture_impl(const std::string &name)
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
if (!m_textures[name])
|
2025-07-05 13:28:41 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(warn, "Failed to find texture named: {}", name);
|
2025-07-05 13:28:41 +03:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_textures[name] = nullptr;
|
2025-07-05 13:28:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Light
|