Browse Source

Implementing the new resource manager (yes new one)
Needs testing

Panagiotis Christopoulos Charitos 15 years ago
parent
commit
28e17043d6

File diff suppressed because it is too large
+ 0 - 1
build/debug/Makefile


+ 1 - 1
src/Main.cpp

@@ -13,7 +13,6 @@
 #include "PointLight.h"
 #include "SpotLight.h"
 #include "Material.h"
-#include "Resource.h"
 #include "Scene.h"
 #include "Scanner.h"
 #include "skybox.h"
@@ -34,6 +33,7 @@
 #include "SkelAnimModelNodeCtrl.h"
 #include "Model.h"
 #include "Logger.h"
+#include "Util.h"
 
 
 // map (hard coded)

+ 0 - 1
src/Misc/skybox.cpp

@@ -1,5 +1,4 @@
 #include "skybox.h"
-#include "Resource.h"
 #include "Renderer.h"
 #include "Math.h"
 #include "Camera.h"

+ 1 - 0
src/Renderer/Sm.h

@@ -4,6 +4,7 @@
 #include "RenderingPass.h"
 #include "Fbo.h"
 #include "Texture.h"
+#include "Properties.h"
 
 
 class Camera;

+ 0 - 64
src/Resources/Core/Resource.h

@@ -1,64 +0,0 @@
-#ifndef RESOURCE_H
-#define RESOURCE_H
-
-#include <string>
-#include "Util.h"
-#include "Properties.h"
-#include "StdTypes.h"
-
-
-template<typename Type>
-class RsrcContainer;
-
-
-/// Every class that it is considered a resource should be derived by this one. This step is not necessary because of
-/// the RsrcContainer template but ensures that loading will be made by the resource manager and not the class itself
-class Resource
-{
-	template<typename Type>
-	friend class RsrcContainer; ///< Cause it calls Resource::load and Resource::unload
-
-	template<typename Type>
-	friend class RsrcPtr; ///< Cause the RsrcPtr copy constructor increases the referenceCounter
-
-	public:
-		enum ResourceType
-		{
-			RT_TEXTURE,
-			RT_SHADER_PROG,
-			RT_MATERIAL,
-			RT_MESH,
-			RT_SKELETON,
-			RT_SKEL_ANIM,
-			RT_LIGHT_PROPS,
-			RT_EXTENSION,
-			RT_PARTICLE_EMITTER_PROPS,
-			RT_SCRIPT,
-			RT_MODEL,
-			RT_SKIN
-		};
-
-	PROPERTY_R(std::string, path, getRsrcPath);
-	PROPERTY_R(std::string, name, getRsrcName);
-	PROPERTY_R(uint, referenceCounter, getRsrcReferencesNum);
-	PROPERTY_R(ResourceType, type, getRsrcType);
-
-	public:
-		Resource(const ResourceType& type_);
-		virtual ~Resource() {/*DEBUG_ERR(referenceCounter != 0);*/}
-
-	private:
-		/// Load the resource
-		/// @param filename The file to load
-		/// @exception Exception
-		virtual void load(const char* filename) = 0;
-};
-
-
-inline Resource::Resource(const ResourceType& type_):
-	referenceCounter(0),
-	type(type_)
-{}
-
-
-#endif

+ 43 - 0
src/Resources/Core/ResourceManager.cpp

@@ -0,0 +1,43 @@
+#include "ResourceManager.h"
+
+#include "Texture.h"
+#include "Material.h"
+#include "ShaderProg.h"
+#include "Mesh.h"
+#include "Skeleton.h"
+#include "SkelAnim.h"
+#include "LightData.h"
+#include "ParticleEmitterProps.h"
+#include "Script.h"
+#include "Model.h"
+#include "Skin.h"
+#include "DummyRsrc.h"
+
+
+// Because we are bored to write the same
+#define SPECIALIZE_TEMPLATE_STUFF(type__, container__) \
+	template<> \
+	ResourceManager::Types<type__>::Container& ResourceManager::choseContainer<type__>() \
+	{ \
+		return container__; \
+	} \
+	\
+	template<> \
+	void ResourceManager::unload<type__>(const Types<type__>::Info& info) \
+	{ \
+		unloadR<type__>(info); \
+	}
+
+
+SPECIALIZE_TEMPLATE_STUFF(Texture, textures)
+SPECIALIZE_TEMPLATE_STUFF(ShaderProg, shaderProgs)
+SPECIALIZE_TEMPLATE_STUFF(Material, materials)
+SPECIALIZE_TEMPLATE_STUFF(Mesh, meshes)
+SPECIALIZE_TEMPLATE_STUFF(Skeleton, skeletons)
+SPECIALIZE_TEMPLATE_STUFF(SkelAnim, skelAnims)
+SPECIALIZE_TEMPLATE_STUFF(LightData, lightProps)
+SPECIALIZE_TEMPLATE_STUFF(ParticleEmitterProps, particleEmitterProps)
+SPECIALIZE_TEMPLATE_STUFF(Script, scripts)
+SPECIALIZE_TEMPLATE_STUFF(Model, models)
+SPECIALIZE_TEMPLATE_STUFF(Skin, skins)
+SPECIALIZE_TEMPLATE_STUFF(DummyRsrc, dummies)

+ 101 - 0
src/Resources/Core/ResourceManager.h

