Ver código fonte

Textures save/load - tested

Marko Pintera 13 anos atrás
pai
commit
a9af94b318

+ 2 - 0
CamelotRenderer/Include/CmResources.h

@@ -50,4 +50,6 @@ namespace CamelotEngine
 		 */
 		void save(ResourcePtr resource, const String& filePath);
 	};
+
+	CM_EXPORT Resources& gResources();
 }

+ 10 - 1
CamelotRenderer/Source/CmApplication.cpp

@@ -13,6 +13,7 @@
 #include "CmDynLib.h"
 #include "CmDynLibManager.h"
 #include "CmImporter.h"
+#include "CmResources.h"
 
 namespace CamelotEngine
 {
@@ -101,7 +102,14 @@ namespace CamelotEngine
 			loadPluginFunc();
 		}
 
-		mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
+		//mDbgTexture = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
+		TexturePtr testTex = std::static_pointer_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));
+		Resources::startUp(new Resources());
+
+		gResources().save(testTex, "C:\\ExportTest.tex");
+		mDbgTexture = std::static_pointer_cast<Texture>(gResources().load("C:\\ExportTest.tex"));
+		mDbgTexture = testTex;
+		// TODO - Compare first few bytes of TextureData before and after save
 	}
 
 	void Application::runMainLoop()
@@ -121,6 +129,7 @@ namespace CamelotEngine
 
 		HighLevelGpuProgramManager::shutDown();
 		DynLibManager::shutDown();
+		Resources::shutDown();
 	}
 
 	void Application::DBG_renderSimpleFrame()

+ 12 - 4
CamelotRenderer/Source/CmResources.cpp

@@ -18,17 +18,20 @@ namespace CamelotEngine
 	ResourcePtr Resources::load(const String& filePath)
 	{
 		FileSerializer fs;
-		std::shared_ptr<IReflectable> resource = fs.decode(filePath);
+		std::shared_ptr<IReflectable> loadedData = fs.decode(filePath);
 
 		// TODO - Low priority. Check is file path valid?
 
-		if(resource == nullptr)
+		if(loadedData == nullptr)
 			CM_EXCEPT(InternalErrorException, "Unable to load resource.");
 
-		if(!resource->isDerivedFrom(Resource::getRTTIStatic()))
+		if(!loadedData->isDerivedFrom(Resource::getRTTIStatic()))
 			CM_EXCEPT(InternalErrorException, "Loaded class doesn't derive from Resource.");
 
-		return std::static_pointer_cast<Resource>(resource);
+		ResourcePtr resource = std::static_pointer_cast<Resource>(loadedData);
+		resource->load();
+
+		return resource;
 	}
 
 	ResourcePtr Resources::load(const UUID& uuid)
@@ -45,4 +48,9 @@ namespace CamelotEngine
 		FileSerializer fs;
 		fs.encode(resource.get(), filePath);
 	}
+
+	CM_EXPORT Resources& gResources()
+	{
+		return Resources::instance();
+	}
 }

+ 2 - 2
CamelotRenderer/Source/CmTexture.cpp

@@ -141,7 +141,7 @@ namespace CamelotEngine {
 		UINT32 height = getHeight();
 		UINT32 depth = getDepth();
 
-		for(UINT32 j = 0; j < numMips; j++)
+		for(UINT32 j = 0; j <= numMips; j++)
 		{
 			UINT32 currentMipSize = PixelUtil::getMemorySize(
 					width, height, depth, mFormat);
@@ -156,7 +156,7 @@ namespace CamelotEngine {
 		UINT8* buffer = new UINT8[totalSize]; // TextureData frees this
 		TextureDataPtr texData(new TextureData(getWidth(), getHeight(), totalSize, mFormat, buffer, getDepth(), 0, getNumMipmaps()));
 
-		for(UINT32 j = 0; j < numMips; j++)
+		for(UINT32 j = 0; j <= numMips; j++)
 		{
 			PixelData pixels = texData->getPixels(j);
 			getBuffer(face, j)->blitToMemory(pixels);

+ 1 - 0
CamelotRenderer/TODO.txt

@@ -45,6 +45,7 @@ TODO:
  - OpenGL too
 
 TOMORROW:
+ - Are resource getting properly unloaded? e.g. when shared_ptr destroys a texture is it removed from gpu?
  - Depth test is disabled by default (OpenGL renderer at least)
  - Serializable callbacks can't be null otherwise compiler complains
  - Ogre performed special DDS loading. I removed that. I'm not sure if I'll need to re-add it?