Bläddra i källkod

Add handles for components

Marko Pintera 12 år sedan
förälder
incheckning
b23df56ce1

+ 4 - 8
CamelotClient/CamelotClient.cpp

@@ -53,7 +53,7 @@ int CALLBACK WinMain(
 	RenderWindowPtr renderWindow = gApplication().getPrimaryRenderWindow();
 	RenderWindowPtr renderWindow = gApplication().getPrimaryRenderWindow();
 
 
 	HGameObject cameraGO = GameObject::create("MainCamera");
 	HGameObject cameraGO = GameObject::create("MainCamera");
-	CameraPtr camera = cameraGO->addComponent<Camera>();
+	HCamera camera = cameraGO->addComponent<Camera>();
 
 
 	camera->init(renderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
 	camera->init(renderWindow, 0.0f, 0.0f, 1.0f, 1.0f, 0);
 	cameraGO->setPosition(Vector3(0,50,1240));
 	cameraGO->setPosition(Vector3(0,50,1240));
@@ -61,13 +61,13 @@ int CALLBACK WinMain(
 	camera->setNearClipDistance(5);
 	camera->setNearClipDistance(5);
 	camera->setAspectRatio(800.0f / 600.0f);
 	camera->setAspectRatio(800.0f / 600.0f);
 
 
-	std::shared_ptr<DebugCamera> debugCamera = cameraGO->addComponent<DebugCamera>();
+	GameObjectHandle<DebugCamera> debugCamera = cameraGO->addComponent<DebugCamera>();
 
 
 	HGameObject testModelGO = GameObject::create("TestMesh");
 	HGameObject testModelGO = GameObject::create("TestMesh");
-	RenderablePtr testRenderable = testModelGO->addComponent<Renderable>();
+	HRenderable testRenderable = testModelGO->addComponent<Renderable>();
 
 
 	HGameObject testTextGO = GameObject::create("TestText");
 	HGameObject testTextGO = GameObject::create("TestText");
-	std::shared_ptr<TestTextSprite> textSprite = testTextGO->addComponent<TestTextSprite>();
+	GameObjectHandle<TestTextSprite> textSprite = testTextGO->addComponent<TestTextSprite>();
 
 
 	HFont font;
 	HFont font;
 	
 	
@@ -298,11 +298,7 @@ int CALLBACK WinMain(
 	vertProgRef.reset();
 	vertProgRef.reset();
 
 
 	testModelGO->destroy();
 	testModelGO->destroy();
-	testRenderable = nullptr;
-
 	cameraGO->destroy();
 	cameraGO->destroy();
-	camera = nullptr;
-	debugCamera = nullptr;
 
 
 	newPassGL = nullptr;
 	newPassGL = nullptr;
 	newTechniqueGL = nullptr;
 	newTechniqueGL = nullptr;

+ 2 - 2
CamelotClient/CmDebugCamera.h

@@ -22,7 +22,7 @@ namespace CamelotEngine
 		Degree mPitch;
 		Degree mPitch;
 		Degree mYaw;
 		Degree mYaw;
 
 
-		CameraPtr mCamera;
+		HCamera mCamera;
 
 
 		void keyDown(CamelotEngine::KeyCode keyCode);
 		void keyDown(CamelotEngine::KeyCode keyCode);
 		void keyUp(CamelotEngine::KeyCode keyCode);
 		void keyUp(CamelotEngine::KeyCode keyCode);
@@ -39,7 +39,7 @@ namespace CamelotEngine
 		/************************************************************************/
 		/************************************************************************/
 		/* 						COMPONENT OVERRIDES                      		*/
 		/* 						COMPONENT OVERRIDES                      		*/
 		/************************************************************************/
 		/************************************************************************/
-	private:
+	protected:
 		friend class GameObject;
 		friend class GameObject;
 
 
 		/** Standard constructor.
 		/** Standard constructor.

+ 2 - 2
CamelotClient/CmTestTextSprite.cpp

@@ -23,11 +23,11 @@ namespace CamelotEngine
 			CM_DELETE(mSkin, GUISkin, GUIAlloc);
 			CM_DELETE(mSkin, GUISkin, GUIAlloc);
 	}
 	}
 
 
-	void TestTextSprite::setText(const CameraPtr& camera, const String& text, HFont font, UINT32 fontSize)
+	void TestTextSprite::setText(const HCamera& camera, const String& text, HFont font, UINT32 fontSize)
 	{
 	{
 		mSkin = CM_NEW(GUISkin, GUIAlloc) GUISkin();
 		mSkin = CM_NEW(GUISkin, GUIAlloc) GUISkin();
 
 
-		OverlayManager::instance().attachOverlay(camera, this);		
+		OverlayManager::instance().attachOverlay(camera.getInternalPtr(), this);		
 
 
 		GUIElementStyle labelStyle;
 		GUIElementStyle labelStyle;
 		labelStyle.font = font;
 		labelStyle.font = font;

+ 2 - 2
CamelotClient/CmTestTextSprite.h

@@ -5,7 +5,7 @@ namespace CamelotEngine
 {
 {
 	class TestTextSprite : public GUIWidget
 	class TestTextSprite : public GUIWidget
 	{
 	{
-	private:
+	protected:
 		friend class GameObject;
 		friend class GameObject;
 
 
 		TestTextSprite(const HGameObject& parent);
 		TestTextSprite(const HGameObject& parent);
@@ -16,6 +16,6 @@ namespace CamelotEngine
 
 
 		virtual void update();
 		virtual void update();
 
 
-		void setText(const CameraPtr& camera, const String& text, HFont font, UINT32 fontSize);
+		void setText(const HCamera& camera, const String& text, HFont font, UINT32 fontSize);
 	};
 	};
 }
 }

+ 1 - 1
CamelotCore/Include/CmCamera.h

@@ -522,7 +522,7 @@ namespace CamelotEngine {
 		/************************************************************************/
 		/************************************************************************/
 		/* 						COMPONENT OVERRIDES                      		*/
 		/* 						COMPONENT OVERRIDES                      		*/
 		/************************************************************************/
 		/************************************************************************/
-	private:
+	protected:
 		friend class GameObject;
 		friend class GameObject;
 
 
 		/** Standard constructor.
 		/** Standard constructor.

+ 8 - 8
CamelotCore/Include/CmGameObject.h

@@ -210,12 +210,12 @@ namespace CamelotEngine
 		/************************************************************************/
 		/************************************************************************/
 	public:
 	public:
 		template <typename T>
 		template <typename T>
-		std::shared_ptr<T> addComponent()
+		GameObjectHandle<T> addComponent()
 		{
 		{
 			BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotEngine::Component, T>::value), 
 			BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotEngine::Component, T>::value), 
 				"Specified type is not a valid Component.");
 				"Specified type is not a valid Component.");
 
 
-			std::shared_ptr<T> newComponent(new T(mThisHandle));
+			GameObjectHandle<T> newComponent = GameObjectHandle<T>::_create(new T(mThisHandle));
 			mComponents.push_back(newComponent);
 			mComponents.push_back(newComponent);
 
 
 			gSceneManager().notifyComponentAdded(newComponent);
 			gSceneManager().notifyComponentAdded(newComponent);
@@ -235,12 +235,12 @@ namespace CamelotEngine
 		 * @return	Component if found, nullptr otherwise.
 		 * @return	Component if found, nullptr otherwise.
 		 */
 		 */
 		template <typename T>
 		template <typename T>
-		std::shared_ptr<T> getComponent()
+		GameObjectHandle<T> getComponent()
 		{
 		{
 			BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotEngine::Component, T>::value), 
 			BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotEngine::Component, T>::value), 
 				"Specified type is not a valid Component.");
 				"Specified type is not a valid Component.");
 
 
