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

Serialization code for animation curves

BearishSun 9 лет назад
Родитель
Сommit
a95d60fa8b

+ 1 - 0
Source/BansheeCore/CMakeSources.cmake

@@ -338,6 +338,7 @@ set(BS_BANSHEECORE_INC_RTTI
 	"Include/BsCAudioSourceRTTI.h"
 	"Include/BsCAudioListenerRTTI.h"
 	"Include/BsAnimationClipRTTI.h"
+	"Include/BsAnimationCurveRTTI.h"
 )
 
 set(BS_BANSHEECORE_SRC_RENDERER

+ 2 - 0
Source/BansheeCore/Include/BsAnimationCurve.h

@@ -60,6 +60,8 @@ namespace BansheeEngine
 		T evaluate(float time, bool loop = true);
 
 	private:
+		friend struct RTTIPlainType<TAnimationCurve<T>>;
+
 		/** 
 		 * Returns a pair of keys that can be used for interpolating to field the value at the provided time. This attempts
 		 * to find keys using the cache first, and if not possible falls back to a full search.

+ 118 - 0
Source/BansheeCore/Include/BsAnimationCurveRTTI.h

@@ -0,0 +1,118 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsRTTIType.h"
+#include "BsAnimationCurve.h"
+
+namespace BansheeEngine
+{
+	/** @cond RTTI */
+	/** @addtogroup RTTI-Impl-Core
+	 *  @{
+	 */
+
+	template<class T> struct RTTIPlainType<TKeyframe<T>>
+	{
+		enum { id = TID_KeyFrame }; enum { hasDynamicSize = 0 };
+
+		/** @copydoc RTTIPlainType::toMemory */
+		static void toMemory(const TKeyframe<T>& data, char* memory)
+		{
+			UINT32 size = sizeof(UINT32);
+			char* memoryStart = memory;
+			memory += sizeof(UINT32);
+
+			memory = rttiWriteElem(data.value, memory, size);
+			memory = rttiWriteElem(data.inTangent, memory, size);
+			memory = rttiWriteElem(data.outTangent, memory, size);
+			memory = rttiWriteElem(data.time, memory, size);
+
+			memcpy(memoryStart, &size, sizeof(UINT32));
+		}
+
+		/** @copydoc RTTIPlainType::fromMemory */
+		static UINT32 fromMemory(TKeyframe<T>& data, char* memory)
+		{
+			UINT32 size = 0;
+			memory = rttiReadElem(size, memory);
+			memory = rttiReadElem(data.value, memory);
+			memory = rttiReadElem(data.inTangent, memory);
+			memory = rttiReadElem(data.outTangent, memory);
+			memory = rttiReadElem(data.time, memory);
+			
+			return size;
+		}
+
+		/** @copydoc RTTIPlainType::getDynamicSize */
+		static UINT32 getDynamicSize(const TKeyframe<T>& data)
+		{
+			UINT64 dataSize = sizeof(UINT32);
+			dataSize += rttiGetElemSize(data.value);
+			dataSize += rttiGetElemSize(data.inTangent);
+			dataSize += rttiGetElemSize(data.outTangent);
+			dataSize += rttiGetElemSize(data.time);
+
+			assert(dataSize <= std::numeric_limits<UINT32>::max());
+
+			return (UINT32)dataSize;
+		}
+	};
+
+	template<class T> struct RTTIPlainType<TAnimationCurve<T>>
+	{
+		enum { id = TID_AnimationCurve }; enum { hasDynamicSize = 1 };
+
+		/** @copydoc RTTIPlainType::toMemory */
+		static void toMemory(const TAnimationCurve<T>& data, char* memory)
+		{
+			UINT32 size = sizeof(UINT32);
+			char* memoryStart = memory;
+			memory += sizeof(UINT32);
+
+			UINT32 version = 0; // In case the data structure changes
+			memory = rttiWriteElem(version, memory, size);
+			memory = rttiWriteElem(data.mStart, memory, size);
+			memory = rttiWriteElem(data.mEnd, memory, size);
+			memory = rttiWriteElem(data.mLength, memory, size);
+			memory = rttiWriteElem(data.mKeyframes, memory, size);
+
+			memcpy(memoryStart, &size, sizeof(UINT32));
+		}
+
+		/** @copydoc RTTIPlainType::fromMemory */
+		static UINT32 fromMemory(TAnimationCurve<T>& data, char* memory)
+		{
+			UINT32 size = 0;
+			memory = rttiReadElem(size, memory);
+
+			UINT32 version;
+			memory = rttiReadElem(version, memory);
+
+			memory = rttiReadElem(data.mStart, memory);
+			memory = rttiReadElem(data.mEnd, memory);
+			memory = rttiReadElem(data.mLength, memory);
+			memory = rttiReadElem(data.mKeyframes, memory);
+
+			return size;
+		}
+
+		/** @copydoc RTTIPlainType::getDynamicSize */
+		static UINT32 getDynamicSize(const TKeyframe<T>& data)
+		{
+			UINT64 dataSize = sizeof(UINT32) + sizeof(UINT32);
+			dataSize += rttiGetElemSize(data.mStart);
+			dataSize += rttiGetElemSize(data.mEnd);
+			dataSize += rttiGetElemSize(data.mLength);
+			dataSize += rttiGetElemSize(data.mKeyframes);
+
+			assert(dataSize <= std::numeric_limits<UINT32>::max());
+
+			return (UINT32)dataSize;
+		}
+	};
+	
+	/** @} */
+	/** @endcond */
+}

+ 1 - 1
Source/BansheeCore/Include/BsAnimationInstance.h

@@ -25,7 +25,7 @@ namespace BansheeEngine
 
 		float time; /**< Time at which to evaluate the curve. */
 	private:
-		template <class U> friend class TAnimationCurve;
+		friend class TAnimationCurve<T>;
 
 		mutable UINT32 cachedKey; /**< Left-most key the curve was last evaluated at. -1 if no cached data. */
 		mutable float cachedCurveStart; /**< Time relative to the animation curve, at which the cached data starts. */

+ 4 - 1
Source/BansheeCore/Include/BsCorePrerequisites.h

@@ -360,6 +360,7 @@ namespace BansheeEngine
 	class AudioSource;
 	class AudioClipImportOptions;
 	class AnimationClip;
+	template <class T> class TAnimationCurve;
 	// Asset import
 	class SpecificImporter;
 	class Importer;
@@ -511,7 +512,9 @@ namespace BansheeEngine
 		TID_AudioClipImportOptions = 1112,
 		TID_CAudioListener = 1113,
 		TID_CAudioSource = 1114,
-		TID_AnimationClip = 1115
+		TID_AnimationClip = 1115,
+		TID_AnimationCurve = 1116,
+		TID_KeyFrame = 1117
 	};
 }