Просмотр исходного кода

Imported and user-created resources will now get registered in Resources as loaded resources. Meaning "unload" needs to be called to properly release them.

Marko Pintera 12 лет назад
Родитель
Сommit
f0ef4bc642

+ 3 - 2
BansheeEngine/Source/BsSpriteTexture.cpp

@@ -1,6 +1,7 @@
 #include "BsSpriteTexture.h"
 #include "BsSpriteTextureRTTI.h"
 #include "CmTexture.h"
+#include "CmResources.h"
 
 using namespace CamelotFramework;
 
@@ -34,7 +35,7 @@ namespace BansheeEngine
 		SpriteTexturePtr texturePtr = cm_core_ptr<SpriteTexture, PoolAlloc>
 			(new (cm_alloc<SpriteTexture, PoolAlloc>()) SpriteTexture(Vector2(0.0f, 0.0f), Vector2(1.0f, 1.0f), texture));
 
-		return static_resource_cast<SpriteTexture>(Resource::_createResourceHandle(texturePtr));
+		return static_resource_cast<SpriteTexture>(gResources().createResourceHandle(texturePtr));
 	}
 
 	HSpriteTexture SpriteTexture::create(const Vector2& uvOffset, const Vector2& uvScale, const HTexture& texture)
@@ -42,7 +43,7 @@ namespace BansheeEngine
 		SpriteTexturePtr texturePtr = cm_core_ptr<SpriteTexture, PoolAlloc>
 			(new (cm_alloc<SpriteTexture, PoolAlloc>()) SpriteTexture(uvOffset, uvScale, texture));
 
-		return static_resource_cast<SpriteTexture>(Resource::_createResourceHandle(texturePtr));
+		return static_resource_cast<SpriteTexture>(gResources().createResourceHandle(texturePtr));
 	}
 
 	SpriteTexturePtr SpriteTexture::createEmpty()

+ 0 - 5
CamelotCore/Include/CmResource.h

@@ -15,11 +15,6 @@ namespace CamelotFramework
 		Resource(bool requiresGpuInitialization = true);
 		virtual ~Resource() {};
 
-		/**
-		 * @brief	Creates a new resource handle. Should only be called by internal methods.
-		 */
-		static HResource _createResourceHandle(ResourcePtr obj);
-
 	protected:
 		friend class Resources;
 

+ 1 - 1
CamelotCore/Include/CmResourceHandle.h

@@ -141,7 +141,7 @@ namespace CamelotFramework
 		}
 
 	private:
-		friend class Resource;
+		friend class Resources;
 
 		explicit ResourceHandle(T* ptr, const String& uuid)
 			:ResourceHandleBase()

+ 7 - 0
CamelotCore/Include/CmResources.h

@@ -119,6 +119,13 @@ namespace CamelotFramework
 		 */
 		void save(HResource resource, const WString& filePath, bool overwrite);
 
+		/**
+		 * @brief	Creates a new resource handle from a resource pointer. 
+		 * 			You will almost never need to call this manually and should instead use
+		 * 			resource-specific methods that return a resource handle in the first place.
+		 */
+		HResource createResourceHandle(const ResourcePtr& obj);
+
 		/**
 		 * @brief	Allows you to set a resource manifest containing UUID <-> file path mapping that is
 		 * 			used when resolving resource references.

+ 2 - 1
CamelotCore/Source/CmBlendState.cpp

@@ -2,6 +2,7 @@
 #include "CmRenderStateManager.h"
 #include "CmRenderSystem.h"
 #include "CmBlendStateRTTI.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -77,7 +78,7 @@ namespace CamelotFramework
 	{
 		BlendStatePtr blendStatePtr = RenderStateManager::instance().createBlendState(desc);
 
-		return static_resource_cast<BlendState>(Resource::_createResourceHandle(blendStatePtr));
+		return static_resource_cast<BlendState>(gResources().createResourceHandle(blendStatePtr));
 	}
 
 	/************************************************************************/

+ 2 - 1
CamelotCore/Source/CmDepthStencilState.cpp

@@ -3,6 +3,7 @@
 #include "CmRenderSystem.h"
 #include "CmDepthStencilStateRTTI.h"
 #include "CmException.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -22,7 +23,7 @@ namespace CamelotFramework
 	{
 		DepthStencilStatePtr depthStencilPtr = RenderStateManager::instance().createDepthStencilState(desc);
 
-		return static_resource_cast<DepthStencilState>(Resource::_createResourceHandle(depthStencilPtr));
+		return static_resource_cast<DepthStencilState>(gResources().createResourceHandle(depthStencilPtr));
 	}
 
 	/************************************************************************/

+ 2 - 1
CamelotCore/Source/CmFont.cpp

@@ -1,6 +1,7 @@
 #include "CmFont.h"
 #include "CmFontRTTI.h"
 #include "CmFontManager.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -86,7 +87,7 @@ namespace CamelotFramework
 	{
 		FontPtr newFont = _createPtr(fontData);
 
-		return Resource::_createResourceHandle(newFont);
+		return gResources().createResourceHandle(newFont);
 	}
 
 	FontPtr Font::_createPtr(const Vector<FontData>::type& fontData)

