Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
ResourceManager.h
Ir a la documentación de este archivo.
1
6#pragma once
7#include "Prerequisites.h"
8#include "IResource.h"
9
10class
12public:
13 ResourceManager() = default;
14 ~ResourceManager() = default;
15
16 // Singleton
18 static ResourceManager instance;
19 return instance;
20 }
21
24
26 template<typename T, typename... Args>
27 std::shared_ptr<T> GetOrLoad(const std::string& key,
28 const std::string& filename,
29 Args&&... args) {
30 static_assert(std::is_base_of<IResource, T>::value,
31 "T debe heredar de IResource");
32 // 1. ¿Ya existe el recurso en el caché?
33 auto it = m_resources.find(key);
34 if (it != m_resources.end()) {
35 // Intentar castear al tipo correcto
36 auto existing = std::dynamic_pointer_cast<T>(it->second);
37 if (existing && existing->GetState() == ResourceState::Loaded) {
38 return existing; // Flyweight: reutilizamos la instancia
39 }
40 }
41
42 // 2. No existe o no está cargado -> crearlo y cargarlo
43 std::shared_ptr<T> resource = std::make_shared<T>(key, std::forward<Args>(args)...);
44
45 if (!resource->load(filename)) {
46 // Puedes manejar errores más fino aquí
47 return nullptr;
48 }
49
50 if (!resource->init()) {
51 return nullptr;
52 }
53
54 // 3. Guardar en el caché y devolver
55 m_resources[key] = resource;
56 return resource;
57 }
58
60 template<typename T>
61 std::shared_ptr<T> Get(const std::string& key) const
62 {
63 auto it = m_resources.find(key);
64 if (it == m_resources.end()) return nullptr;
65
66 return std::dynamic_pointer_cast<T>(it->second);
67 }
68
70 void Unload(const std::string& key)
71 {
72 auto it = m_resources.find(key);
73 if (it != m_resources.end()) {
74 it->second->unload();
75 m_resources.erase(it);
76 }
77 }
78
80 void UnloadAll()
81 {
82 for (auto& [key, res] : m_resources) {
83 if (res) {
84 res->unload();
85 }
86 }
87 m_resources.clear();
88 }
89
90private:
91 std::unordered_map<std::string, std::shared_ptr<IResource>> m_resources;
92};
93
Declara la API de IResource dentro del subsistema Core.
Declara la API de Prerequisites dentro del subsistema Core.
ResourceManager()=default
void Unload(const std::string &key)
Liberar un recurso específico.
std::shared_ptr< T > GetOrLoad(const std::string &key, const std::string &filename, Args &&... args)
Obtener o cargar un recurso de tipo T (T debe heredar de IResource).
~ResourceManager()=default
std::shared_ptr< T > Get(const std::string &key) const
Obtener un recurso ya cargado, sin cargarlo si no existe.
ResourceManager(const ResourceManager &)=delete
ResourceManager & operator=(const ResourceManager &)=delete
void UnloadAll()
Liberar todos los recursos.
static ResourceManager & getInstance()
std::unordered_map< std::string, std::shared_ptr< IResource > > m_resources