Resource manager OBJ vs Model conflict fixed

This commit is contained in:
ViktorLjung
2014-04-27 08:07:18 +02:00
parent c4e127b154
commit cb6b0a58d8
7 changed files with 35 additions and 12 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "Model.h" #include "Model.h"
Model::Model(OBJ &obj, ResourceManager* rm) Model::Model(ResourceManager* rm, OBJ &obj)
{ {
OBJ::MaterialInfo* currentMaterial = nullptr; OBJ::MaterialInfo* currentMaterial = nullptr;
TextureGroup* currentTexGroup = nullptr; TextureGroup* currentTexGroup = nullptr;
+1 -1
View File
@@ -17,7 +17,7 @@
class Model : public Resource class Model : public Resource
{ {
public: public:
Model(OBJ &obj, ResourceManager* rm); Model(ResourceManager* rm, OBJ &obj);
struct TextureGroup struct TextureGroup
{ {
+1 -1
View File
@@ -9,7 +9,7 @@ bool OBJ::LoadFromFile(std::string filename)
std::ifstream file(m_Path.string()); std::ifstream file(m_Path.string());
if (!file.is_open()) if (!file.is_open())
{ {
LOG_ERROR("Failed to open .obj \"%s\"", m_Path.string().c_str()); LOG_ERROR("Failed to open .obj \"%s\": %s", m_Path.string().c_str(), strerror(errno));
return false; return false;
} }
+3 -1
View File
@@ -12,7 +12,9 @@
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
class OBJ #include "ResourceManager.h"
class OBJ : public Resource
{ {
public: public:
struct MaterialInfo struct MaterialInfo
+4 -4
View File
@@ -16,7 +16,7 @@ Resource* ResourceManager::CreateResource(std::string resourceType, std::string
resource->TypeID = GetTypeID(resourceType); resource->TypeID = GetTypeID(resourceType);
resource->ResourceID = GetNewResourceID(resource->TypeID); resource->ResourceID = GetNewResourceID(resource->TypeID);
// Cache // Cache
m_ResourceCache[resourceName] = resource; m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource;
return resource; return resource;
} }
@@ -28,7 +28,7 @@ void ResourceManager::RegisterType(std::string resourceType, std::function<Resou
void ResourceManager::Preload(std::string resourceType, std::string resourceName) void ResourceManager::Preload(std::string resourceType, std::string resourceName)
{ {
if (IsResourceLoaded(resourceName)) if (IsResourceLoaded(resourceType, resourceName))
{ {
LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName); LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName);
return; return;
@@ -54,7 +54,7 @@ unsigned int ResourceManager::GetNewResourceID(unsigned int typeID)
return m_ResourceCount[typeID]++; return m_ResourceCount[typeID]++;
} }
bool ResourceManager::IsResourceLoaded(std::string resourceName) bool ResourceManager::IsResourceLoaded(std::string resourceType, std::string resourceName)
{ {
return m_ResourceCache.find(resourceName) != m_ResourceCache.end(); return m_ResourceCache.find(std::make_pair(resourceType, resourceName)) != m_ResourceCache.end();
} }
+5 -4
View File
@@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "Util/UnorderedMapPair.h"
#include "Factory.h" #include "Factory.h"
class Resource class Resource
@@ -28,7 +29,7 @@ public:
void Preload(std::string resourceType, std::string resourceName); void Preload(std::string resourceType, std::string resourceName);
// Checks if a resource is in cache // Checks if a resource is in cache
bool IsResourceLoaded(std::string resourceName); bool IsResourceLoaded(std::string resourceType, std::string resourceName);
template <typename T> template <typename T>
// Hot-loads a resource and caches it for future use // Hot-loads a resource and caches it for future use
@@ -40,7 +41,7 @@ public:
private: private:
std::unordered_map<std::string, std::function<Resource*(std::string)>> m_FactoryFunctions; // type -> factory function std::unordered_map<std::string, std::function<Resource*(std::string)>> m_FactoryFunctions; // type -> factory function
std::unordered_map<std::string, Resource*> m_ResourceCache; // name -> resource std::unordered_map<std::pair<std::string, std::string>, Resource*> m_ResourceCache; // (type, name) -> resource
// TODO: Getters for IDs // TODO: Getters for IDs
unsigned int m_CurrentResourceTypeID; unsigned int m_CurrentResourceTypeID;
@@ -60,7 +61,7 @@ private:
template <typename T> template <typename T>
T* ResourceManager::Load(std::string resourceType, std::string resourceName) T* ResourceManager::Load(std::string resourceType, std::string resourceName)
{ {
auto it = m_ResourceCache.find(resourceName); auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName));
if (it != m_ResourceCache.end()) if (it != m_ResourceCache.end())
return static_cast<T*>(it->second); return static_cast<T*>(it->second);
@@ -79,7 +80,7 @@ T* ResourceManager::Load(std::string resourceType, std::string resourceName)
template <typename T> template <typename T>
T* ResourceManager::Fetch(std::string resourceName) const T* ResourceManager::Fetch(std::string resourceName) const
{ {
auto it = m_ResourceCache.find(resourceName); auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName));
if (it == m_ResourceCache.end()) if (it == m_ResourceCache.end())
{ {
LOG_ERROR("Failed to fetch resource \"%s\": Resource not loaded!", resourceName.c_str()); LOG_ERROR("Failed to fetch resource \"%s\": Resource not loaded!", resourceName.c_str());
+20
View File
@@ -0,0 +1,20 @@
#ifndef Util_UnorderedMapPair_h__
#define Util_UnorderedMapPair_h__
#include <boost/functional/hash.hpp>
namespace std
{
template<typename S, typename T> struct hash<pair<S, T>>
{
inline size_t operator()(const pair<S, T> & v) const
{
size_t seed = 0;
boost::hash_combine(seed, v.first);
boost::hash_combine(seed, v.second);
return seed;
}
};
}
#endif // Util_UnorderedMapPair_h__