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

Render system factory and singleton

Marko Pintera 13 лет назад
Родитель
Сommit
511b03d806

+ 9 - 0
CamelotRenderer/CamelotRenderer.cpp

@@ -3,6 +3,8 @@
 
 #include "stdafx.h"
 
+#include <string>
+
 #include "OgreBuildSettings.h"
 #include "OgreColourValue.h"
 #include "OgreConfig.h"
@@ -25,9 +27,16 @@
 
 #include "OgreD3D9Prerequisites.h"
 #include "OgreD3D9VideoMode.h"
+#include "OgreRenderSystem.h"
+
+#include "CmApplication.h"
+
+using namespace CamelotEngine;
 
 int _tmain(int argc, _TCHAR* argv[])
 {
+	const Ogre::String& name = CamelotEngine::gApplication().getRenderSystem()->getName();
+
 	return 0;
 }
 

+ 6 - 0
CamelotRenderer/CamelotRenderer.vcxproj

@@ -88,6 +88,9 @@
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="asm_math.h" />
+    <ClInclude Include="CmApplication.h" />
+    <ClInclude Include="CmD3D9RenderSystemFactory.h" />
+    <ClInclude Include="CmRenderSystemFactory.h" />
     <ClInclude Include="OgreAlignedAllocator.h" />
     <ClInclude Include="OgreAxisAlignedBox.h" />
     <ClInclude Include="OgreBitwise.h" />
@@ -175,6 +178,9 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="CamelotRenderer.cpp" />
+    <ClCompile Include="CmApplication.cpp" />
+    <ClCompile Include="CmD3D9RenderSystemFactory.cpp" />
+    <ClCompile Include="CmRenderSystemFactory.cpp" />
     <ClCompile Include="OgreAlignedAllocator.cpp" />
     <ClCompile Include="OgreAxisAlignedBox.cpp" />
     <ClCompile Include="OgreColourValue.cpp" />

+ 18 - 0
CamelotRenderer/CamelotRenderer.vcxproj.filters

@@ -298,6 +298,15 @@
     <ClInclude Include="OgreWindowEventUtilities.h">
       <Filter>Header Files\Utility</Filter>
     </ClInclude>
+    <ClInclude Include="CmRenderSystemFactory.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="CmD3D9RenderSystemFactory.h">
+      <Filter>Header Files\D3D9RenderSystem</Filter>
+    </ClInclude>
+    <ClInclude Include="CmApplication.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="stdafx.cpp">
@@ -486,5 +495,14 @@
     <ClCompile Include="OgreWindowEventUtilities.cpp">
       <Filter>Source Files\Utility</Filter>
     </ClCompile>
+    <ClCompile Include="CmD3D9RenderSystemFactory.cpp">
+      <Filter>Source Files\D3D9RenderSystem</Filter>
+    </ClCompile>
+    <ClCompile Include="CmApplication.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="CmRenderSystemFactory.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 10 - 0
CamelotRenderer/CmApplication.cpp

@@ -0,0 +1,10 @@
+#include "CmApplication.h"
+
+namespace CamelotEngine
+{
+	Application& gApplication()
+	{
+		static Application application;
+		return application;
+	}
+}

+ 30 - 0
CamelotRenderer/CmApplication.h

@@ -0,0 +1,30 @@
+#pragma once
+
+#include <memory>
+
+#include "CmRenderSystemFactory.h"
+
+namespace Ogre
+{
+	class RenderSystem;
+	typedef std::shared_ptr<Ogre::RenderSystem> RenderSystemPtr;
+}
+
+namespace CamelotEngine
+{
+	class Application
+	{
+		public:
+			Application()
+			{
+				mActiveRenderSystem = RenderSystemManager::create("D3D9RenderSystem");
+			}
+
+			RenderSystemPtr getRenderSystem() { return mActiveRenderSystem; }
+
+		private:
+			RenderSystemPtr mActiveRenderSystem;
+	};
+
+	Application& gApplication();
+}

+ 17 - 0
CamelotRenderer/CmD3D9RenderSystemFactory.cpp

