Browse Source

Removing exceptions

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
9acb781007
5 changed files with 123 additions and 67 deletions
  1. 16 18
      include/anki/scene/Camera.h
  2. 15 10
      include/anki/scene/Light.h
  3. 1 1
      include/anki/scene/SceneNode.h
  4. 23 12
      src/scene/Camera.cpp
  5. 68 26
      src/scene/Light.cpp

+ 16 - 18
include/anki/scene/Camera.h

@@ -29,12 +29,12 @@ public:
 		COUNT
 	};
 
-	Camera(
-		const CString& name, SceneGraph* scene, // SceneNode
-		Type type, Frustum* frustum); // Self
+	Camera(SceneGraph* scene, Type type, Frustum* frustum);
 
 	virtual ~Camera();
 
+	ANKI_USE_RESULT Error create(const CString& name);
+
 	Type getCameraType() const
 	{
 		return m_type;
@@ -54,7 +54,7 @@ public:
 
 	/// @name MoveComponent virtuals
 	/// @{
-	void onMoveComponentUpdate(
+	ANKI_USE_RESULT Error onMoveComponentUpdate(
 		SceneNode& node, F32 prevTime, F32 crntTime) override;
 	/// @}
 
@@ -80,13 +80,13 @@ private:
 class PerspectiveCamera: public Camera
 {
 public:
-	/// @name Constructors
-	/// @{
-	PerspectiveCamera(const CString& name, SceneGraph* scene);
-	/// @}
+	PerspectiveCamera(SceneGraph* scene);
+
+	ANKI_USE_RESULT Error create(const CString& name)
+	{
+		return Camera::create(name);
+	}
 
-	/// @name Accessors
-	/// @{
 	F32 getFovX() const
 	{
 		return m_frustum.getFovX();
@@ -112,7 +112,6 @@ public:
 		m_frustum.setAll(fovX_, fovY_, near_, far_);
 		frustumUpdate();
 	}
-	/// @}
 
 	/// @name SpatialComponent virtuals
 	/// @{
@@ -130,13 +129,13 @@ private:
 class OrthographicCamera: public Camera
 {
 public:
-	/// @name Constructors
-	/// @{
-	OrthographicCamera(const CString& name, SceneGraph* scene);
-	/// @}
+	OrthographicCamera(SceneGraph* scene);
+
+	ANKI_USE_RESULT Error create(const CString& name)
+	{
+		return Camera::create(name);
+	}
 
-	/// @name Accessors
-	/// @{
 	F32 getNear() const
 	{
 		return m_frustum.getNear();
@@ -172,7 +171,6 @@ public:
 		m_frustum.setAll(left, right, near, far, top, bottom);
 		frustumUpdate();
 	}
-	/// @}
 
 	/// @name SpatialComponent virtuals
 	/// @{

+ 15 - 10
include/anki/scene/Light.h

@@ -82,12 +82,12 @@ public:
 		COUNT
 	};
 
-	Light(
-		const CString& name, SceneGraph* scene, // SceneNode
-		Type t); // Self
+	Light(SceneGraph* scene, Type t);
 
 	virtual ~Light();
 
+	ANKI_USE_RESULT Error create(const CString& name);
+
 	Type getLightType() const
 	{
 		return m_type;
@@ -191,7 +191,7 @@ public:
 		m_flaresAlpha = val;
 	}
 
-	void loadLensFlare(const CString& filename);
+	ANKI_USE_RESULT Error loadLensFlare(const CString& filename);
 
 	/// @name SpatialComponent virtuals
 	/// @{
@@ -229,7 +229,9 @@ private:
 class PointLight: public Light
 {
 public:
-	PointLight(const CString& name, SceneGraph* scene);
+	PointLight(SceneGraph* scene);
+
+	ANKI_USE_RESULT Error create(const CString& name);
 
 	F32 getRadius() const
 	{
@@ -249,12 +251,13 @@ public:
 
 	/// @name SceneNode virtuals
 	/// @{
-	void frameUpdate(F32 prevUpdateTime, F32 crntTime) override;
+	ANKI_USE_RESULT Error frameUpdate(
+		F32 prevUpdateTime, F32 crntTime) override;
 	/// @}
 
 	/// @name MoveComponent virtuals
 	/// @{
-	void onMoveComponentUpdate(SceneNode&, F32, F32) override;
+	ANKI_USE_RESULT Error onMoveComponentUpdate(SceneNode&, F32, F32) override;
 	/// @}
 
 	/// @name SpatialComponent virtuals
@@ -289,7 +292,9 @@ public:
 class SpotLight: public Light, public FrustumComponent
 {
 public:
-	SpotLight(const CString& name, SceneGraph* scene);
+	SpotLight(SceneGraph* scene);
+
+	ANKI_USE_RESULT Error create(const CString& name);
 
 	GlTextureHandle& getTexture()
 	{
@@ -347,7 +352,7 @@ public:
 
 	/// @name MoveComponent virtuals
 	/// @{
-	void onMoveComponentUpdate(SceneNode&, F32, F32) override;
+	ANKI_USE_RESULT Error onMoveComponentUpdate(SceneNode&, F32, F32) override;
 	/// @}
 
 	/// @name SpatialComponent virtuals
@@ -358,7 +363,7 @@ public:
 	}
 	/// @}
 
-	void loadTexture(const CString& filename);
+	ANKI_USE_RESULT Error loadTexture(const CString& filename);
 
 private:
 	PerspectiveFrustum m_frustum;

+ 1 - 1
include/anki/scene/SceneNode.h

@@ -82,7 +82,7 @@ public:
 		{
 			if((*it)->getType() == type)
 			{
-				err = func(*(*it));
+				err = func((*it)->downCast<Component>());
 			}
 		}
 

+ 23 - 12
src/scene/Camera.cpp

@@ -12,19 +12,28 @@ namespace anki {
 //==============================================================================
 
 //==============================================================================
-Camera::Camera(
-	const CString& name, SceneGraph* scene, // SceneNode
-	Type type, Frustum* frustum) 
-:	SceneNode(name, scene), 
+Camera::Camera(SceneGraph* scene, Type type, Frustum* frustum) 
+:	SceneNode(scene), 
 	MoveComponent(this),
 	FrustumComponent(this, frustum),
 	SpatialComponent(this),
 	m_type(type)
+{}
+
+//==============================================================================
+Error Camera::create(const CString& name) 
 {
+	Error err = SceneNode::create(name, 3);
+
 	// Init components
-	addComponent(static_cast<MoveComponent*>(this));
-	addComponent(static_cast<FrustumComponent*>(this));
-	addComponent(static_cast<SpatialComponent*>(this));
+	if(!err)
+	{
+		addComponent(static_cast<MoveComponent*>(this));
+		addComponent(static_cast<FrustumComponent*>(this));
+		addComponent(static_cast<SpatialComponent*>(this));
+	}
+
+	return err;
 }
 
 //==============================================================================
@@ -61,7 +70,7 @@ void Camera::frustumUpdate()
 }
 
 //==============================================================================
-void Camera::onMoveComponentUpdate(SceneNode&, F32, F32)
+Error Camera::onMoveComponentUpdate(SceneNode&, F32, F32)
 {
 	// Frustum
 	FrustumComponent& fr = *this;
@@ -73,6 +82,8 @@ void Camera::onMoveComponentUpdate(SceneNode&, F32, F32)
 	// Spatial
 	SpatialComponent& sp = *this;
 	sp.markForUpdate();
+
+	return ErrorCode::NONE;
 }
 
 //==============================================================================
@@ -80,8 +91,8 @@ void Camera::onMoveComponentUpdate(SceneNode&, F32, F32)
 //==============================================================================
 
 //==============================================================================
-PerspectiveCamera::PerspectiveCamera(const CString& name, SceneGraph* scene)
-:	Camera(name, scene, Type::PERSPECTIVE, &m_frustum)
+PerspectiveCamera::PerspectiveCamera(SceneGraph* scene)
+:	Camera(scene, Type::PERSPECTIVE, &m_frustum)
 {}
 
 //==============================================================================
@@ -89,8 +100,8 @@ PerspectiveCamera::PerspectiveCamera(const CString& name, SceneGraph* scene)
 //==============================================================================
 
 //==============================================================================
-OrthographicCamera::OrthographicCamera(const CString& name, SceneGraph* scene)
-:	Camera(name, scene, Type::ORTHOGRAPHIC, &m_frustum)
+OrthographicCamera::OrthographicCamera(SceneGraph* scene)
+:	Camera(scene, Type::ORTHOGRAPHIC, &m_frustum)
 {}
 
 } // end namespace anki

+ 68 - 26
src/scene/Light.cpp

@@ -21,19 +21,27 @@ LightComponent::LightComponent(Light* node)
 //==============================================================================
 
 //==============================================================================
-Light::Light(
-	const CString& name, SceneGraph* scene, // SceneNode
-	Type t) // Self
-:	SceneNode(name, scene),
+Light::Light(SceneGraph* scene, Type t)
+:	SceneNode(scene),
 	LightComponent(this),
 	MoveComponent(this),
 	SpatialComponent(this),
 	m_type(t)
+{}
+
+//==============================================================================
+Error Light::create(const CString& name)
 {
-	// Register components
-	addComponent(static_cast<MoveComponent*>(this));
-	addComponent(static_cast<SpatialComponent*>(this));
-	addComponent(static_cast<LightComponent*>(this));
+	Error err = SceneNode::create(name, 3 + 1);
+	
+	if(!err)
+	{
+		addComponent(static_cast<MoveComponent*>(this));
+		addComponent(static_cast<SpatialComponent*>(this));
+		addComponent(static_cast<LightComponent*>(this));
+	}
+
+	return err;
 }
 
 //==============================================================================
@@ -44,13 +52,18 @@ Light::~Light()
 void Light::frustumUpdate()
 {
 	// Update the frustums
-	iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr)
+	Error err = iterateComponentsOfType<FrustumComponent>(
+		[&](FrustumComponent& fr) -> Error
 	{
 		fr.setProjectionMatrix(fr.getFrustum().calculateProjectionMatrix());
 		fr.setViewProjectionMatrix(
 			fr.getProjectionMatrix() * fr.getViewMatrix());
+
+		return ErrorCode::NONE;
 	});
 
+	(void)err;
+
 	// Mark the spatial for update
 	SpatialComponent& sp = getComponent<SpatialComponent>();
 	sp.markForUpdate();
@@ -62,7 +75,8 @@ void Light::onMoveComponentUpdateCommon()
 	MoveComponent& move = *this;
 
 	// Update the frustums
-	iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fr)
+	Error err = iterateComponentsOfType<FrustumComponent>(
+		[&](FrustumComponent& fr) -> Error
 	{
 		fr.setProjectionMatrix(fr.getFrustum().calculateProjectionMatrix());
 		fr.setViewMatrix(Mat4(move.getWorldTransform().getInverse()));
@@ -72,18 +86,22 @@ void Light::onMoveComponentUpdateCommon()
 		fr.getFrustum().resetTransform(move.getWorldTransform());
 
 		fr.markForUpdate();
+
+		return ErrorCode::NONE;
 	});
 
+	(void)err;
+
 	// Update the spatial
 	SpatialComponent& sp = getComponent<SpatialComponent>();
 	sp.markForUpdate();
 }
 
 //==============================================================================
-void Light::loadLensFlare(const CString& filename)
+Error Light::loadLensFlare(const CString& filename)
 {
 	ANKI_ASSERT(!hasLensFlare());
-	m_flaresTex.load(filename, &getResourceManager());
+	return m_flaresTex.load(filename, &getResourceManager());
 }
 
 //==============================================================================
@@ -91,23 +109,34 @@ void Light::loadLensFlare(const CString& filename)
 //==============================================================================
 
 //==============================================================================
-PointLight::PointLight(const CString& name, SceneGraph* scene)
-:	Light(name, scene, Light::Type::POINT)
+PointLight::PointLight(SceneGraph* scene)
+:	Light(scene, Light::Type::POINT)
 {}
 
 //==============================================================================
-void PointLight::onMoveComponentUpdate(SceneNode&, F32, F32)
+Error PointLight::create(const CString& name)
+{
+	return Light::create(name);
+}
+
+//==============================================================================
+Error PointLight::onMoveComponentUpdate(SceneNode&, F32, F32)
 {
 	m_sphereW.setCenter(getWorldTransform().getOrigin());
 	onMoveComponentUpdateCommon();
+	return ErrorCode::NONE;
 }
 
 //==============================================================================
-void PointLight::frameUpdate(F32 prevUpdateTime, F32 crntTime)
+Error PointLight::frameUpdate(F32 prevUpdateTime, F32 crntTime)
 {
 	if(getShadowEnabled() && m_shadowData == nullptr)
 	{
 		m_shadowData = getSceneAllocator().newInstance<ShadowData>(this);
+		if(m_shadowData == nullptr)
+		{
+			return ErrorCode::OUT_OF_MEMORY;
+		}
 
 		const F32 ang = toRad(90.0);
 		F32 dist = m_sphereW.getRadius();
@@ -128,6 +157,8 @@ void PointLight::frameUpdate(F32 prevUpdateTime, F32 crntTime)
 		trfs[4].setRotation(Mat3x4(Mat3(Axisang(ang, axis))));
 		trfs[5].setRotation(Mat3x4(Mat3(Axisang(-ang, axis))));
 	}
+
+	return ErrorCode::NONE;
 }
 
 //==============================================================================
@@ -135,30 +166,41 @@ void PointLight::frameUpdate(F32 prevUpdateTime, F32 crntTime)
 //==============================================================================
 
 //==============================================================================
-SpotLight::SpotLight(const CString& name, SceneGraph* scene)
-:	Light(name, scene, Light::Type::SPOT),
+SpotLight::SpotLight(SceneGraph* scene)
+:	Light(scene, Light::Type::SPOT),
 	FrustumComponent(this, &m_frustum)
+{}
+
+//==============================================================================
+Error SpotLight::create(const CString& name)
 {
-	// Init components
-	addComponent(static_cast<FrustumComponent*>(this));
+	Error err = Light::create(name);
+
+	if(!err)
+	{
+		addComponent(static_cast<FrustumComponent*>(this));
+
+		const F32 ang = toRad(45.0);
+		const F32 dist = 1.0;
 
-	const F32 ang = toRad(45.0);
-	const F32 dist = 1.0;
+		m_frustum.setAll(ang, ang, 0.1, dist);
+	}
 
-	m_frustum.setAll(ang, ang, 0.1, dist);
+	return err;
 }
 
 //==============================================================================
-void SpotLight::onMoveComponentUpdate(SceneNode&, F32, F32)
+Error SpotLight::onMoveComponentUpdate(SceneNode&, F32, F32)
 {
 	m_frustum.resetTransform(getWorldTransform());
 	onMoveComponentUpdateCommon();
+	return ErrorCode::NONE;
 }
 
 //==============================================================================
-void SpotLight::loadTexture(const CString& filename)
+Error SpotLight::loadTexture(const CString& filename)
 {
-	m_tex.load(filename, &getResourceManager());
+	return m_tex.load(filename, &getResourceManager());
 }
 
 } // end namespace anki