-			return std::static_pointer_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
+			return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
 		}
 		}
 
 
 		/**
 		/**
@@ -254,21 +254,21 @@ namespace CamelotEngine
 		 *
 		 *
 		 * @return	Component if found, nullptr otherwise.
 		 * @return	Component if found, nullptr otherwise.
 		 */
 		 */
-		ComponentPtr getComponent(UINT32 typeId) const;
+		HComponent getComponent(UINT32 typeId) const;
 
 
 		/**
 		/**
 		 * @brief	Removes the component from this GameObject, and deallocates it.
 		 * @brief	Removes the component from this GameObject, and deallocates it.
 		 *
 		 *
 		 * @param [in]	component	The component to destroy.
 		 * @param [in]	component	The component to destroy.
 		 */
 		 */
-		void destroyComponent(ComponentPtr component);
+		void destroyComponent(const HComponent& component);
 
 
 		/**
 		/**
 		 * @brief	Returns all components on this GameObject.
 		 * @brief	Returns all components on this GameObject.
 		 */
 		 */
-		vector<ComponentPtr>::type& getComponents() { return mComponents; }
+		vector<HComponent>::type& getComponents() { return mComponents; }
 
 
 	private:
 	private:
-		vector<ComponentPtr>::type mComponents;
+		vector<HComponent>::type mComponents;
 	};
 	};
 }
 }

