Created resource manager
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
@@ -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__
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "Texture.h"
|
#include "Texture.h"
|
||||||
|
|
||||||
Texture::Texture(std::string path)
|
Texture::Texture(std::string path)
|
||||||
|
: Resource(path)
|
||||||
{
|
{
|
||||||
Load(path);
|
Load(path);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -6,7 +6,9 @@
|
|||||||
|
|
||||||
#include <SOIL.h>
|
#include <SOIL.h>
|
||||||
|
|
||||||
class Texture
|
#include "ContentManager.h"
|
||||||
|
|
||||||
|
class Texture : public Resource
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Texture(std::string path);
|
Texture(std::string path);
|
||||||
|
|||||||
@@ -97,6 +97,7 @@
|
|||||||
<ClCompile Include="..\..\src\OBJ.cpp" />
|
<ClCompile Include="..\..\src\OBJ.cpp" />
|
||||||
<ClCompile Include="..\..\src\PrecompiledHeader.cpp" />
|
<ClCompile Include="..\..\src\PrecompiledHeader.cpp" />
|
||||||
<ClCompile Include="..\..\src\Renderer.cpp" />
|
<ClCompile Include="..\..\src\Renderer.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\ResourceManager.cpp" />
|
||||||
<ClCompile Include="..\..\src\ShaderProgram.cpp" />
|
<ClCompile Include="..\..\src\ShaderProgram.cpp" />
|
||||||
<ClCompile Include="..\..\src\Skybox.cpp" />
|
<ClCompile Include="..\..\src\Skybox.cpp" />
|
||||||
<ClCompile Include="..\..\src\Systems\DebugSystem.cpp" />
|
<ClCompile Include="..\..\src\Systems\DebugSystem.cpp" />
|
||||||
@@ -144,6 +145,7 @@
|
|||||||
<ClInclude Include="..\..\src\OBJ.h" />
|
<ClInclude Include="..\..\src\OBJ.h" />
|
||||||
<ClInclude Include="..\..\src\PrecompiledHeader.h" />
|
<ClInclude Include="..\..\src\PrecompiledHeader.h" />
|
||||||
<ClInclude Include="..\..\src\Renderer.h" />
|
<ClInclude Include="..\..\src\Renderer.h" />
|
||||||
|
<ClInclude Include="..\..\src\ResourceManager.h" />
|
||||||
<ClInclude Include="..\..\src\ShaderProgram.h" />
|
<ClInclude Include="..\..\src\ShaderProgram.h" />
|
||||||
<ClInclude Include="..\..\src\Skybox.h" />
|
<ClInclude Include="..\..\src\Skybox.h" />
|
||||||
<ClInclude Include="..\..\src\System.h" />
|
<ClInclude Include="..\..\src\System.h" />
|
||||||
|
|||||||
@@ -48,6 +48,7 @@
|
|||||||
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp">
|
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp">
|
||||||
<Filter>Input\Systems</Filter>
|
<Filter>Input\Systems</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\ResourceManager.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Filter Include="Util">
|
<Filter Include="Util">
|
||||||
@@ -236,6 +237,7 @@
|
|||||||
<ClInclude Include="..\..\src\Components\DirectionalLight.h">
|
<ClInclude Include="..\..\src\Components\DirectionalLight.h">
|
||||||
<Filter>Rendering\Components</Filter>
|
<Filter>Rendering\Components</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\ResourceManager.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
||||||
|
|||||||
Reference in New Issue
Block a user