|
|
@@ -6,7 +6,7 @@
|
|
|
#include "material_manager.h"
|
|
|
#include "memory.h"
|
|
|
#include "sort_map.h"
|
|
|
-#include "device.h"
|
|
|
+#include "resource_manager.h"
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
@@ -15,9 +15,9 @@ namespace material_manager
|
|
|
{
|
|
|
static MaterialManager* s_mmgr = NULL;
|
|
|
|
|
|
- void init()
|
|
|
+ void init(ResourceManager& rm)
|
|
|
{
|
|
|
- s_mmgr = CE_NEW(default_allocator(), MaterialManager)();
|
|
|
+ s_mmgr = CE_NEW(default_allocator(), MaterialManager)(rm);
|
|
|
}
|
|
|
|
|
|
void shutdown()
|
|
|
@@ -31,26 +31,48 @@ namespace material_manager
|
|
|
}
|
|
|
} // namespace material_manager
|
|
|
|
|
|
-MaterialManager::MaterialManager()
|
|
|
+MaterialManager::MaterialManager(ResourceManager& rm)
|
|
|
+ : _resource_manager(&rm)
|
|
|
+ , _materials(default_allocator())
|
|
|
{
|
|
|
}
|
|
|
|
|
|
-MaterialId MaterialManager::create_material(StringId64 id)
|
|
|
+MaterialManager::~MaterialManager()
|
|
|
{
|
|
|
- MaterialId new_id = id_table::create(_materials_ids);
|
|
|
- _materials[new_id.index].create((MaterialResource*) device()->resource_manager()->get(MATERIAL_TYPE, id), *this);
|
|
|
- return new_id;
|
|
|
+ const SortMap<StringId64, Material*>::Entry* begin = sort_map::begin(_materials);
|
|
|
+ const SortMap<StringId64, Material*>::Entry* end = sort_map::end(_materials);
|
|
|
+
|
|
|
+ for (; begin != end; ++begin)
|
|
|
+ {
|
|
|
+ begin->pair.second->destroy();
|
|
|
+ CE_DELETE(default_allocator(), begin->pair.second);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void MaterialManager::create_material(StringId64 id)
|
|
|
+{
|
|
|
+ if (sort_map::has(_materials, id))
|
|
|
+ return;
|
|
|
+
|
|
|
+ Material* mat = CE_NEW(default_allocator(), Material);
|
|
|
+ mat->create((MaterialResource*)_resource_manager->get(MATERIAL_TYPE, id), *this);
|
|
|
+
|
|
|
+ sort_map::set(_materials, id, mat);
|
|
|
+ sort_map::sort(_materials);
|
|
|
}
|
|
|
|
|
|
-void MaterialManager::destroy_material(MaterialId id)
|
|
|
+void MaterialManager::destroy_material(StringId64 id)
|
|
|
{
|
|
|
- _materials[id.index].destroy();
|
|
|
- id_table::destroy(_materials_ids, id);
|
|
|
+ Material* mat = sort_map::get(_materials, id, (Material*)NULL);
|
|
|
+ mat->destroy();
|
|
|
+ CE_DELETE(default_allocator(), mat);
|
|
|
+ sort_map::remove(_materials, id);
|
|
|
+ sort_map::sort(_materials);
|
|
|
}
|
|
|
|
|
|
-Material* MaterialManager::lookup_material(MaterialId id)
|
|
|
+Material* MaterialManager::lookup_material(StringId64 id)
|
|
|
{
|
|
|
- return &_materials[id.index];
|
|
|
+ return sort_map::get(_materials, id, (Material*)NULL);
|
|
|
}
|
|
|
|
|
|
} // namespace crown
|