Browse Source

Refactor resource management coupling

Daniele Bartolini 13 năm trước cách đây
mục cha
commit
a503690dff
4 tập tin đã thay đổi với 79 bổ sung31 xóa
  1. 31 12
      src/ResourceLoader.cpp
  2. 15 9
      src/ResourceLoader.h
  3. 25 3
      src/ResourceManager.cpp
  4. 8 7
      src/ResourceManager.h

+ 31 - 12
src/ResourceLoader.cpp

@@ -25,6 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "ResourceLoader.h"
 #include "ResourceManager.h"
+#include "ResourceArchive.h"
 #include "String.h"
 #include "Hash.h"
 #include "TextureResource.h"
@@ -36,12 +37,12 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-ResourceLoader::ResourceLoader(ResourceManager* resource_manager, Allocator& resource_allocator, Filesystem* filesystem) :
-	m_resource_manager(resource_manager),
-	m_resource_allocator(&resource_allocator),
-	m_filesystem(filesystem),
-	m_resource_archive(filesystem),
-	m_resources(m_allocator)
+ResourceLoader::ResourceLoader(Allocator& resource_allocator, ResourceArchive& archive) :
+	m_resource_allocator(resource_allocator),
+	m_resource_archive(archive),
+	m_resources(m_allocator),
+	m_loading_callback(NULL),
+	m_online_callback(NULL)
 {
 	m_config_hash = hash::fnv1a_32("config", string::strlen("config"));
 	m_texture_hash = hash::fnv1a_32("tga", string::strlen("tga"));
@@ -60,7 +61,10 @@ void ResourceLoader::load(ResourceId name)
 	m_resources.push_back(name);
 	
 	// callback to the resource manager
-	m_resource_manager->loading(name);
+	if (m_loading_callback != NULL)
+	{
+		m_loading_callback(name);
+	}
 }
 
 //-----------------------------------------------------------------------------
@@ -78,12 +82,27 @@ void ResourceLoader::flush()
 		
 		void* data = load_by_type(resource);
 
-		m_resource_manager->online(m_resources.front(), data);
+		if (m_online_callback != NULL)
+		{
+			m_online_callback(m_resources.front(), data);
+		}
 
 		m_resources.pop_front();
 	}
 }
 
+//-----------------------------------------------------------------------------
+void ResourceLoader::set_loading_callback(ResourceLoadingCallback f)
+{
+	m_loading_callback = f;
+}
+
+//-----------------------------------------------------------------------------
+void ResourceLoader::set_online_callback(ResourceOnlineCallback f)
+{
+	m_online_callback = f;
+}
+
 //-----------------------------------------------------------------------------
 void* ResourceLoader::load_by_type(ResourceId name)
 {
@@ -94,12 +113,12 @@ void* ResourceLoader::load_by_type(ResourceId name)
 
 	if (name.type == m_texture_hash)
 	{
-		return TextureResource::load(*m_resource_allocator, &m_resource_archive, name);
+		return TextureResource::load(m_resource_allocator, &m_resource_archive, name);
 	}
 
 	if (name.type == m_txt_hash)
 	{
-		return TextResource::load(*m_resource_allocator, &m_resource_archive, name);
+		return TextResource::load(m_resource_allocator, &m_resource_archive, name);
 	}
 
 	return NULL;
@@ -115,12 +134,12 @@ void ResourceLoader::unload_by_type(ResourceId name, void* resource)
 
 	if (name.type == m_texture_hash)
 	{
-		TextureResource::unload(*m_resource_allocator, (TextureResource*)resource);
+		TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
 	}
 
 	if (name.type == m_txt_hash)
 	{
-		TextResource::unload(*m_resource_allocator, (TextResource*)resource);
+		TextResource::unload(m_resource_allocator, (TextResource*)resource);
 	}
 
 	return;

+ 15 - 9
src/ResourceLoader.h

@@ -28,20 +28,23 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Queue.h"
 #include "Resource.h"
 #include "MallocAllocator.h"
-#include "ResourceArchive.h"
 
 namespace crown
 {
 
 class ResourceManager;
 class Allocator;
-class Filesystem;
+class ResourceArchive;
+
+// Callbacks typedefs
+typedef void (*ResourceLoadingCallback)(ResourceId);
+typedef void (*ResourceOnlineCallback)(ResourceId, void*);
 
 class ResourceLoader
 {
 public:
 
-						ResourceLoader(ResourceManager* resource_manager, Allocator& resource_allocator, Filesystem* filesystem);
+						ResourceLoader(Allocator& resource_allocator, ResourceArchive& archive);
 						~ResourceLoader();
 
 	void				load(ResourceId name);
@@ -49,6 +52,9 @@ public:
 
 	void				flush();
 
+	void				set_loading_callback(ResourceLoadingCallback f);
+	void				set_online_callback(ResourceOnlineCallback f);
+
 private:
 
 	void*				load_by_type(ResourceId name);
@@ -56,14 +62,15 @@ private:
 
 private:
 
-	ResourceManager*	m_resource_manager;
-	Allocator*			m_resource_allocator;
-	Filesystem*			m_filesystem;
+	Allocator&			m_resource_allocator;
+	ResourceArchive&	m_resource_archive;
 
-	ResourceArchive		m_resource_archive;
 	MallocAllocator		m_allocator;
-
 	Queue<ResourceId>	m_resources;
+
+	// Callbacks
+	ResourceLoadingCallback m_loading_callback;
+	ResourceOnlineCallback	m_online_callback;
 	
 	uint32_t			m_config_hash;
 	uint32_t			m_texture_hash;
@@ -72,4 +79,3 @@ private:
 };
 
 } // namespace crown
-

+ 25 - 3
src/ResourceManager.cpp

@@ -29,6 +29,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "String.h"
 #include "Hash.h"
 #include "Path.h"
+#include "Log.h"
 #include <algorithm>
 
 #include "TextureResource.h"
@@ -37,9 +38,8 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-ResourceManager::ResourceManager(Allocator& resource_allocator, Filesystem* filesystem) :
-	m_resource_allocator(&resource_allocator),
-	m_resource_loader(this, resource_allocator, filesystem),
+ResourceManager::ResourceManager(ResourceLoader& loader) :
+	m_resource_loader(loader),
 	m_resources(m_allocator)
 {
 }
@@ -163,6 +163,12 @@ uint32_t ResourceManager::references(ResourceId name) const
 	return m_resources[name.index].references;
 }
 
