Daniele Bartolini 10 yıl önce
ebeveyn
işleme
55560f7e0e

+ 5 - 0
src/resource/resource_loader.cpp

@@ -114,4 +114,9 @@ s32 ResourceLoader::run()
 	return 0;
 }
 
+s32 ResourceLoader::thread_proc(void* thiz)
+{
+	return ((ResourceLoader*)thiz)->run();
+}
+
 } // namespace crown

+ 15 - 24
src/resource/resource_loader.h

@@ -30,6 +30,21 @@ struct ResourceRequest
 /// @ingroup Resource
 class ResourceLoader
 {
+	Filesystem& _fs;
+
+	Queue<ResourceRequest> _requests;
+	Queue<ResourceRequest> _loaded;
+
+	Thread _thread;
+	Mutex _mutex;
+	Mutex _loaded_mutex;
+	bool _exit;
+
+	u32 num_requests();
+	void add_loaded(ResourceRequest rr);
+	s32 run();
+	static s32 thread_proc(void* thiz);
+
 public:
 
 	/// Read resources from @a fs.
@@ -47,30 +62,6 @@ public:
 
 	/// Returns all the resources that have been loaded.
 	void get_loaded(Array<ResourceRequest>& loaded);
-
-private:
-
-	u32 num_requests();
-	void add_loaded(ResourceRequest rr);
-
-	// Loads resources in the loading queue.
-	s32 run();
-
-	static s32 thread_proc(void* thiz)
-	{
-		return ((ResourceLoader*)thiz)->run();
-	}
-
-private:
-
-	Thread _thread;
-	Filesystem& _fs;
-
-	Queue<ResourceRequest> _requests;
-	Queue<ResourceRequest> _loaded;
-	Mutex _mutex;
-	Mutex _loaded_mutex;
-	bool _exit;
 };
 
 } // namespace crown

+ 9 - 6
src/resource/resource_manager.cpp