@@ -0,0 +1,17 @@
+#include "CmD3D9RenderSystemFactory.h"
+
+namespace CamelotEngine
+{
+	RenderSystemPtr D3D9RenderSystemFactory::create()
+	{
+	#ifdef OGRE_STATIC_LIB
+		HINSTANCE hInst = GetModuleHandle( NULL );
+	#else
+		HINSTANCE hInst = GetModuleHandle( "CamelotEngine.dll" ); // TODO - Change name if I plan on using external dll
+	#endif
+
+		return RenderSystemPtr(new Ogre::D3D9RenderSystem(hInst));
+	}
+
+	D3D9RenderSystemFactory::InitOnStart D3D9RenderSystemFactory::initOnStart;
+}

+ 34 - 0
CamelotRenderer/CmD3D9RenderSystemFactory.h

@@ -0,0 +1,34 @@
+#pragma once
+
+#include <string>
+#include "CmRenderSystemFactory.h"
+#include "OgreD3D9RenderSystem.h"
+
+namespace CamelotEngine
+{
+	const std::string SystemName = "D3D9RenderSystem";
+
+	class D3D9RenderSystemFactory : public RenderSystemFactory
+	{
+		public:
+			virtual RenderSystemPtr create();
+			virtual const std::string& name() const { return SystemName; }
+
+		private:
+			class InitOnStart
+			{
+				public:
+					InitOnStart() 
+					{ 
+						static RenderSystemFactoryPtr newFactory;
+						if(newFactory == nullptr)
+						{
+							newFactory = RenderSystemFactoryPtr(new D3D9RenderSystemFactory());
+							RenderSystemManager::registerRenderSystemFactory(newFactory);
+						}
+					}
+			};
+
+			static InitOnStart initOnStart; // Makes sure factory is registered on program start
+	};
+}

+ 28 - 0
CamelotRenderer/CmRenderSystemFactory.cpp

@@ -0,0 +1,28 @@
+#include "CmRenderSystemFactory.h"
+
+namespace CamelotEngine
+{
+	RenderSystemPtr RenderSystemManager::create(const std::string& name)
+	{
+		for(auto iter = getAvailableFactories().begin(); iter != getAvailableFactories().end(); ++iter)
+		{
+			if((*iter)->name() == name)
+				return (*iter)->create();
+		}
+
+		return nullptr;
+	}
+
+	void RenderSystemManager::registerRenderSystemFactory(RenderSystemFactoryPtr factory)
+	{
+		assert(factory != nullptr);
+
+		getAvailableFactories().push_back(factory);
+	}
+
+	std::vector<RenderSystemFactoryPtr>& RenderSystemManager::getAvailableFactories()
+	{
+		static std::vector<RenderSystemFactoryPtr> availableFactories;
+		return availableFactories;
+	}
+}

+ 35 - 0
CamelotRenderer/CmRenderSystemFactory.h

@@ -0,0 +1,35 @@
+#pragma once
+
+#include <string>
+#include <vector>
+#include <memory>
+#include <assert.h>
+
+namespace Ogre
+{
+	class RenderSystem;
+}
+
+namespace CamelotEngine
+{
+	typedef std::shared_ptr<Ogre::RenderSystem> RenderSystemPtr;
+
+	class RenderSystemFactory
+	{
+	public:
+		virtual RenderSystemPtr create() = 0;
+		virtual const std::string& name() const = 0;
+	};
+
+	typedef std::shared_ptr<RenderSystemFactory> RenderSystemFactoryPtr;
+
+	class RenderSystemManager
+	{
+	public:
+		static RenderSystemPtr create(const std::string& name);
+		static void registerRenderSystemFactory(RenderSystemFactoryPtr factory);
+
+	private:
+		static std::vector<RenderSystemFactoryPtr>& getAvailableFactories();
+	};
+}

+ 2 - 1
CamelotRenderer/TODO.txt

@@ -83,4 +83,5 @@ Other notes:
    - As well as TextureManager
  - Ogre::ColourValue -> CamelotEngine::Color (Other struct names are okay for the most part)
  - Port all math methods to Camelot
- - Rename all macros and other OGRE references to CM
+ - Rename all macros and other OGRE references to CM
+ - How am I notified on device reset? (When I need to reload my resources)