+ 15 - 3
CamelotCore/Include/CmGameObjectHandle.h

@@ -33,11 +33,15 @@ namespace CamelotEngine
 		 */
 		 */
 		bool isDestroyed() const { return mData->mPtr == nullptr; }
 		bool isDestroyed() const { return mData->mPtr == nullptr; }
 
 
+		/**
+		 * @brief	Internal method only. Not meant to be called directly.
+		 */
+		std::shared_ptr<GameObjectHandleData> getHandleData() const { return mData; }
 	protected:
 	protected:
 		GameObjectHandleBase();
 		GameObjectHandleBase();
 
 
 		inline void throwIfDestroyed() const;
 		inline void throwIfDestroyed() const;
-		std::shared_ptr<GameObjectHandleData> getHandleData() const { return mData; }
+		
 
 
 		std::shared_ptr<GameObjectHandleData> mData;
 		std::shared_ptr<GameObjectHandleData> mData;
 	};
 	};
@@ -54,11 +58,19 @@ namespace CamelotEngine
 
 
 		template <typename T1>
 		template <typename T1>
 		GameObjectHandle(const GameObjectHandle<T1>& ptr)
 		GameObjectHandle(const GameObjectHandle<T1>& ptr)
-			:GameObjectHandleBase(ptr.getHandleData())
+			:GameObjectHandleBase()
 		{ 	
 		{ 	
 			mData = ptr.getHandleData();
 			mData = ptr.getHandleData();
 		}
 		}
 
 
+		/**
+		 * @brief	Creates a new handle. Internal use only. Don't call this manually.
+		 */
+		static GameObjectHandle<T> _create(T* ptr)
+		{
+			return GameObjectHandle<T>(ptr);
+		}
+
 		T* get() const 
 		T* get() const 
 		{ 
 		{ 
 			throwIfDestroyed();
 			throwIfDestroyed();
@@ -104,7 +116,7 @@ namespace CamelotEngine
 	};
 	};
 
 
 	template<class _Ty1, class _Ty2>
 	template<class _Ty1, class _Ty2>
-		GameObjectHandle<_Ty1> static_resource_cast(const GameObjectHandle<_Ty2>& other)
+		GameObjectHandle<_Ty1> static_object_cast(const GameObjectHandle<_Ty2>& other)
 	{	
 	{	
 		return GameObjectHandle<_Ty1>(other);
 		return GameObjectHandle<_Ty1>(other);
 	}
 	}

+ 2 - 0
CamelotCore/Include/CmPrerequisites.h

@@ -344,6 +344,8 @@ namespace CamelotEngine
 	// Game object handles
 	// Game object handles
 	typedef GameObjectHandle<GameObject> HGameObject;
 	typedef GameObjectHandle<GameObject> HGameObject;
 	typedef GameObjectHandle<Component> HComponent;
 	typedef GameObjectHandle<Component> HComponent;
+	typedef GameObjectHandle<Camera> HCamera;
+	typedef GameObjectHandle<Renderable> HRenderable;
 }
 }
 
 
 #endif
 #endif

+ 1 - 1
CamelotCore/Include/CmRenderable.h

@@ -21,7 +21,7 @@ namespace CamelotEngine
 		/* 							COMPONENT OVERRIDES                    		*/
 		/* 							COMPONENT OVERRIDES                    		*/
 		/************************************************************************/
 		/************************************************************************/
 
 
-	private:
+	protected:
 		friend class GameObject;
 		friend class GameObject;
 
 
 		/** Standard constructor.
 		/** Standard constructor.

+ 1 - 1
CamelotCore/Include/CmRenderer.h

@@ -17,6 +17,6 @@ namespace CamelotEngine
 		/**
 		/**
 		 * @brief	 Renders the scene from the perspective of a single camera
 		 * @brief	 Renders the scene from the perspective of a single camera
 		 */
 		 */
-		virtual void render(const CameraPtr camera) = 0;
+		virtual void render(const HCamera& camera) = 0;
 	};
 	};
 }
 }

+ 5 - 5
CamelotCore/Include/CmSceneManager.h