@@ -0,0 +1,101 @@
+#ifndef RESOURCE_MANAGER_H
+#define RESOURCE_MANAGER_H
+
+#include <list>
+#include <string>
+#include "Singleton.h"
+
+
+class Texture;
+class ShaderProg;
+class Material;
+class Mesh;
+class Skeleton;
+class SkelAnim;
+class LightData;
+class ParticleEmitterProps;
+class Script;
+class Model;
+class Skin;
+class DummyRsrc;
+
+
+/// Includes information about the resources
+template<typename Type>
+struct RsrcHook
+{
+	std::string uuid;
+	int referenceCounter;
+	Type* resource;
+};
+
+
+/// Responsible of loading and unloading resources
+class ResourceManager
+{
+	public:
+		/// Because the current C++ standard does not offer template typedefs
+		template<typename Type>
+		struct Types
+		{
+			typedef RsrcHook<Type> Info;
+			typedef std::list<Info> Container;
+			typedef typename Container::iterator Iterator;
+			typedef typename Container::const_iterator ConstIterator;
+		};
+
+		/// Load a resource
+		/// See if its already loaded, if its not:
+		/// - Create a instance
+		/// - Call load method of the instance
+		/// If it loaded:
+		/// - Increase the resource counter
+		template<typename Type>
+		typename Types<Type>::Info& load(const char* filename);
+
+		/// Unload a resource if no one uses it
+		template<typename Type>
+		void unload(const typename Types<Type>::Info& info);
+
+	private:
+		/// Find a resource using the filename
+		template<typename Type>
+		typename Types<Type>::Iterator find(const char* filename, typename Types<Type>::Container& container);
+
+		/// Find a resource using the pointer
+		template<typename Type>
+		typename Types<Type>::Iterator find(const Type* resource, typename Types<Type>::Container& container);
+
+		/// Specialized func
+		template<typename Type>
+		typename Types<Type>::Container& choseContainer();
+
+		/// Unload a resource if no one uses it. This is the real deal
+		template<typename Type>
+		void unloadR(const typename Types<Type>::Info& info);
+
+		/// @name Containers
+		/// @{
+		Types<Texture>::Container textures;
+		Types<ShaderProg>::Container shaderProgs;
+		Types<Material>::Container materials;
+		Types<Mesh>::Container meshes;
+		Types<Skeleton>::Container skeletons;
+		Types<SkelAnim>::Container skelAnims;
+		Types<LightData>::Container lightProps;
+		Types<ParticleEmitterProps>::Container particleEmitterProps;
+		Types<Script>::Container scripts;
+		Types<Model>::Container models;
+		Types<Skin>::Container skins;
+		Types<DummyRsrc>::Container dummies;
+		/// @}
+};
+
+
+#include "ResourceManager.inl.h"
+
+
+typedef Singleton<ResourceManager> ResourceManagerSingleton;
+
+
+#endif

+ 123 - 0
src/Resources/Core/ResourceManager.inl.h

@@ -0,0 +1,123 @@
+#include <boost/type_traits.hpp>
+#include "Exception.h"
+
+
+//======================================================================================================================
+// loadRsrc                                                                                                            =
+//======================================================================================================================
+template<typename Type>
+typename ResourceManager::Types<Type>::Info& ResourceManager::load(const char* filename)
+{
+	// Chose container
+	typename Types<Type>::Container& c = choseContainer<Type>();
+
+	// Find
+	typename Types<Type>::Iterator it = find<Type>(filename, c);
+
+	// If already loaded
+	if(it != c.end())
+	{
+		++it->referenceCounter;
+	}
+	// else create new, load it and update the container
+	else
+	{
+		Type* newInstance = NULL;
+
+		try
+		{
+			newInstance = new Type();
+			newInstance->load(filename);
+		}
+		catch(std::exception& e)
+		{
+			if(newInstance != NULL)
+			{
+				delete newInstance;
+			}
+			//throw EXCEPTION("Cannot load resource: " + e.what());
+		}
+
+		c.push_back(typename Types<Type>::Info());
+		it = c.end();
+		--it;
+
+		it->uuid = filename;
+		it->referenceCounter = 1;
+		it->resource = newInstance;
+	}
+
+	return *it;
+}
+
+
+//======================================================================================================================
+// unload                                                                                                              =
+//======================================================================================================================
+template<typename Type>
+void ResourceManager::unloadR(const typename Types<Type>::Info& info)
+{
+	// Chose container
+	typename Types<Type>::Container& c = choseContainer<Type>();
+
+	// Find
+	typename Types<Type>::Iterator it = find<Type>(info.resource, c);
+
+	// If not found
+	if(it == c.end())
+	{
+		throw EXCEPTION("Resource hook incorrect (\"" + info.uuid + "\")");
+	}
+
+	RASSERT_THROW_EXCEPTION(it->uuid != info.uuid);
+	RASSERT_THROW_EXCEPTION(it->referenceCounter != info.referenceCounter);
+
+	--it->referenceCounter;
+
+	// Delete the resource
+	if(it->referenceCounter == 0)
+	{
+		delete it->resource;
+		c.erase(it);
+	}
+}
+
+
+//======================================================================================================================
+// find [char*]                                                                                                        =
+//======================================================================================================================
+template<typename Type>
+typename ResourceManager::Types<Type>::Iterator
+ResourceManager::find(const char* filename, typename Types<Type>::Container& container)
+{
+	typename Types<Type>::Iterator it = container.begin();
+	for(; it != container.end(); it++)
+	{
+		if(it->uuid == filename)
+		{
+			break;
+		}
+	}
+
+	return it;
+}
+
+
+//======================================================================================================================
+// find [Type*]                                                                                                        =
+//======================================================================================================================
+template<typename Type>
+typename ResourceManager::Types<Type>::Iterator
+ResourceManager::find(const Type* resource, typename Types<Type>::Container& container)
+{
+	typename Types<Type>::Iterator it = container.begin();
+	for(; it != container.end(); it++)
+	{
+		if(it->resource == resource)
+		{
+			break;
+		}
+	}
+
+	return it;
+}

+ 0 - 55
src/Resources/Core/RsrcContainer.h

