Loading...
Searching...
No Matches
AssetManager.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 08/12/2025.
3//
4
5#include "AssetManager.h"
6
7#include "hellfire/serializers/MaterialSerializer.h"
8#include "hellfire/serializers/MeshSerializer.h"
9
10namespace hellfire {
12
13 std::shared_ptr<Mesh> AssetManager::get_mesh(AssetID id) {
14 // Check cache
15 if (auto it = mesh_cache_.find(id); it != mesh_cache_.end()) {
16 return it->second;
17 }
18
19 // Load from disk
20 auto meta = registry_.get_asset(id);
21 if (!meta || meta->type != AssetType::MESH) {
22 std::cerr << "Invalid mesh asset: " << id << std::endl;
23 return nullptr;
24 }
25
27 if (!mesh) {
28 std::cerr << "Failed to load mesh: " << std::endl;
29 return nullptr;
30 }
31
32 if (mesh && !mesh->is_built()) {
33 mesh->build();
34 }
35
36 mesh_cache_[id] = mesh;
37 return mesh;
38 }
39
40 std::shared_ptr<Material> AssetManager::get_material(AssetID id) {
41 if (auto it = material_cache_.find(id); it != material_cache_.end()) {
42 return it->second;
43 }
44
45 auto meta = registry_.get_asset(id);
46 if (!meta || meta->type != AssetType::MATERIAL) {
47 return nullptr;
48 }
49
51 if (!data) {
52 return nullptr;
53 }
54
55 // Convert MaterialData to Material, loading textures
56 auto material = MaterialBuilder::create(data->name);
57 material->set_diffuse_color(data->diffuse_color);
58 material->set_ambient_color(data->ambient_color);
59 material->set_specular_color(data->specular_color);
60 material->set_emissive_color(data->emissive_color);
61 material->set_opacity(data->opacity);
62 material->set_shininess(data->shininess);
63 material->set_metallic(data->metallic);
64 material->set_roughness(data->roughness);
65
66 for (const auto &tex_id: data->texture_assets | std::views::values) {
67 if (auto tex = get_texture(tex_id)) {
68 material->set_texture(tex, 0);
69 }
70 }
71
72 material_cache_[id] = material;
73 return material;
74 }
75
76 std::shared_ptr<Texture> AssetManager::get_texture(AssetID id) {
77 if (auto it = texture_cache_.find(id); it != texture_cache_.end()) {
78 return it->second;
79 }
80
81 auto meta = registry_.get_asset(id);
82 if (!meta || meta->type != AssetType::TEXTURE) {
83 return nullptr;
84 }
85
86 auto texture = std::make_shared<Texture>(
88 );
89
90 if (!texture->is_valid()) {
91 return nullptr;
92 }
93
94 texture_cache_[id] = texture;
95 return texture;
96 }
97
98 void AssetManager::unload(AssetID id) {
99 mesh_cache_.erase(id);
100 material_cache_.erase(id);
101 texture_cache_.erase(id);
102 }
103
105 mesh_cache_.clear();
106 material_cache_.clear();
107 texture_cache_.clear();
108 }
109
111 for (const AssetID id : registry_.get_modified_assets()) {
112 unload(id);
113 }
115 }
116} // hellfire
std::shared_ptr< Texture > get_texture(AssetID id)
AssetManager(AssetRegistry &registry)
std::shared_ptr< Mesh > get_mesh(AssetID id)
void unload(AssetID id)
AssetRegistry & registry_
std::shared_ptr< Material > get_material(AssetID id)
Registry for storing assets.
std::filesystem::path get_absolute_path(AssetID uuid)
static std::optional< MaterialData > load(const std::filesystem::path &filepath)
static std::shared_ptr< Mesh > load(const std::filesystem::path &filepath)