+ 2 - 1
CamelotCore/Source/CmGpuProgInclude.cpp

@@ -1,4 +1,5 @@
 #include "CmGpuProgInclude.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -10,7 +11,7 @@ namespace CamelotFramework
 
 	HGpuProgInclude GpuProgInclude::create(const String& includeString)
 	{
-		return static_resource_cast<GpuProgInclude>(Resource::_createResourceHandle(_createPtr(includeString)));
+		return static_resource_cast<GpuProgInclude>(gResources().createResourceHandle(_createPtr(includeString)));
 	}
 
 	GpuProgIncludePtr GpuProgInclude::_createPtr(const String& includeString)

+ 2 - 1
CamelotCore/Source/CmHighLevelGpuProgram.cpp

@@ -30,6 +30,7 @@ THE SOFTWARE.
 #include "CmException.h"
 #include "CmCoreThread.h"
 #include "CmAsyncOp.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -70,7 +71,7 @@ namespace CamelotFramework
 	{
 		HighLevelGpuProgramPtr programPtr = _createPtr(source, entryPoint, language, gptype, profile, includes);
 
-		return static_resource_cast<HighLevelGpuProgram>(Resource::_createResourceHandle(programPtr));
+		return static_resource_cast<HighLevelGpuProgram>(gResources().createResourceHandle(programPtr));
 	}
 
 	HighLevelGpuProgramPtr HighLevelGpuProgram::_createPtr(const String& source, const String& entryPoint, 

+ 2 - 1
CamelotCore/Source/CmImporter.cpp

@@ -10,6 +10,7 @@
 #include "CmDataStream.h"
 #include "CmException.h"
 #include "CmUUID.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -77,7 +78,7 @@ namespace CamelotFramework
 		}
 
 		ResourcePtr importedResource = importer->import(inputFilePath, importOptions);
-		return Resource::_createResourceHandle(importedResource);
+		return gResources().createResourceHandle(importedResource);
 	}
 
 	void Importer::reimport(HResource& existingResource, const WString& inputFilePath, ConstImportOptionsPtr importOptions)

+ 3 - 2
CamelotCore/Source/CmMaterial.cpp

@@ -12,6 +12,7 @@
 #include "CmMaterialRTTI.h"
 #include "CmMaterialManager.h"
 #include "CmDebug.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -713,14 +714,14 @@ namespace CamelotFramework
 	{
 		MaterialPtr materialPtr = MaterialManager::instance().create();
 
-		return static_resource_cast<Material>(Resource::_createResourceHandle(materialPtr));
+		return static_resource_cast<Material>(gResources().createResourceHandle(materialPtr));
 	}
 
 	HMaterial Material::create(ShaderPtr shader)
 	{
 		MaterialPtr materialPtr = MaterialManager::instance().create(shader);
 
-		return static_resource_cast<Material>(Resource::_createResourceHandle(materialPtr));
+		return static_resource_cast<Material>(gResources().createResourceHandle(materialPtr));
 	}
 
 	RTTITypeBase* Material::getRTTIStatic()

+ 4 - 3
CamelotCore/Source/CmMesh.cpp

@@ -10,6 +10,7 @@
 #include "CmAsyncOp.h"
 #include "CmAABox.h"
 #include "CmVertexDataDesc.h"
+#include "CmResources.h"
 
 #include "CmProfiler.h"
 
@@ -354,7 +355,7 @@ namespace CamelotFramework
 	{
 		MeshPtr meshPtr = _createPtr(numVertices, numIndices, vertexDesc, bufferType, drawOp, indexType);
 
-		return static_resource_cast<Mesh>(Resource::_createResourceHandle(meshPtr));
+		return static_resource_cast<Mesh>(gResources().createResourceHandle(meshPtr));
 	}
 
 	HMesh Mesh::create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, 
@@ -363,14 +364,14 @@ namespace CamelotFramework
 		MeshPtr meshPtr = _createPtr(numVertices, numIndices, vertexDesc, 
 			initialMeshData, bufferType, drawOp, indexType);
 