@@ -1,55 +0,0 @@
-#ifndef RSRC_CONTAINER_H
-#define RSRC_CONTAINER_H
-
-#include "Vec.h"
-
-
-template<typename Type>
-class RsrcPtr;
-
-
-/// Resource container template class
-template<typename Type>
-class RsrcContainer: public Vec<Type*>
-{
-	friend class RsrcPtr<Type>; ///< So it can access load, unload
-
-	private:
-		typedef Vec<Type*> BaseClass;
-		typedef typename BaseClass::iterator Iterator; ///< Just to save me time from typing
-
-	public:
-		RsrcContainer() {}
-		~RsrcContainer();
-
-	private:
-		/// load an object and register it. If its already loaded return its pointer
-		/// @param fname The filename that initializes the object
-		/// @return A pointer of a new resource or NULL on fail
-		Type* load(const char* fname);
-
-		/// unload item. If nobody else uses it then delete it completely
-		/// @param x Pointer to the instance we want to unload
-		void unload(Type* x);
-
-		/// Search inside the container by name
-		/// @param name The name of the resource
-		/// @return The iterator of the content end of vector if not found
-		Iterator findByName(const char* name);
-
-		/// Search inside the container by name and path
-		/// @param name The name of the resource
-		/// @param path The path of the resource
-		/// @return The iterator of the content end of vector if not found
-		Iterator findByNameAndPath(const char* name, const char* path);
-
-	  /// Search inside the container by pointer
-		/// @param name The name of the resource object
-		/// @return The iterator of the content end of vector if not found
-		Iterator findByPtr(Type* ptr);
-};
-
-
-#include "RsrcContainer.inl.h"
-
-#endif

+ 0 - 138
src/Resources/Core/RsrcContainer.inl.h

@@ -1,138 +0,0 @@
-#include <boost/filesystem.hpp>
-#include <boost/lexical_cast.hpp>
-#include "RsrcContainer.h"
-#include "Exception.h"
-#include "Logger.h"
-
-
-//======================================================================================================================
-// Destructor                                                                                                          =
-//======================================================================================================================
-template<typename Type>
-RsrcContainer<Type>::~RsrcContainer()
-{
-	if(BaseClass::size() != 0)
-	{
-		ERROR("Resources are still loaded");
-	}
-}
-
-
-//======================================================================================================================
-// findByName                                                                                                          =
-//======================================================================================================================
-template<typename Type>
-typename RsrcContainer<Type>::Iterator RsrcContainer<Type>::findByName(const char* name)
-{
-	Iterator it = BaseClass::begin();
-	while(it != BaseClass::end())
-	{
-		if((*it).name == name)
-			return it;
-		++it;
-	}
-
-	return it;
-}
-
-
-//======================================================================================================================
-// findByNameAndPath                                                                                                   =
-//======================================================================================================================
-template<typename Type>
-typename RsrcContainer<Type>::Iterator RsrcContainer<Type>::findByNameAndPath(const char* name, const char* path)
-{
-	Iterator it = BaseClass::begin();
-	while(it != BaseClass::end())
-	{
-		if((*it)->name == name && (*it)->path == path)
-			return it;
-		++it;
-	}
-
-	return it;
-}
-
-
-//======================================================================================================================
-// findByPtr                                                                                                           =
-//======================================================================================================================
-template<typename Type>
-typename RsrcContainer<Type>::Iterator RsrcContainer<Type>::findByPtr(Type* ptr)
-{
-	Iterator it = BaseClass::begin();
-	while(it != BaseClass::end())
-	{
-		if(ptr == (*it)) return it;
-		++it;
-	}
-
-	return it;
-}
-
-
-
-//======================================================================================================================
-// load                                                                                                                =
-//======================================================================================================================
-template<typename Type>
-Type* RsrcContainer<Type>::load(const char* fname)
-{
-	RASSERT_THROW_EXCEPTION(fname == NULL);
-	boost::filesystem::path fpathname = boost::filesystem::path(fname);
-	std::string name = fpathname.filename();
-	std::string path = fpathname.parent_path().string();
-	Iterator it = findByNameAndPath(name.c_str(), path.c_str());
-
-	// if already loaded then inc the users and return the pointer
-	if(it != BaseClass::end())
-	{
-		++ (*it)->referenceCounter;
-		return (*it);
-	}
-
-	// else create new, loaded and update the container
-	Type* newInstance = new Type();
-	newInstance->name = name;
-	newInstance->path = path;
-	newInstance->referenceCounter = 1;
-
-	try
-	{
-		newInstance->load(fname);
-	}
-	catch(std::exception& e)
-	{
-		delete newInstance;
-		throw EXCEPTION("Cannot load resource: " + e.what());
-	}
-
-	BaseClass::push_back(newInstance);
-	return newInstance;
-}
-
-
-//======================================================================================================================
-// unload                                                                                                              =
-//======================================================================================================================
-template<typename Type>
-void RsrcContainer<Type>::unload(Type* x)
-{
-	Iterator it = findByPtr(x);
-	if(it == BaseClass::end())
-	{
-		throw EXCEPTION("Cannot find resource with pointer " + boost::lexical_cast<std::string>(x));
-	}
-
-	Type* del_ = (*it);
-	RASSERT_THROW_EXCEPTION(del_->referenceCounter < 1); // WTF?
-
-	--del_->referenceCounter;
-
-	// if no other users then call unload and update the container
-	if(del_->referenceCounter == 0)
-	{
-		delete del_;
-		BaseClass::erase(it);
-	}
-}

+ 0 - 29
src/Resources/Core/RsrcContainers.cpp