+//-----------------------------------------------------------------------------
+void ResourceManager::flush()
+{
+	m_resource_loader.flush();
+}
+
 //-----------------------------------------------------------------------------
 void ResourceManager::loading(ResourceId name)
 {
@@ -187,5 +193,21 @@ void ResourceManager::online(ResourceId name, void* resource)
 	entry.state = RS_LOADED;
 }
 
+//-----------------------------------------------------------------------------
+void ResourceManager::loading_callback_wrapper(void* thiz, ResourceId name)
+{
+	ResourceManager* self = (ResourceManager*)thiz;
+
+	self->loading(name);
+}
+
+//-----------------------------------------------------------------------------
+void ResourceManager::online_callback_wrapper(void* thiz, ResourceId name, void* resource)
+{
+	ResourceManager* self = (ResourceManager*)thiz;
+
+	self->online(name, resource);
+}
+
 } // namespace crown
 

+ 8 - 7
src/ResourceManager.h

@@ -28,14 +28,11 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Types.h"
 #include "List.h"
 #include "Resource.h"
-#include "ResourceLoader.h"
 #include "MallocAllocator.h"
 
 namespace crown
 {
 
-class Filesystem;
-
 struct ResourceEntry
 {
 	ResourceId		id;
@@ -63,7 +60,7 @@ class ResourceManager
 {
 public:
 
-							ResourceManager(Allocator& resource_allocator, Filesystem* filesystem);
+							ResourceManager(ResourceLoader& loader);
 							~ResourceManager();
 
 	/// Loads the resource by @name and returns its ResourceId.
@@ -104,7 +101,11 @@ public:
 	uint32_t				references(ResourceId name) const;
 	
 	/// Forces the loading of all of the queued resource load requests.
-	void					flush() { m_resource_loader.flush(); }
+	void					flush();
+
+	/// Callback wrappers to member functions
+	void					loading_callback_wrapper(void* thiz, ResourceId name);
+	void					online_callback_wrapper(void* thiz, ResourceId name, void* resource);
 
 private:
 
@@ -113,8 +114,8 @@ private:
 
 private:
 
-	Allocator*				m_resource_allocator;
-	ResourceLoader			m_resource_loader;
+	ResourceLoader&			m_resource_loader;
+
 	MallocAllocator			m_allocator;
 	List<ResourceEntry>		m_resources;