Panagiotis Christopoulos Charitos 14 лет назад
Родитель
Сommit
9c49aeaab8
5 измененных файлов с 107 добавлено и 80 удалено
  1. 1 1
      anki/collision/Frustum.cpp
  2. 8 2
      anki/scene/Movable.cpp
  3. 4 7
      anki/scene/Movable.h
  4. 20 0
      anki/scene/Property.cpp
  5. 74 70
      anki/scene/Property.h

+ 1 - 1
anki/collision/Frustum.cpp

@@ -160,7 +160,7 @@ Mat4 PerspectiveFrustum::calculateProjectionMatrix() const
 	projectionMat(1, 3) = 0.0;
 	projectionMat(2, 0) = 0.0;
 	projectionMat(2, 1) = 0.0;
-	projectionMat(2, 2) = (zFar + zNear) / ( zNear - zFar);
+	projectionMat(2, 2) = (zFar + zNear) / (zNear - zFar);
 	projectionMat(2, 3) = (2.0 * zFar * zNear) / (zNear - zFar);
 	projectionMat(3, 0) = 0.0;
 	projectionMat(3, 1) = 0.0;

+ 8 - 2
anki/scene/Movable.cpp

@@ -1,5 +1,6 @@
 #include "anki/scene/Movable.h"
 #include "anki/scene/Property.h"
+#include <boost/foreach.hpp>
 
 
 namespace anki {
@@ -9,8 +10,8 @@ namespace anki {
 Movable::Movable(uint flags_, Movable* parent, PropertyMap& pmap)
 	: Base(this, parent), flags(flags_)
 {
-	pmap.addProperty("localTransform", &lTrf, PropertyBase::PF_READ_WRITE);
-	pmap.addProperty("worldTransform", &wTrf, PropertyBase::PF_READ);
+	pmap.addNewProperty(new ReadWritePointerProperty<Transform>("localTransform", &lTrf));
+	pmap.addNewProperty(new ReadPointerProperty<Transform>("worldTransform", &wTrf));
 }
 
 
@@ -37,6 +38,11 @@ void Movable::updateWorldTransform()
 	}
 
 	enableFlag(MF_MOVED, prevWTrf != wTrf);
+
+	BOOST_FOREACH(Movable* child, getChildren())
+	{
+		child->updateWorldTransform();
+	}
 }
 
 

+ 4 - 7
anki/scene/Movable.h

@@ -43,10 +43,6 @@ public:
 	{
 		return lTrf;
 	}
-	Transform& getLocalTransform()
-	{
-		return lTrf;
-	}
 	void setLocalTransform(const Transform& x)
 	{
 		lTrf = x;
@@ -115,9 +111,6 @@ public:
 	}
 	/// @}
 
-	/// This update happens always. It updates the MF_MOVED flag
-	void updateWorldTransform();
-
 	/// This is called after the updateWorldTransform() and if the MF_MOVED is
 	/// true
 	virtual void moveUpdate()
@@ -136,6 +129,10 @@ protected:
 	Transform prevWTrf;
 
 	ulong flags; ///< The state flags
+
+	/// Called when the local transform changes. Then its called fpr all the
+	/// children. It updates the MF_MOVED flag
+	void updateWorldTransform();
 };
 /// @}
 

+ 20 - 0
anki/scene/Property.cpp

@@ -0,0 +1,20 @@
+#include "anki/scene/Property.h"
+
+
+namespace anki {
+
+
+/// To avoid idiotic mistakes
+#define ANKI_DEFINE_PROPERTY_TYPE(SubType_) \
+	class SubType_; \
+	template<> const uint Property<SubType_>::TYPE_ID = __LINE__;
+
+
+template<> const uint Property<float>::TYPE_ID = __LINE__;
+ANKI_DEFINE_PROPERTY_TYPE(Vec2)
+ANKI_DEFINE_PROPERTY_TYPE(Vec3)
+ANKI_DEFINE_PROPERTY_TYPE(OrthographicFrustum)
+ANKI_DEFINE_PROPERTY_TYPE(PerspectiveFrustum)
+
+
+}  // namespace anki

+ 74 - 70
anki/scene/Property.h

@@ -4,9 +4,9 @@
 #include "anki/util/Observer.h"
 #include "anki/util/Exception.h"
 #include "anki/util/Assert.h"
+#include "anki/util/ConstCharPtrHashMap.h"
 #include <boost/ptr_container/ptr_vector.hpp>
 #include <boost/shared_ptr.hpp>
