Created resource manager

This commit is contained in:
2014-04-13 00:20:51 +02:00
parent d13c19d654
commit 04cb1f1204
6 changed files with 124 additions and 1 deletions
+51
View File
@@ -0,0 +1,51 @@
#include "PrecompiledHeader.h"
#include "ContentManager.h"
Resource* ContentManager::CreateResource(std::string resourceType, std::string resourceName)
{
auto it = m_FactoryFunctions.find(resourceType);
if (it != m_FactoryFunctions.end())
{
return it->second(resourceName);
}
else
{
return nullptr;
}
}
void ContentManager::PreCache()
{
LOG_INFO("Pre-caching resources...");
for (auto pair : m_RegisteredResources)
{
std::string name = pair.first;
std::string type = pair.second;
Resource* resource = CreateResource(type, name);
if (resource == nullptr)
{
LOG_WARNING("Failed to pre-cache %s resource \"%s\": Resource not registered!", type.c_str(), name.c_str());
continue;
}
unsigned int typeID = m_ResourceTypeIDs[type];
unsigned int resourceID = m_ResourceCount[typeID]++;
resource->TypeID = typeID;
resource->ResourceID = resourceID;
m_ResourceIDs[name] = resourceID;
m_ResourceCache[name] = resource;
}
}
void ContentManager::RegisterResource(std::string resourceType, std::string resourceName)
{
m_RegisteredResources[resourceName] = resourceType;
m_ResourceTypeIDs[resourceName] = m_ResourceTypeCount++;
}
void ContentManager::RegisterType(std::string resourceType, std::function<Resource*(std::string)> factoryFunction)
{
m_FactoryFunctions[resourceType] = factoryFunction;
}
+65
View File
@@ -0,0 +1,65 @@
#ifndef ContentManager_h__
#define ContentManager_h__
#include <string>
#include <functional>
#include <vector>
#include <unordered_map>
#include "Factory.h"
class Resource
{
public:
unsigned int TypeID;
unsigned int ResourceID;
std::string ResourceName;
protected:
Resource(std::string resourceName)
: ResourceName(resourceName) {}
};
class ResourceManager
{
public:
// Registers the factory function of a resource type
void RegisterType(std::string resourceType, std::function<Resource*(std::string)> factoryFunction);
// Registers a future instance of a resource
void RegisterResource(std::string resourceType, std::string resourceName);
// Loads all registered resources and caches them
void PreCache();
template <typename T>
T* Fetch(std::string resourceName);
private:
unsigned int m_ResourceTypeCount = 0;
std::unordered_map<std::string, unsigned int> m_ResourceTypeIDs;
std::unordered_map<unsigned int, unsigned int> m_ResourceCount;
std::unordered_map<std::string, unsigned int> m_ResourceIDs;
std::unordered_map<std::string, std::function<Resource*(std::string)>> m_FactoryFunctions; // type -> factory function
std::unordered_map<std::string, std::string> m_RegisteredResources; // name -> type
std::unordered_map<std::string, Resource*> m_ResourceCache; // name -> resource
Resource* CreateResource(std::string resourceType, std::string resourceName);
};
template <typename T>
T* ContentManager::Fetch(std::string resourceName)
{
if (m_ResourceCache.find(resourceName) == m_ResourceCache.end())
{
LOG_ERROR("Failed to load resource \"%s\": Resource not precached!", resourceName.c_str());
return nullptr;
}
else
{
return static_cast<T*>(m_ResourceCache[resourceName]);
}
}
#endif // ContentManager_h__
+1
View File
@@ -2,6 +2,7 @@
#include "Texture.h"
Texture::Texture(std::string path)
: Resource(path)
{
Load(path);
}
+3 -1
View File
@@ -6,7 +6,9 @@
#include <SOIL.h>
class Texture
#include "ContentManager.h"
class Texture : public Resource
{
public:
Texture(std::string path);