1 | // tPath.cpp  |
2 | //  |
3 | // This file implements scene splines and paths.  |
4 | //  |
5 | // Copyright (c) 2006, 2017 Tristan Grimmer.  |
6 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
7 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
8 | //  |
9 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
10 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
11 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
12 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
13 | // PERFORMANCE OF THIS SOFTWARE.  |
14 |   |
15 | #include "Scene/tPath.h"  |
16 | using namespace tMath;  |
17 | namespace tScene  |
18 | {  |
19 |   |
20 |   |
21 | tPath::tPath(const tBezierPath& src, const tString& name, uint32 id) :  |
22 | tObject(name, id)  |
23 | {  |
24 | if (!src.IsValid())  |
25 | return;  |
26 |   |
27 | Type = tType::CubicBezier;  |
28 | IsClosed = src.IsClosed();  |
29 |   |
30 | NumControlVerts = src.GetNumControlVerts();  |
31 | tAssert((NumControlVerts >= 4) && (((NumControlVerts-1) % 3) == 0));  |
32 |   |
33 | ControlVerts = new tVector3[NumControlVerts];  |
34 | for (int cv = 0; cv < NumControlVerts; cv++)  |
35 | ControlVerts[cv] = src.GetControlVerts()[cv];  |
36 | }  |
37 |   |
38 |   |
39 | void tPath::Save(tChunkWriter& chunk) const  |
40 | {  |
41 | if (!IsValid())  |
42 | return;  |
43 | tAssert((NumControlVerts > 0) && ControlVerts);  |
44 |   |
45 | chunk.Begin(tChunkID::Scene_Path);  |
46 | {  |
47 | tObject::Save(chunk);  |
48 |   |
49 | chunk.Begin(tChunkID::Scene_PathParameters);  |
50 | chunk.Write(Type);  |
51 | chunk.Write(NumControlVerts);  |
52 | chunk.Write(IsClosed);  |
53 | chunk.End();  |
54 |   |
55 | chunk.Begin(tChunkID::Scene_PathControlVertTable);  |
56 | chunk.Write(ControlVerts, NumControlVerts);  |
57 | chunk.End();  |
58 | }  |
59 | chunk.End();  |
60 | }  |
61 |   |
62 |   |
63 | void tPath::Load(const tChunk& splineChunk)  |
64 | {  |
65 | tAssert(splineChunk.ID() == tChunkID::Scene_Path);  |
66 | Clear();  |
67 |   |
68 | for (tChunk chunk = splineChunk.First(); chunk.Valid(); chunk = chunk.Next())  |
69 | {  |
70 | switch (chunk.ID())  |
71 | {  |
72 | case tChunkID::Scene_Object:  |
73 | tObject::Load(chunk);  |
74 | break;  |
75 |   |
76 | case tChunkID::Scene_PathParameters:  |
77 | chunk.GetItem(Type);  |
78 | chunk.GetItem(NumControlVerts);  |
79 | chunk.GetItem(IsClosed);  |
80 | break;  |
81 |   |
82 | case tChunkID::Scene_PathControlVertTable:  |
83 | tAssert((NumControlVerts > 0) && !ControlVerts);  |
84 | ControlVerts = new tVector3[NumControlVerts];  |
85 | tStd::tMemcpy(ControlVerts, chunk.GetData(), sizeof(tVector3) * NumControlVerts);  |
86 | break;  |
87 | }  |
88 | }  |
89 | }  |
90 |   |
91 |   |
92 | }  |
93 | |