@@ -1,29 +0,0 @@
-#include "RsrcContainer.h"
-#include "Texture.h"
-#include "Material.h"
-#include "ShaderProg.h"
-#include "Mesh.h"
-#include "Skeleton.h"
-#include "SkelAnim.h"
-#include "LightData.h"
-#include "ParticleEmitterProps.h"
-#include "Script.h"
-#include "Model.h"
-#include "Skin.h"
-
-
-namespace RsrcContainers {
-
-RsrcContainer<Texture>    textures;
-RsrcContainer<ShaderProg> shaderProgs;
-RsrcContainer<Material>   materials;
-RsrcContainer<Mesh>       meshes;
-RsrcContainer<Skeleton>   skeletons;
-RsrcContainer<SkelAnim>   skelAnims;
-RsrcContainer<LightData> lightProps;
-RsrcContainer<ParticleEmitterProps> particleEmitterProps;
-RsrcContainer<Script> scripts;
-RsrcContainer<Model> models;
-RsrcContainer<Skin> skins;
-
-}

+ 0 - 265
src/Resources/Core/RsrcPtr.cpp

@@ -1,265 +0,0 @@
-#include "RsrcPtr.h"
-#include "RsrcContainer.h"
-#include "Texture.h"
-#include "ShaderProg.h"
-#include "Material.h"
-#include "Mesh.h"
-#include "Skeleton.h"
-#include "SkelAnim.h"
-#include "LightData.h"
-#include "ParticleEmitterProps.h"
-#include "Script.h"
-#include "Model.h"
-#include "Skin.h"
-
-
-namespace RsrcContainers {
-
-extern RsrcContainer<Texture> textures;
-extern RsrcContainer<ShaderProg> shaderProgs;
-extern RsrcContainer<Material> materials;
-extern RsrcContainer<Mesh> meshes;
-extern RsrcContainer<Skeleton> skeletons;
-extern RsrcContainer<SkelAnim> skelAnims;
-extern RsrcContainer<LightData> lightProps;
-extern RsrcContainer<ParticleEmitterProps> particleEmitterProps;
-extern RsrcContainer<Script> scripts;
-extern RsrcContainer<Model> models;
-extern RsrcContainer<Skin> skins;
-
-}
-
-
-#define LOAD_RSRC(container) \
-	p = RsrcContainers::container.load(filename); \
-	return p != NULL;
-
-
-#define UNLOAD_RSRC(container) \
-	if(p) \
-	{ \
-		RsrcContainers::container.unload(p); \
-		p = NULL; \
-	}
-
-
-//======================================================================================================================
-// loadRsrc <Texture>                                                                                                  =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Texture>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(textures);
-}
-
-
-//======================================================================================================================
-// loadRsrc <ShaderProg>                                                                                               =
-//======================================================================================================================
-template<>
-bool RsrcPtr<ShaderProg>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(shaderProgs);
-}
-
-
-//======================================================================================================================
-// loadRsrc <Material>                                                                                                 =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Material>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(materials);
-}
-
-
-//======================================================================================================================
-// loadRsrc <Mesh>                                                                                                     =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Mesh>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(meshes);
-}
-
-
-//======================================================================================================================
-// loadRsrc <Skeleton>                                                                                                 =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Skeleton>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(skeletons);
-}
-
-
-//======================================================================================================================
-// loadRsrc <SkelAnim>                                                                                                 =
-//======================================================================================================================
-template<>
-bool RsrcPtr<SkelAnim>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(skelAnims);
-}
-
-
-//======================================================================================================================
-// loadRsrc <LightProp>                                                                                                =
-//======================================================================================================================
-template<>
-bool RsrcPtr<LightData>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(lightProps);
-}
-
-
-//======================================================================================================================
-// loadRsrc <ParticleEmitterProps>                                                                                     =
-//======================================================================================================================
-template<>
-bool RsrcPtr<ParticleEmitterProps>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(particleEmitterProps);
-}
-
-
-//======================================================================================================================
-// loadRsrc <Script>                                                                                                   =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Script>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(scripts);
-}
-
-
-//======================================================================================================================
-// loadRsrc <Model>                                                                                                    =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Model>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(models);
-}
-
-
-//======================================================================================================================
-// loadRsrc <Skin>                                                                                                    =
-//======================================================================================================================
-template<>
-bool RsrcPtr<Skin>::loadRsrc(const char* filename)
-{
-	LOAD_RSRC(skins);
-}
-
-//----------------------------------------------------------------------------------------------------------------------
-
-//======================================================================================================================
-// unload <Texture>                                                                                                    =
-//======================================================================================================================
-template<>
-void RsrcPtr<Texture>::unload()
-{
-	UNLOAD_RSRC(textures);
-}
-
-
-//======================================================================================================================
-// unload <ShaderProg>                                                                                                 =
-//======================================================================================================================
-template<>
-void RsrcPtr<ShaderProg>::unload()
-{
-	UNLOAD_RSRC(shaderProgs);
-}
-
-
-//======================================================================================================================
-// unload <Material>                                                                                                   =
-//======================================================================================================================
-template<>
-void RsrcPtr<Material>::unload()
-{
-	UNLOAD_RSRC(materials);
-}
-
-
-//======================================================================================================================
-// unload <Mesh>                                                                                                       =
-//======================================================================================================================
-template<>
-void RsrcPtr<Mesh>::unload()
-{
-	UNLOAD_RSRC(meshes);
-}
-
-
-//======================================================================================================================
-// unload <Skeleton>                                                                                                   =
-//======================================================================================================================
-template<>
-void RsrcPtr<Skeleton>::unload()
-{
-	UNLOAD_RSRC(skeletons);
-}
-
-
-
-//======================================================================================================================
-// unload <SkelAnim>                                                                                                   =
-//======================================================================================================================
-template<>
-void RsrcPtr<SkelAnim>::unload()
-{
-	UNLOAD_RSRC(skelAnims);
-}
-
-
-//======================================================================================================================
-// unload <LightProps>                                                                                                 =
-//======================================================================================================================
-template<>
-void RsrcPtr<LightData>::unload()
-{
-	UNLOAD_RSRC(lightProps);
-}
-
-
-//======================================================================================================================
-// unload <ParticleEmitterProps>                                                                                       =
-//======================================================================================================================
-template<>
-void RsrcPtr<ParticleEmitterProps>::unload()
-{
-	UNLOAD_RSRC(particleEmitterProps);
-}
-
-
-//======================================================================================================================
-// unload <Script>                                                                                                     =
-//======================================================================================================================
-template<>
-void RsrcPtr<Script>::unload()
-{
-	UNLOAD_RSRC(scripts);
-}
-
-
-//======================================================================================================================
-// unload <Model>                                                                                                      =
-//======================================================================================================================
-template<>
-void RsrcPtr<Model>::unload()
-{
-	UNLOAD_RSRC(models);
-}
-
-
-//======================================================================================================================
-// unload <Skin>                                                                                                      =
-//======================================================================================================================
-template<>
-void RsrcPtr<Skin>::unload()
-{
-	UNLOAD_RSRC(skins);
-}