@@ -24,9 +24,9 @@ namespace CamelotEngine
 		/**
 		/**
 		 * @brief	Returns all cameras in the scene.
 		 * @brief	Returns all cameras in the scene.
 		 */
 		 */
-		const vector<CameraPtr>::type& getAllCameras() const { return mCachedCameras; }
+		const vector<HCamera>::type& getAllCameras() const { return mCachedCameras; }
 
 
-		vector<RenderablePtr>::type getVisibleRenderables(const CameraPtr camera) const;
+		vector<HRenderable>::type getVisibleRenderables(const HCamera& camera) const;
 
 
 	private:
 	private:
 		friend class GameObject;
 		friend class GameObject;
@@ -42,10 +42,10 @@ namespace CamelotEngine
 		 */
 		 */
 		void registerNewGO(const HGameObject& node);
 		void registerNewGO(const HGameObject& node);
 
 
-		void notifyComponentAdded(ComponentPtr component);
-		void notifyComponentRemoved(ComponentPtr component);
+		void notifyComponentAdded(const HComponent& component);
+		void notifyComponentRemoved(const HComponent& component);
 
 
-		vector<CameraPtr>::type mCachedCameras;
+		vector<HCamera>::type mCachedCameras;
 	};
 	};
 
 
 	CM_EXPORT SceneManager& gSceneManager();
 	CM_EXPORT SceneManager& gSceneManager();

+ 4 - 4
CamelotCore/Source/CmGameObject.cpp

@@ -34,7 +34,7 @@ namespace CamelotEngine
 
 
 	HGameObject GameObject::createInternal(const String& name)
 	HGameObject GameObject::createInternal(const String& name)
 	{
 	{
-		HGameObject gameObject = HGameObject(new GameObject(name));
+		HGameObject gameObject = HGameObject::_create(new GameObject(name));
 		gameObject->mThisHandle = gameObject;
 		gameObject->mThisHandle = gameObject;
 
 
 		return gameObject;
 		return gameObject;
@@ -339,7 +339,7 @@ namespace CamelotEngine
 		}
 		}
 	}
 	}
 
 