@@ -82,7 +82,7 @@ ResourceManager::~ResourceManager()
 
 void ResourceManager::load(StringId64 type, StringId64 name)
 {
-	ResourcePair id = make_pair(type, name);
+	ResourcePair id = { type, name };
 	ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
 
 	if (entry == ResourceEntry::NOT_FOUND)
@@ -116,7 +116,7 @@ void ResourceManager::unload(StringId64 type, StringId64 name)
 {
 	flush();
 
-	ResourcePair id = make_pair(type, name);
+	ResourcePair id = { type, name };
 	ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
 
 	if (--entry.references == 0)
@@ -131,7 +131,7 @@ void ResourceManager::unload(StringId64 type, StringId64 name)
 
 void ResourceManager::reload(StringId64 type, StringId64 name)
 {
-	const ResourcePair id = make_pair(type, name);
+	const ResourcePair id = { type, name };
 	const ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
 	const u32 old_refs = entry.references;
 
@@ -145,12 +145,13 @@ void ResourceManager::reload(StringId64 type, StringId64 name)
 
 bool ResourceManager::can_get(StringId64 type, StringId64 name)
 {
-	return _autoload ? true : sort_map::has(_rm, make_pair(type, name));
+	const ResourcePair id = { type, name };
+	return _autoload ? true : sort_map::has(_rm, id);
 }
 
 const void* ResourceManager::get(StringId64 type, StringId64 name)
 {
-	const ResourcePair id = make_pair(type, name);
+	const ResourcePair id = { type, name };
 	char type_buf[StringId64::STRING_LENGTH];
 	char name_buf[StringId64::STRING_LENGTH];
 
@@ -199,7 +200,9 @@ void ResourceManager::complete_request(StringId64 type, StringId64 name, void* d
 	entry.references = 1;
 	entry.data = data;
 
-	sort_map::set(_rm, make_pair(type, name), entry);
+	ResourcePair id = { type, name };
+
+	sort_map::set(_rm, id, entry);
 	sort_map::sort(_rm);
 
 	on_online(type, name);

+ 41 - 53
src/resource/resource_manager.h

@@ -20,55 +20,11 @@ namespace crown
 /// @ingroup Resource
 class ResourceManager
 {
-public:
-
-	/// Uses @a rl to load resources.
-	ResourceManager(ResourceLoader& rl);
-	~ResourceManager();
-
-	/// Loads the resource (@a type, @a name).
-	/// You can check whether the resource is available with can_get().
-	void load(StringId64 type, StringId64 name);
-
-	/// Unloads the resource @a type @a name.
-	void unload(StringId64 type, StringId64 name);
-
-	/// Reloads the resource (@a type, @a name).
-	/// @note The user has to manually update all the references to the old resource.
-	void reload(StringId64 type, StringId64 name);
-
-	/// Returns whether the manager has the resource (@a type, @a name).
-	bool can_get(StringId64 type, StringId64 name);
-
-	/// Returns the data of the resource (@a type, @a name).
-	const void* get(StringId64 type, StringId64 name);
-
-	/// Sets whether resources should be automatically loaded when accessed.
-	void enable_autoload(bool enable);
-
-	/// Blocks until all load() requests have been completed.
-	void flush();
-
-	/// Completes all load() requests which have been loaded by ResourceLoader.
-	void complete_requests();
-
-private:
-
 	typedef void* (*LoadFunction)(File& file, Allocator& a);
 	typedef void (*OnlineFunction)(StringId64 name, ResourceManager& rm);
 	typedef void (*OfflineFunction)(StringId64 name, ResourceManager& rm);
 	typedef void (*UnloadFunction)(Allocator& allocator, void* resource);
 
-	void register_resource_type(StringId64 type, LoadFunction load, OnlineFunction online, OfflineFunction offline, UnloadFunction unload);
-
-	void on_online(StringId64 type, StringId64 name);
-	void on_offline(StringId64 type, StringId64 name);
-	void on_unload(StringId64 type, void* data);
-
-	void complete_request(StringId64 type, StringId64 name, void* data);
-
-private:
-
 	struct ResourcePair
 	{
 		StringId64 type;
@@ -80,22 +36,16 @@ private:
 		}
 	};
 
-	ResourcePair make_pair(StringId64 type, StringId64 name)
-	{
-		ResourcePair pair = { type, name };
-		return pair;
-	}
-
 	struct ResourceEntry
 	{
+		u32 references;
+		void* data;
+
 		bool operator==(const ResourceEntry& e)
 		{
 			return references == e.references && data == e.data;
 		}
 
-		u32 references;
-		void* data;
-
 		static const ResourceEntry NOT_FOUND;
 	};
 
@@ -115,6 +65,44 @@ private:
 	TypeMap _type_data;
 	ResourceMap _rm;
 	bool _autoload;
+
+	void register_resource_type(StringId64 type, LoadFunction load, OnlineFunction online, OfflineFunction offline, UnloadFunction unload);
+	void on_online(StringId64 type, StringId64 name);
+	void on_offline(StringId64 type, StringId64 name);
+	void on_unload(StringId64 type, void* data);
+	void complete_request(StringId64 type, StringId64 name, void* data);
+
+public:
+
+	/// Uses @a rl to load resources.
+	ResourceManager(ResourceLoader& rl);
+	~ResourceManager();
+
+	/// Loads the resource (@a type, @a name).
+	/// You can check whether the resource is available with can_get().
+	void load(StringId64 type, StringId64 name);
+
+	/// Unloads the resource @a type @a name.
+	void unload(StringId64 type, StringId64 name);
+
+	/// Reloads the resource (@a type, @a name).
+	/// @note The user has to manually update all the references to the old resource.
+	void reload(StringId64 type, StringId64 name);
+
+	/// Returns whether the manager has the resource (@a type, @a name).
+	bool can_get(StringId64 type, StringId64 name);
+
+	/// Returns the data of the resource (@a type, @a name).
+	const void* get(StringId64 type, StringId64 name);
+
+	/// Sets whether resources should be automatically loaded when accessed.
+	void enable_autoload(bool enable);
+
+	/// Blocks until all load() requests have been completed.
+	void flush();
+
+	/// Completes all load() requests which have been loaded by ResourceLoader.
+	void complete_requests();
 };
 
 } // namespace crown