+ 9 - 37
src/Resources/Core/RsrcPtr.h

@@ -1,7 +1,7 @@
 #ifndef RSRC_PTR_H
 #define RSRC_PTR_H
 
-#include "Exception.h"
+#include "ResourceManager.h"
 
 
 /// This is a special smart pointer that points to Resource derivatives. It looks like auto_ptr but the main difference
@@ -11,61 +11,33 @@ class RsrcPtr
 {
 	public:
 		/// Default constructor
-		RsrcPtr(): p(NULL) {}
+		RsrcPtr(): hook(NULL) {}
 
 		/// Copy constructor
-		RsrcPtr(const RsrcPtr& a);
+		RsrcPtr(const RsrcPtr<Type>& a);
 
-		/// It unloads the resource or it decreases its reference counter
 		~RsrcPtr() {unload();}
 
 		/// Loads a resource and sets the RsrcPtr::p. The implementation of the function is different for every Resource
 		/// (see RsrcPtr.cpp)
 		/// @param filename
 		/// @return True on success
-		bool loadRsrc(const char* filename);
+		void loadRsrc(const char* filename);
 
 		Type& operator*() const;
 		Type* operator->() const;
-		Type* get() const {return p;}
+		Type* get() const {return hook->resource;}
+		const std::string& getRsrcName() const;
 
 	private:
+		RsrcHook<Type>* hook; ///< Points to a container in the resource manager
+
 		/// Unloads the resource @see loadRsrc
 		void unload();
-
-		Type* p; ///< Pointer to the Resource derivative
 };
 
 
-//======================================================================================================================
-// Inlines                                                                                                             =
-//======================================================================================================================
-
-template<typename Type>
-RsrcPtr<Type>::RsrcPtr(const RsrcPtr& a):
-	p(a.p)
-{
-	if(p)
-	{
-		++p->referenceCounter;
-	}
-}
-
-
-template<typename Type>
-Type& RsrcPtr<Type>::operator*() const
-{
-	RASSERT_THROW_EXCEPTION(p == NULL);
-	return *p;
-}
-
-
-template<typename Type>
-Type* RsrcPtr<Type>::operator->() const
-{
-	RASSERT_THROW_EXCEPTION(p == NULL);
-	return p;
-}
+#include "RsrcPtr.inl.h"
 
 
 #endif

+ 74 - 0
src/Resources/Core/RsrcPtr.inl.h

@@ -0,0 +1,74 @@
+#include "RsrcPtr.h"
+#include "Exception.h"
+
+
+//======================================================================================================================
+// Constructor                                                                                                         =
+//======================================================================================================================
+template<typename Type>
+RsrcPtr<Type>::RsrcPtr(const RsrcPtr& a):
+	hook(a.hook)
+{
+	if(hook)
+	{
+		++hook->referenceCounter;
+	}
+}
+
+
+//======================================================================================================================
+// operator*                                                                                                           =
+//======================================================================================================================
+template<typename Type>
+Type& RsrcPtr<Type>::operator*() const
+{
+	RASSERT_THROW_EXCEPTION(hook == NULL);
+	return *hook->resource;
+}
+
+
+//======================================================================================================================
+// operator->                                                                                                          =
+//======================================================================================================================
+template<typename Type>
+Type* RsrcPtr<Type>::operator->() const
+{
+	RASSERT_THROW_EXCEPTION(hook == NULL);
+	return hook->resource;
+}
+
+
+//======================================================================================================================
+// loadRsrc                                                                                                            =
+//======================================================================================================================
+template<typename Type>
+void RsrcPtr<Type>::loadRsrc(const char* filename)
+{
+	RASSERT_THROW_EXCEPTION(hook != NULL);
+	hook = &ResourceManagerSingleton::getInstance().load<Type>(filename);
+}
+
+
+//======================================================================================================================
+// unload                                                                                                              =
+//======================================================================================================================
+template<typename Type>
+void RsrcPtr<Type>::unload()
+{
+	if(hook != NULL)
+	{
+		ResourceManagerSingleton::getInstance().unload<Type>(*hook);
+		hook = NULL;
+	}
+}
+
+
+//======================================================================================================================
+// getRsrcName                                                                                                         =
+//======================================================================================================================
+template<typename Type>
+const std::string& RsrcPtr<Type>::getRsrcName() const
+{
+	RASSERT_THROW_EXCEPTION(hook == NULL);
+	return hook->uuid;
+}

+ 3 - 0
src/Resources/DummyRsrc.cpp

@@ -0,0 +1,3 @@
+#include "DummyRsrc.h"
+
+int DummyRsrc::mem = 0;

