1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "Prerequisites/BsPrerequisitesUtil.h"
6#include "Image/BsColorGradient.h"
7#include "Error/BsException.h"
8#include "Debug/BsDebug.h"
9
10namespace bs
11{
12 /** @cond RTTI */
13 /** @addtogroup RTTI-Impl-Utility
14 * @{
15 */
16
17 template<> struct RTTIPlainType<ColorGradient>
18 {
19 enum { id = TID_ColorGradient }; enum { hasDynamicSize = 1 };
20
21 static void toMemory(const ColorGradient& data, char* memory)
22 {
23 UINT32 size = getDynamicSize(data);
24
25 const UINT32 curSize = sizeof(UINT32);
26 memcpy(memory, &size, curSize);
27 memory += curSize;
28
29 const UINT32 version = 0;
30 memory = rttiWriteElem(version, memory);
31
32 for(UINT32 i = 0; i < ColorGradient::MAX_KEYS; i++)
33 {
34 memory = rttiWriteElem(data.mColors[i], memory);
35 memory = rttiWriteElem(data.mTimes[i], memory);
36 }
37
38 memory = rttiWriteElem(data.mNumKeys, memory);
39 memory = rttiWriteElem(data.mDuration, memory);
40 }
41
42 static UINT32 fromMemory(ColorGradient& data, char* memory)
43 {
44 UINT32 size;
45 memcpy(&size, memory, sizeof(UINT32));
46 memory += sizeof(UINT32);
47
48 UINT32 version;
49 memory = rttiReadElem(version, memory);
50
51 switch(version)
52 {
53 case 0:
54 for (UINT32 i = 0; i < ColorGradient::MAX_KEYS; i++)
55 {
56 memory = rttiReadElem(data.mColors[i], memory);
57 memory = rttiReadElem(data.mTimes[i], memory);
58 }
59
60 memory = rttiReadElem(data.mNumKeys, memory);
61 memory = rttiReadElem(data.mDuration, memory);
62 break;
63 default:
64 LOGERR("Unknown version of ColorGradient data. Unable to deserialize.");
65 break;
66 }
67
68 return size;
69 }
70
71 static UINT32 getDynamicSize(const ColorGradient& data)
72 {
73 const UINT64 dataSize =
74 rttiGetElemSize(data.mColors[0]) * ColorGradient::MAX_KEYS +
75 rttiGetElemSize(data.mTimes[0]) * ColorGradient::MAX_KEYS +
76 rttiGetElemSize(data.mNumKeys) + rttiGetElemSize(data.mDuration) + sizeof(UINT32) * 2;
77
78#if BS_DEBUG_MODE
79 if(dataSize > std::numeric_limits<UINT32>::max())
80 {
81 BS_EXCEPT(InternalErrorException, "Data overflow! Size doesn't fit into 32 bits.");
82 }
83#endif
84
85 return (UINT32)dataSize;
86 }
87 };
88
89 /** @} */
90 /** @endcond */
91}
92