#pragma once #include #include "IEngine.h" #include "IResourceFactory.h" #include "../Resources/RenderTarget/SwapChain/SwapChain.h" #include "../Resources/RenderTarget/RenderTargetTexture/RenderTargetTexture.h" #include "../Resources/GeometryBuffer/GeometryBuffer.h" #include "../Resources/Shader/Shader.h" #include "../Resources/Shader/InternalShader.h" #include "../Resources/RenderCommandNode/RenderCommandNode.h" #include "../Resources/ShaderParameterCollection/ShaderParameterCollection.h" #include "../Resources/Texture/Texture.h" #include "../Resources/Transformation/Transformation.h" #include "../Resources/TransformationAnimation/TransformationAnimation.h" #include "../Resources/Camera/OrthographicCamera.h" #include "../Resources/Camera/PerspectiveCamera.h" #include "../Resources/Bone/Bone.h" #include "../Resources/Bone/BoneController.h" #include "../Window/Window.h" class ResourceFactory : public IResourceFactory { public: ResourceFactory(IEngine& argEngine); virtual ~ResourceFactory(); virtual ITransformationAnimation& CreateTransformationAnimation(); virtual void DeleteTransformationAnimation(ITransformationAnimation& argValue, bool argRemoveOnly = false); virtual IWindow& CreateSwapChainWindow(); virtual void DeleteSwapChainWindow(IWindow& argValue, bool argRemoveOnly = false); virtual IRenderCommandNode& CreateRenderCommandNode(); virtual void DeleteRenderCommandNode(IRenderCommandNode& argValue, bool argRemoveOnly = false); virtual IShader* FindShader(const string8& argId); virtual IShader& CreateOrFindShader(const string8& argId = ""); virtual void DeleteShader(IShader& argValue, bool argRemoveOnly = false); virtual IInternalShader* FindInternalShader(const string8& argId); virtual IInternalShader& CreateOrFindInternalShader(const string8& argId = ""); virtual void DeleteInternalShader(IInternalShader& argValue, bool argRemoveOnly = false); virtual ISwapChain* FindSwapChain(const string8& argId); virtual ISwapChain& CreateOrFindSwapChain(const string8& argId = ""); virtual void DeleteSwapChain(ISwapChain& argValue, bool argRemoveOnly = false); virtual IRenderTargetTexture* FindRenderTargetTexture(const string8& argId); virtual IRenderTargetTexture& CreateOrFindRenderTargetTexture(const string8& argId = ""); virtual void DeleteRenderTargetTexture(IRenderTargetTexture& argValue, bool argRemoveOnly = false); virtual IGeometryBuffer* FindGeometryBuffer(const string8& argId); virtual IGeometryBuffer& CreateOrFindGeometryBuffer(const string8& argId = ""); virtual void DeleteGeometryBuffer(IGeometryBuffer& argValue, bool argRemoveOnly = false); virtual IShaderParameterCollection* FindShaderParameterCollection(const string8& argId); virtual IShaderParameterCollection& CreateOrFindShaderParameterCollection(const string8& argId = ""); virtual void DeleteShaderParameterCollection(IShaderParameterCollection& argValue, bool argRemoveOnly = false); virtual ITexture* FindTexture(const string8& argId); virtual ITexture& CreateOrFindTexture(const string8& argId = ""); virtual void DeleteTexture(ITexture& argValue, bool argRemoveOnly = false); virtual ITransformation* FindTransformation(const string8& argId); virtual ITransformation& CreateOrFindTransformation(const string8& argId = ""); virtual void DeleteTransformation(ITransformation& argValue, bool argRemoveOnly = false); virtual IPerspectiveCamera* FindPerspectiveCamera(const string8& argId); virtual IPerspectiveCamera& CreateOrFindPerspectiveCamera(const string8& argId = ""); virtual void DeletePerspectiveCamera(IPerspectiveCamera& argValue, bool argRemoveOnly = false); virtual IOrthographicCamera* FindOrthographicCamera(const string8& argId); virtual IOrthographicCamera& CreateOrFindOrthographicCamera(const string8& argId = ""); virtual void DeleteOrthographicCamera(IOrthographicCamera& argValue, bool argRemoveOnly = false); virtual IBone* FindBone(const string8& argId); virtual IBone& CreateOrFindBone(const string8& argId = ""); virtual void DeleteBone(IBone& argValue, bool argRemoveOnly = false); virtual IBoneController* FindBoneController(const string8& argId); virtual IBoneController& CreateOrFindBoneController(const string8& argId = ""); virtual void DeleteBoneController(IBoneController& argValue, bool argRemoveOnly = false); virtual bool HasCreatedResource() { return m_LastCallCreatedResource; } protected: template void DeleteInternal(std::map& argPool, B& argWhich, bool argRemoveOnly) { A* valueA = dynamic_cast(&argWhich); for (std::map::iterator iter = argPool.begin(); iter != argPool.end(); ++iter) if (iter->second == valueA) { ICommandUser* commandUser = dynamic_cast(iter->second); if (commandUser != NULL) std::cout << "Deleting \"" << std::green << iter->first << std::white << "\" [" << commandUser->get_UserName() << "]" << std::endl; argPool.erase(iter); if (!argRemoveOnly) delete valueA; return; } } template void ClearInternal(std::map& argPool) { while (!argPool.empty()) { ICommandUser* commandUser = dynamic_cast(argPool.begin()->second); if (commandUser != NULL) std::cout << "Deleting \"" << std::green << argPool.begin()->first << std::white << "\" [" << commandUser->get_UserName() << "]" << std::endl; T* valueA = argPool.begin()->second; argPool.erase(argPool.begin()); delete valueA; } } template void ClearInternal(std::vector& argPool) { while (!argPool.empty()) { delete *argPool.begin(); argPool.erase(argPool.begin()); } } template T* FindInternal(std::map& argPool, const string8& argId) { m_LastCallCreatedResource = false; if (argPool.find(argId) == argPool.end()) return NULL; return argPool[argId]; } template string8 CreateUniqueName(std::map& argPool, const string8& argPrefix = "") { int NameIndex = argPool.size(); std::stringstream CurrentName; do { CurrentName.flush(); CurrentName << argPrefix << NameIndex++; } while (FindInternal(argPool, CurrentName.str()) != NULL); return CurrentName.str(); } template T* CreateOrFindInternal(std::map& argPool, const string8& argId) { std::string currentId = argId; if (currentId.empty()) currentId = CreateUniqueName(argPool); T* instance = this->FindInternal(argPool, currentId); if (instance != NULL) return instance; m_LastCallCreatedResource = true; instance = new T(m_Engine); ICommandUser* commandUser = dynamic_cast(instance); if (commandUser != NULL) std::cout << "Creating \"" << std::green << currentId << std::white << "\" [" << commandUser->get_UserName() << "]" << std::endl; argPool[currentId] = instance; return instance; } private: IEngine& m_Engine; std::map m_InternalShaderPool; std::map m_ShaderPool; std::map m_GeometryBufferPool; std::map m_SwapChainPool; std::map m_RenderTargetTexturePool; std::map m_ShaderParameterCollectionPool; std::map m_TexturePool; std::map m_TransformationPool; std::map m_OrthographicCameraPool; std::map m_PerspectiveCameraPool; std::map m_BonePool; std::map m_BoneControllerPool; std::vector m_RenderCommandNodes; std::vector m_Windows; std::vector m_TransformationAnimations; bool m_LastCallCreatedResource; };