-	ComponentPtr GameObject::getComponent(UINT32 typeId) const
+	HComponent GameObject::getComponent(UINT32 typeId) const
 	{
 	{
 		for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
 		for(auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
 		{
 		{
@@ -347,10 +347,10 @@ namespace CamelotEngine
 				return *iter;
 				return *iter;
 		}
 		}
 
 
-		return nullptr;
+		return HComponent();
 	}
 	}
 
 
-	void GameObject::destroyComponent(ComponentPtr component)
+	void GameObject::destroyComponent(const HComponent& component)
 	{
 	{
 		if(component == nullptr)
 		if(component == nullptr)
 		{
 		{

+ 8 - 8
CamelotCore/Source/CmSceneManager.cpp

@@ -27,7 +27,7 @@ namespace CamelotEngine
 			HGameObject currentGO = todo.top();
 			HGameObject currentGO = todo.top();
 			todo.pop();
 			todo.pop();
 			                  
 			                  
-			vector<ComponentPtr>::type components = currentGO->getComponents();
+			vector<HComponent>::type components = currentGO->getComponents();
 
 
 			for(auto iter = components.begin(); iter != components.end(); ++iter)
 			for(auto iter = components.begin(); iter != components.end(); ++iter)
 			{
 			{
@@ -39,11 +39,11 @@ namespace CamelotEngine
 		}
 		}
 	}
 	}
 
 
-	vector<RenderablePtr>::type SceneManager::getVisibleRenderables(const CameraPtr camera) const
+	vector<HRenderable>::type SceneManager::getVisibleRenderables(const HCamera& camera) const
 	{
 	{
 		// TODO - Cull invisible objects
 		// TODO - Cull invisible objects
 
 
-		vector<RenderablePtr>::type renderables;
+		vector<HRenderable>::type renderables;
 
 
 		stack<HGameObject>::type todo;
 		stack<HGameObject>::type todo;
 		todo.push(mRootNode);
 		todo.push(mRootNode);
@@ -53,7 +53,7 @@ namespace CamelotEngine
 			HGameObject currentGO = todo.top();
 			HGameObject currentGO = todo.top();
 			todo.pop();
 			todo.pop();
 
 
-			RenderablePtr curRenderable = currentGO->getComponent<Renderable>();
+			HRenderable curRenderable = currentGO->getComponent<Renderable>();
 			if(curRenderable != nullptr)
 			if(curRenderable != nullptr)
 				renderables.push_back(curRenderable);
 				renderables.push_back(curRenderable);
 
 
@@ -70,11 +70,11 @@ namespace CamelotEngine
 			node->setParent(mRootNode);
 			node->setParent(mRootNode);
 	}
 	}
 
 
-	void SceneManager::notifyComponentAdded(ComponentPtr component)
+	void SceneManager::notifyComponentAdded(const HComponent& component)
 	{
 	{
 		if(component->getTypeId() == TID_Camera)
 		if(component->getTypeId() == TID_Camera)
 		{
 		{
-			CameraPtr camera = std::static_pointer_cast<Camera>(component);
+			HCamera camera = static_object_cast<Camera>(component);
 			auto findIter = std::find(mCachedCameras.begin(), mCachedCameras.end(), camera);
 			auto findIter = std::find(mCachedCameras.begin(), mCachedCameras.end(), camera);
 
 
 			if(findIter != mCachedCameras.end())
 			if(findIter != mCachedCameras.end())
@@ -86,11 +86,11 @@ namespace CamelotEngine
 		}
 		}
 	}
 	}
 
 
-	void SceneManager::notifyComponentRemoved(ComponentPtr component)
+	void SceneManager::notifyComponentRemoved(const HComponent& component)
 	{
 	{
 		if(component->getTypeId() == TID_Camera)
 		if(component->getTypeId() == TID_Camera)
 		{
 		{
-			CameraPtr camera = std::static_pointer_cast<Camera>(component);
+			HCamera camera = static_object_cast<Camera>(component);
 			auto findIter = std::find(mCachedCameras.begin(), mCachedCameras.end(), camera);
 			auto findIter = std::find(mCachedCameras.begin(), mCachedCameras.end(), camera);
 
 
 			if(findIter == mCachedCameras.end())
 			if(findIter == mCachedCameras.end())

+ 1 - 1
CamelotForwardRenderer/Include/CmForwardRenderer.h

@@ -14,7 +14,7 @@ namespace CamelotEngine
 		virtual const String& getName() const;
 		virtual const String& getName() const;
 
 
 		virtual void renderAll();
 		virtual void renderAll();
-		virtual void render(const CameraPtr camera);
+		virtual void render(const HCamera& camera);
 
 
 	protected:
 	protected:
 		RenderCommandBuffer* mCommandBuffer;
 		RenderCommandBuffer* mCommandBuffer;

+ 4 - 4
CamelotForwardRenderer/Source/CmForwardRenderer.cpp

@@ -33,7 +33,7 @@ namespace CamelotEngine
 	{
 	{
 		DeferredRenderContextPtr renderContext = gApplication().getPrimaryRenderContext();
 		DeferredRenderContextPtr renderContext = gApplication().getPrimaryRenderContext();
 
 
-		const vector<CameraPtr>::type& allCameras = gSceneManager().getAllCameras();
+		const vector<HCamera>::type& allCameras = gSceneManager().getAllCameras();
 		for(auto iter = allCameras.begin(); iter != allCameras.end(); ++iter)
 		for(auto iter = allCameras.begin(); iter != allCameras.end(); ++iter)
 		{
 		{
 			render(*iter);
 			render(*iter);
@@ -50,9 +50,9 @@ namespace CamelotEngine
 		}
 		}
 	}
 	}
 
 
-	void ForwardRenderer::render(const CameraPtr camera) 
+	void ForwardRenderer::render(const HCamera& camera) 
 	{
 	{
-		vector<RenderablePtr>::type allRenderables = gSceneManager().getVisibleRenderables(camera);
+		vector<HRenderable>::type allRenderables = gSceneManager().getVisibleRenderables(camera);
 
 
 		DeferredRenderContextPtr renderContext = gApplication().getPrimaryRenderContext();
 		DeferredRenderContextPtr renderContext = gApplication().getPrimaryRenderContext();
 		renderContext->setViewport(camera->getViewport());
 		renderContext->setViewport(camera->getViewport());
@@ -96,7 +96,7 @@ namespace CamelotEngine
 		}
 		}
 
 
 		// Render overlays for this camera
 		// Render overlays for this camera
-		OverlayManager::instance().render(camera, renderContext);
+		OverlayManager::instance().render(camera.getInternalPtr(), renderContext);
 
 
 		renderContext->endFrame();
 		renderContext->endFrame();