+ 45 - 0
src/Resources/DummyRsrc.h

@@ -0,0 +1,45 @@
+#ifndef DUMMY_RSRC_H
+#define DUMMY_RSRC_H
+
+
+/// A dummy resource for the unit tests of the ResourceManager
+class DummyRsrc
+{
+	public:
+		DummyRsrc();
+		~DummyRsrc();
+
+		void load(const char* filename);
+
+		static int getMem() {return mem;}
+	private:
+		static int mem;
+		bool loaded;
+};
+
+
+inline DummyRsrc::DummyRsrc()
+{
+	++mem;
+	loaded = false;
+}
+
+
+inline DummyRsrc::~DummyRsrc()
+{
+	--mem;
+	if(loaded)
+	{
+		--mem;
+	}
+}
+
+
+inline void DummyRsrc::load(const char* /*filename*/)
+{
+	++mem;
+	loaded = true;
+}
+
+
+#endif

+ 1 - 0
src/Resources/Extension.cpp

@@ -1,5 +1,6 @@
 #include <dlfcn.h>
 #include "Extension.h"
+#include "Exception.h"
 
 
 //======================================================================================================================

+ 2 - 3
src/Resources/Extension.h

@@ -1,11 +1,11 @@
 #ifndef EXTENSION_H
 #define EXTENSION_H
 
-#include "Resource.h"
+#include "StdTypes.h"
 
 
 /// Extension @ref Resource resource