-		return static_resource_cast<Mesh>(Resource::_createResourceHandle(meshPtr));
+		return static_resource_cast<Mesh>(gResources().createResourceHandle(meshPtr));
 	}
 
 	HMesh Mesh::create(const MeshDataPtr& initialMeshData, MeshBufferType bufferType, DrawOperationType drawOp)
 	{
 		MeshPtr meshPtr = _createPtr(initialMeshData, bufferType, drawOp);
 
-		return static_resource_cast<Mesh>(Resource::_createResourceHandle(meshPtr));
+		return static_resource_cast<Mesh>(gResources().createResourceHandle(meshPtr));
 	}
 
 	MeshPtr Mesh::_createPtr(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, 

+ 2 - 1
CamelotCore/Source/CmRasterizerState.cpp

@@ -2,6 +2,7 @@
 #include "CmRenderStateManager.h"
 #include "CmRenderSystem.h"
 #include "CmRasterizerStateRTTI.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -21,7 +22,7 @@ namespace CamelotFramework
 	{
 		RasterizerStatePtr rasterizerPtr = RenderStateManager::instance().createRasterizerState(desc);
 
-		return static_resource_cast<RasterizerState>(Resource::_createResourceHandle(rasterizerPtr));
+		return static_resource_cast<RasterizerState>(gResources().createResourceHandle(rasterizerPtr));
 	}
 
 	/************************************************************************/

+ 3 - 2
CamelotCore/Source/CmRenderTexture.cpp

@@ -31,6 +31,7 @@ THE SOFTWARE.
 #include "CmPixelBuffer.h"
 #include "CmTexture.h"
 #include "CmTextureManager.h"
+#include "CmResources.h"
 
 namespace CamelotFramework
 {
@@ -121,10 +122,10 @@ namespace CamelotFramework
 		// Create non-persistent resource handles for the used textures (we only need them because a lot of the code accepts only handles,
 		// since they're non persistent they don't really have any benefit over shared pointers)
 		if(mColorSurface != nullptr)
-			mBindableColorTex = Resource::_createResourceHandle(mColorSurface->getTexture());
+			mBindableColorTex = gResources().createResourceHandle(mColorSurface->getTexture());
 
 		if(mDepthStencilSurface != nullptr)
-			mBindableDepthStencilTex = Resource::_createResourceHandle(mDepthStencilSurface->getTexture());
+			mBindableDepthStencilTex = gResources().createResourceHandle(mDepthStencilSurface->getTexture());
 	}
 
 	void RenderTexture::throwIfBuffersDontMatch() const

+ 1 - 5
CamelotCore/Source/CmResource.cpp

@@ -1,5 +1,6 @@
 #include "CmResource.h"
 #include "CmResourceRTTI.h"
+#include "CmResources.h"
 #include "CmUUID.h"
 #include "CmRenderSystem.h"
 
@@ -9,11 +10,6 @@ namespace CamelotFramework
 		:CoreObject(initializeOnRenderThread), mSize(0)
 	{ }
 
-	HResource Resource::_createResourceHandle(ResourcePtr obj)
-	{
-		return HResource(obj, UUIDGenerator::generateRandom());
-	}
-
 	RTTITypeBase* Resource::getRTTIStatic()
 	{
 		return ResourceRTTI::instance();

+ 14 - 0
CamelotCore/Source/CmResources.cpp

@@ -266,6 +266,20 @@ namespace CamelotFramework
 		fs.encode(resource.get(), filePath);
 	}
 
+	HResource Resources::createResourceHandle(const ResourcePtr& obj)
+	{
+		String uuid = UUIDGenerator::generateRandom();
+		HResource newHandle(obj, uuid);
+
+		{
+			CM_LOCK_MUTEX(mLoadedResourceMutex);
+
+			mLoadedResources[uuid] = newHandle;
+		}
+	
+		return newHandle;
+	}
+
 	const WString& Resources::getPathFromUUID(const String& uuid) const
 	{
 		return mResourceManifest->uuidToFilePath(uuid);

+ 2 - 1
CamelotCore/Source/CmSamplerState.cpp

@@ -3,6 +3,7 @@
 #include "CmRenderStateManager.h"
 #include "CmRenderSystem.h"
 #include "CmException.h"
+#include "CmResources.h"
 
 namespace CamelotFramework 
 {
@@ -42,7 +43,7 @@ namespace CamelotFramework
 	{
 		SamplerStatePtr samplerPtr = RenderStateManager::instance().createSamplerState(desc);
 
-		return static_resource_cast<SamplerState>(Resource::_createResourceHandle(samplerPtr));
+		return static_resource_cast<SamplerState>(gResources().createResourceHandle(samplerPtr));
 	}
 
 	/************************************************************************/

+ 3 - 2
CamelotCore/Source/CmTexture.cpp

@@ -33,6 +33,7 @@ THE SOFTWARE.
 #include "CmDebug.h"
 #include "CmCoreThread.h"
 #include "CmAsyncOp.h"
+#include "CmResources.h"
 
 namespace CamelotFramework 
 {
@@ -331,7 +332,7 @@ namespace CamelotFramework
 		TexturePtr texturePtr = _createPtr(texType, 
 			width, height, depth, num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
 
-		return static_resource_cast<Texture>(Resource::_createResourceHandle(texturePtr));
+		return static_resource_cast<Texture>(gResources().createResourceHandle(texturePtr));
 	}
 	
 	HTexture Texture::create(TextureType texType, UINT32 width, UINT32 height, 
@@ -340,7 +341,7 @@ namespace CamelotFramework
 		TexturePtr texturePtr = _createPtr(texType, 
 			width, height, num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
 
-		return static_resource_cast<Texture>(Resource::_createResourceHandle(texturePtr));
+		return static_resource_cast<Texture>(gResources().createResourceHandle(texturePtr));
 	}
 
 	TexturePtr Texture::_createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,