-#include <boost/unordered_map.hpp>
 
 
 namespace anki {
@@ -21,21 +21,12 @@ class Property;
 class PropertyBase
 {
 public:
-	/// Flags for property
-	enum PropertyFlag
-	{
-		PF_NONE = 0,
-		PF_READ = 1,
-		PF_WRITE = 2,
-		PF_READ_WRITE = PF_READ | PF_WRITE
-	};
-
 	/// @name Constructors/Destructor
 	/// @{
-	PropertyBase(const char* name_, uint tid_, uint flags_ = PF_NONE)
-		: name(name_), tid(tid_), flags(flags_)
+	PropertyBase(const char* name_, uint tid_)
+		: name(name_), tid(tid_)
 	{
-		ANKI_ASSERT(tid != 0);
+		ANKI_ASSERT(tid != 0 && "Property::TYPE_ID not set");
 	}
 
 	virtual ~PropertyBase()
@@ -68,26 +59,9 @@ public:
 		return static_cast<Property<T>*>(this)->setValue(x);
 	}
 
-	bool isFlagEnabled(PropertyFlag flag) const
-	{
-		return flags & flag;
-	}
-	/// @}
-
-protected:
-	void enableFlag(PropertyFlag flag, bool enable = true)
-	{
-		flags = enable ? flags | flag : flags & ~flag;
-	}
-	void disableFlag(PropertyFlag flag)
-	{
-		enableFlag(flag, false);
-	}
-
 private:
 	std::string name;
 	uint tid;
-	uint flags;
 
 	/// Runtime type checking
 	template<typename T>
@@ -115,52 +89,92 @@ public:
 	/// @{
 
 	/// Read only property
-	Property(const char* name, const Value* x, uint flags = PF_NONE)
-		: PropertyBase(name, TYPE_ID, flags), ptr(x)
-	{
-		disableFlag(PF_WRITE);
-	}
-
-	/// Read/write property
-	Property(const char* name, Value* x, uint flags = PF_NONE)
-		: PropertyBase(name, TYPE_ID, flags), ptr(x)
+	Property(const char* name)
+		: PropertyBase(name, TYPE_ID)
 	{}
 	/// @}
 
 	/// @name Accessors
 	/// @{
-	const Value& getValue() const
+	virtual const Value& getValue() const
 	{
-		if(!isFlagEnabled(PF_READ))
-		{
-			throw ANKI_EXCEPTION("Property is not readable: " + name);
-		}
-
-		return *ptr;
+		throw ANKI_EXCEPTION("Property is not readable: " + getName());
 	}
 
 	/// Set the value and emit the signal valueChanged
-	void setValue(const Value& x)
+	virtual void setValue(const Value& x)
 	{
-		if(!isFlagEnabled(PF_WRITE))
-		{
-			throw ANKI_EXCEPTION("Property is not writable: " + name);
-		}
-
-		*(const_cast<Value*>(ptr)) = x;
-		ANKI_EMIT valueChanged(x);
+		throw ANKI_EXCEPTION("Property is not writable: " + getName());
 	}
 	/// @}
 
 	ANKI_SIGNAL(const Value&, valueChanged)
+};
+
+
+template<typename T>
+const uint Property<T>::TYPE_ID = 0;
+
+
+/// XXX
+template<typename T>
+class ReadPointerProperty: public Property<T>
+{
+public:
+	typedef T Value;
+	typedef Property<T> Base;
+
+	ReadPointerProperty(const char* name, const Value* x)
+		: Base(name), ptr(x)
+	{}
+
+	/// @name Accessors
+	/// @{
+
+	/// Overrides Property::getValue()
+	const Value& getValue() const
+	{
+		return *ptr;
+	}
+	/// @}
 
 private:
 	const Value* ptr; ///< Have only one const pointer for size saving
 };
 
 
+/// XXX
 template<typename T>
-const uint Property<T>::TYPE_ID = 0;
+class ReadWritePointerProperty: public Property<T>
+{
+public:
+	typedef T Value;
+	typedef Property<T> Base;
+
+	ReadWritePointerProperty(const char* name, Value* x)
+		: Base(name), ptr(x)
+	{}
+
+	/// @name Accessors
+	/// @{
+
+	/// Overrides Property::getValue()
+	const Value& getValue() const
+	{
+		return *ptr;
+	}
+
+	/// Overrides Property::setValue()
+	virtual void setValue(const Value& x)
+	{
+		*ptr = x;
+		ANKI_EMIT valueChanged(x);
+	}
+	/// @}
+
+private:
+	Value* ptr; ///< Have only one const pointer for size saving
+};
 
 
 /// A set of properties
@@ -168,28 +182,23 @@ class PropertyMap
 {
 public:
 	typedef boost::ptr_vector<PropertyBase> Container;
-	typedef boost::unordered_map<std::string, PropertyBase*> NameToPropertyMap;
-
+	typedef ConstCharPtrHashMap<PropertyBase*>::Type NameToPropertyMap;
 
 	/// Create a new property
 	template<typename T>
-	Property<T>& addProperty(const char* name, T* x,
-		uint flags = PropertyBase::PF_NONE)
+	Property<T>& addNewProperty(Property<T>* newp)
 	{
-		if(propertyExists(name))
+		if(propertyExists(newp->getName().c_str()))
 		{
-			throw ANKI_EXCEPTION("Property already exists: " + name);
+			throw ANKI_EXCEPTION("Property already exists: " + newp->getName());
 		}
 
-		Property<T>* newp = new Property<T>(name, x, flags);
-
 		props.push_back(newp);
-		map[name] = newp;
+		map[newp->getName().c_str()] = newp;
 
 		return *newp;
 	}
 
-
 	/// XXX
 	const PropertyBase& findPropertyBaseByName(const char* name) const
 	{
@@ -201,7 +210,6 @@ public:
 		return *(it->second);
 	}
 
-
 	/// XXX
 	PropertyBase& findPropertyBaseByName(const char* name)
 	{
@@ -213,28 +221,24 @@ public:
 		return *(it->second);
 	}
 
-
 	/// Alias for findPropertyBaseByName
 	const PropertyBase& operator[](const char* name) const
 	{
 		return findPropertyBaseByName(name);
 	}
 
-
 	/// Alias for findPropertyBaseByName
 	PropertyBase& operator[](const char* name)
 	{
 		return findPropertyBaseByName(name);
 	}
 
-
 	/// Return true if the property named @a name exists
 	bool propertyExists(const char* name) const
 	{
 		return map.find(name) != map.end();
 	}
 
-
 	/// XXX
 	template<typename T>
 	void setValue(const char* name, const T& v)