-class Extension: public Resource
+class Extension
 {
 	private:
 		void* libHandle;
@@ -21,7 +21,6 @@ class Extension: public Resource
 
 
 inline Extension::Extension():
-	Resource(RT_EXTENSION),
 	libHandle(NULL),
 	foobarPtr(NULL)
 {}

+ 0 - 1
src/Resources/LightData.cpp

@@ -8,7 +8,6 @@
 // Constructor                                                                                                         =
 //======================================================================================================================
 LightData::LightData():
-	Resource(RT_LIGHT_PROPS),
 	diffuseCol(0.5),
 	specularCol(0.5),
 	castsShadow_(false),

+ 2 - 2
src/Resources/LightData.h

@@ -1,14 +1,14 @@
 #ifndef LIGHT_PROPS_H
 #define LIGHT_PROPS_H
 
-#include "Resource.h"
 #include "Math.h"
 #include "RsrcPtr.h"
 #include "Texture.h"
+#include "Properties.h"
 
 
 /// Light properties Resource
-class LightData: public Resource
+class LightData
 {
 	public:
 		enum LightType

+ 1 - 3
src/Resources/Material.cpp

@@ -4,7 +4,6 @@
 #include <boost/foreach.hpp>
 #include <boost/lexical_cast.hpp>
 #include "Material.h"
-#include "Resource.h"
 #include "Parser.h"
 #include "Texture.h"
 #include "ShaderProg.h"
@@ -113,8 +112,7 @@ static bool searchBlendEnum(const char* str, int& gl_enum)
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Material::Material():
-	Resource(RT_MATERIAL)
+Material::Material()
 {
 	blendingStage = false;
 	blendingSfactor = GL_ONE;

+ 1 - 2
src/Resources/Material.h

@@ -4,7 +4,6 @@
 #include <boost/ptr_container/ptr_vector.hpp>
 #include <boost/property_tree/ptree_fwd.hpp>
 #include "Math.h"
-#include "Resource.h"
 #include "ShaderProg.h"
 #include "RsrcPtr.h"
 #include "MtlUserDefinedVar.h"
@@ -67,7 +66,7 @@
 /// *: Has nothing to do with the blendingStage. blendFuncs can be in material stage as well
 /// **: Depends on the type of the var
 /// @endcode
-class Material: public Resource
+class Material
 {
 	//====================================================================================================================
 	// Nested                                                                                                            =

+ 0 - 9
src/Resources/Mesh.cpp

@@ -6,15 +6,6 @@
 #include "Vbo.h"
 
 
-//======================================================================================================================
-// Constructor                                                                                                         =
-//======================================================================================================================
-Mesh::Mesh():
-	Resource(RT_MESH),
-	Object(NULL)
-{}
-
-
 //======================================================================================================================
 // load                                                                                                                =
 //======================================================================================================================

+ 3 - 4
src/Resources/Mesh.h

@@ -3,17 +3,16 @@
 
 #include <boost/array.hpp>
 #include "Math.h"
-#include "Resource.h"
 #include "RsrcPtr.h"
-#include "Object.h"
 #include "Vbo.h"
+#include "Properties.h"
 
 
 class MeshData;
 
 
 /// Mesh Resource. It contains the geometry packed in VBOs
-class Mesh: public Resource, public Object
+class Mesh
 {
 	public:
 		/// Used in @ref vbos array
@@ -32,7 +31,7 @@ class Mesh: public Resource, public Object
 
 	public:
 		/// Default constructor
-		Mesh();
+		Mesh() {}
 
 		/// Does nothing
 		~Mesh() {}

+ 2 - 3
src/Resources/Model.h

@@ -2,7 +2,6 @@
 #define MODEL_H
 
 #include <boost/ptr_container/ptr_vector.hpp>
-#include "Resource.h"
 #include "RsrcPtr.h"
 #include "Vao.h"
 #include "ModelPatch.h"
@@ -29,10 +28,10 @@
 /// - If the materials need texture coords then mesh should have them
 /// - The skeleton and skelAnims are optional
 /// - Its an error to have skelAnims without skeleton
-class Model: public Resource
+class Model
 {
 	public:
-		Model(): Resource(RT_MODEL) {}
+		Model() {}
 
 		void load(const char* filename);
 

+ 1 - 1
src/Resources/ModelPatch.cpp

@@ -73,6 +73,6 @@ void ModelPatch::doMeshAndMtlSanityChecks(const Mesh& mesh, const Material& mtl)
 	}
 	catch(std::exception& e)
 	{
-		throw EXCEPTION("Resource \"" + mesh.getRsrcName() + "\" and \"" + mtl.getRsrcName() + "\" are incompatible");
+		throw EXCEPTION("Mesh and material are incompatible");
 	}
 }

+ 1 - 0
src/Resources/MtlUserDefinedVar.cpp

@@ -1,4 +1,5 @@
 #include "MtlUserDefinedVar.h"
+#include "Texture.h"
 
 
 //======================================================================================================================

+ 1 - 1
src/Resources/ParticleEmitterProps.cpp

@@ -5,7 +5,7 @@
 static const char* errMsg = "Incorrect or missing value ";
 
 
-#define PE_EXCEPTION(x) EXCEPTION("File \"" + getRsrcPath() + getRsrcName() + "\": " + x)
+#define PE_EXCEPTION(x) EXCEPTION("Particle emmiter: " + x)
 
 
 //======================================================================================================================

+ 1 - 5
src/Resources/ParticleEmitterProps.h

@@ -2,7 +2,6 @@
 #define PARTICLE_EMITTER_PROPS_H
 
 #include "Math.h"
-#include "Resource.h"
 
 
 /// This is the properties of the particle emitter resource. Its a separate class from ParticleEmitterProps cause
@@ -49,12 +48,9 @@ class ParticleEmitterPropsStruct
 
 
 /// The actual particle emitter resource
-class ParticleEmitterProps: public ParticleEmitterPropsStruct, public Resource
+class ParticleEmitterProps: public ParticleEmitterPropsStruct
 {
 	public:
-		ParticleEmitterProps(): Resource(RT_PARTICLE_EMITTER_PROPS) {}
-		~ParticleEmitterProps() {}
-
 		void load(const char* filename);
 };
 

+ 1 - 2
src/Resources/Path.h

@@ -1,12 +1,11 @@
 #ifndef PATH_H
 #define PATH_H
 
-#include "Resource.h"
 #include "Math.h"
 
 
 /// Path @ref Resource resource
-class Path: public Resource
+class Path
 {
 	public:
 		Vec<Vec3> positions; ///< AKA translations

+ 1 - 4
src/Resources/Script.h

@@ -2,15 +2,12 @@
 #define SCRIPT_H
 
 #include <string>
-#include "Resource.h"
 
 
 /// Python script resource
-class Script: public Resource
+class Script
 {
 	public:
-		Script(): Resource(RT_SCRIPT) {}
-		~Script() {}
 		void load(const char* filename);
 
 	private:

+ 5 - 3
src/Resources/ShaderProg.cpp

@@ -6,9 +6,10 @@
 #include "App.h" // To get cache dir
 #include "GlException.h"
 #include "Logger.h"
+#include "Util.h"
 
 
-#define SPROG_EXCEPTION(x) EXCEPTION("Shader prog \"" + getRsrcName() + "\": " + x)
+#define SPROG_EXCEPTION(x) EXCEPTION("Shader prog \"" + rsrcFilename + "\": " + x)
 
 
 //======================================================================================================================
@@ -126,7 +127,7 @@ void ShaderProg::getUniAndAttribVars()
 		int loc = glGetAttribLocation(glId, name_);
 		if(loc == -1) // if -1 it means that its an FFP var
 		{
-			WARNING("Shader prog: \"" << getRsrcName() << "\": You are using FFP vertex attributes (\"" << name_ << "\")");
+			WARNING("Shader prog: \"" << rsrcFilename << "\": You are using FFP vertex attributes (\"" << name_ << "\")");
 			continue;
 		}
 
@@ -147,7 +148,7 @@ void ShaderProg::getUniAndAttribVars()
 		int loc = glGetUniformLocation(glId, name_);
 		if(loc == -1) // if -1 it means that its an FFP var
 		{
-			WARNING("Shader prog: \"" << getRsrcName() << "\": You are using FFP vertex uniforms (\"" << name_ << "\")");
+			WARNING("Shader prog: \"" << rsrcFilename << "\": You are using FFP vertex uniforms (\"" << name_ << "\")");
 			continue;
 		}
 
@@ -187,6 +188,7 @@ void ShaderProg::bindCustomAttribLocs(const ShaderPrePreprocessor& pars) const
 //======================================================================================================================
 void ShaderProg::load(const char* filename)
 {
+	rsrcFilename = filename;
 	RASSERT_THROW_EXCEPTION(glId != std::numeric_limits<uint>::max());
 
 	ShaderPrePreprocessor pars(filename);

+ 6 - 9
src/Resources/ShaderProg.h

@@ -3,11 +3,11 @@
 
 #include <GL/glew.h>
 #include <limits>
-#include "Resource.h"
 #include "CharPtrHashMap.h"
 #include "Exception.h"
 #include "SProgUniVar.h"
 #include "SProgAttribVar.h"
+#include "Vec.h"
 
 
 /// Shader program @ref Resource
@@ -15,11 +15,8 @@
 /// Shader program. Combines a fragment and a vertex shader. Every shader program consist of one OpenGL ID, a vector of
 /// uniform variables and a vector of attribute variables. Every variable is a struct that contains the variable's name,
 /// location, OpenGL data type and if it is a uniform or an attribute var.
-class ShaderProg: public Resource
+class ShaderProg
 {
-	friend class Material;
-	friend class RsrcContainer<ShaderProg>;
-		
 	private:
 		/// Uniform variable name to variable iterator
 		typedef CharPtrHashMap<SProgUniVar*>::const_iterator NameToSProgUniVarIterator;
@@ -33,6 +30,9 @@ class ShaderProg: public Resource
 		ShaderProg();
 		~ShaderProg() {/** @todo add code */}
 
+		/// Resource load
+		void load(const char* filename);
+
 		/// Accessor to glId
 		GLuint getGlId() const;
 
@@ -85,6 +85,7 @@ class ShaderProg: public Resource
 	// Private                                                                                                           =
 	//====================================================================================================================
 	private:
+		std::string rsrcFilename;
 		GLuint glId; ///< The OpenGL ID of the shader program
 		GLuint vertShaderGlId; ///< Vertex shader OpenGL id
 		GLuint geomShaderGlId; ///< Geometry shader OpenGL id
@@ -111,9 +112,6 @@ class ShaderProg: public Resource
 		/// Link the shader program
 		/// @exception Exception
 		void link() const;
-
-		/// Resource load
-		void load(const char* filename);
 }; 
 
 
@@ -122,7 +120,6 @@ class ShaderProg: public Resource
 //======================================================================================================================
 
 inline ShaderProg::ShaderProg():
-	Resource(RT_SHADER_PROG),
 	glId(std::numeric_limits<uint>::max())
 {}
 

+ 2 - 5
src/Resources/SkelAnim.h

@@ -1,8 +1,8 @@
 #ifndef SKEL_ANIM_H
 #define SKEL_ANIM_H
 
-#include "Resource.h"
 #include "Math.h"
+#include "Vec.h"
 
 
 /// Skeleton animation resource
@@ -41,7 +41,7 @@
 /// 	}
 /// }
 /// @endcode
-class SkelAnim: public Resource
+class SkelAnim
 {
 	public:
 		/// Bone pose
@@ -66,9 +66,6 @@ class SkelAnim: public Resource
 		uint framesNum;
 		Vec<BoneAnim> bones;
 
-		SkelAnim(): Resource(RT_SKEL_ANIM) {}
-		~SkelAnim() {}
-
 		/// Implements Resource::loat
 		void load(const char* filename);
 };

+ 3 - 5
src/Resources/Skeleton.h

@@ -2,7 +2,8 @@
 #define SKELETON_H
 
 #include "Math.h"
-#include "Resource.h"
+#include "Properties.h"
+#include "Vec.h"
 
 
 /// It contains the bones with their position and hierarchy
@@ -26,7 +27,7 @@
 /// child:
 /// uint: bone id
 /// @endcode
-class Skeleton: public Resource
+class Skeleton
 {
 	public:
 		/// Skeleton bone
@@ -63,9 +64,6 @@ class Skeleton: public Resource
 	
 		Vec<Bone> bones;
 
-		 Skeleton(): Resource(RT_SKELETON) {}
-		~Skeleton() {}
-
 		/// Implements Resource::load
 		void load(const char* filename);
 };

+ 3 - 4
src/Resources/Skin.cpp

@@ -58,8 +58,8 @@ void Skin::load(const char* filename)
 			// Bone number problem
 			if(skelAnims[i]->bones.size() != skeleton->bones.size())
 			{
-				throw EXCEPTION("Skeleton animation \"" + skelAnims[i]->getRsrcName() + "\" and skeleton \"" +
-				                skeleton->getRsrcName() + "\" dont have equal bone count");
+				throw EXCEPTION("Skeleton animation \"" + skelAnims[i].getRsrcName() + "\" and skeleton \"" +
+				                skeleton.getRsrcName() + "\" dont have equal bone count");
 			}
 		}
 
@@ -68,8 +68,7 @@ void Skin::load(const char* filename)
 		{
 			if(!model->getModelPatches()[i].supportsHwSkinning())
 			{
-				throw EXCEPTION("Mesh " + model->getModelPatches()[i].getMesh().getRsrcName() +
-				                " does not support HW skinning");
+				throw EXCEPTION("Mesh does not support HW skinning");
 			}
 		}
   }

+ 1 - 5
src/Resources/Skin.h

@@ -1,7 +1,6 @@
 #ifndef SKIN_H
 #define SKIN_H
 
-#include "Resource.h"
 #include "RsrcPtr.h"
 #include "Model.h"
 
@@ -22,12 +21,9 @@ class SkelAnim;
 /// 	</skelAnims>
 /// </skin>
 /// @endcode
-class Skin: public Resource
+class Skin
 {
 	public:
-		/// Nothing special
-		Skin(): Resource(RT_SKIN) {}
-
 		/// Implements Resource::load
 		void load(const char*);
 

+ 0 - 1
src/Resources/Texture.cpp

@@ -20,7 +20,6 @@ int Texture::anisotropyLevel = 8;
 // Constructor                                                                                                         =
 //======================================================================================================================
 Texture::Texture():
-	Resource(RT_TEXTURE),
 	glId(std::numeric_limits<uint>::max()),
 	target(GL_TEXTURE_2D)
 {}

+ 2 - 2
src/Resources/Texture.h

@@ -2,8 +2,8 @@
 #define TEXTURE_H
 
 #include <limits>
-#include "Resource.h"
 #include "StdTypes.h"
+#include "Exception.h"
 
 
 /// Texture resource class
@@ -12,7 +12,7 @@
 /// uncompressed TGAs and some of the formats of PNG (PNG loading uses libpng)
 ///
 /// @note The last texture unit is reserved and you cannot use it
-class Texture: public Resource
+class Texture
 {
 	friend class Renderer; /// @todo Remove this when remove the SSAO load noise map crap
 	friend class Ssao;

+ 5 - 0
src/Util/StdTypes.h

@@ -19,4 +19,9 @@
 #endif
 
 
+#if !defined(NULL)
+	#define NULL 0
+#endif
+
+
 #endif

Some files were not shown because too many files changed in this diff