OpenClonk
C4MeshDenumerator.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 #include "C4Include.h"
20 
21 #include "object/C4Def.h"
22 #include "object/C4Object.h"
23 
24 const StdMeshInstance::AttachedMesh::DenumeratorFactoryFunc C4MeshDenumeratorFactory = StdMeshInstance::AttachedMesh::DenumeratorFactory<C4MeshDenumerator>;
25 
27 {
28  if (pComp->isDeserializer())
29  {
30  int32_t def;
31  pComp->Value(mkNamingCountAdapt(def, "ChildInstance"));
32 
33  if (def)
34  {
35  C4DefGraphics* pGfx = nullptr;
36  pComp->Value(mkNamingAdapt(C4DefGraphicsAdapt(pGfx), "ChildMesh"));
37  Def = pGfx->pDef;
38 
39  if (pGfx->Type != C4DefGraphics::TYPE_Mesh)
40  {
41  pComp->excCorrupt("ChildMesh points to non-mesh graphics");
42  }
43  assert(!attach->Child);
44  pComp->Value(mkParAdapt(mkNamingContextPtrAdapt(attach->Child, *pGfx->Mesh, "ChildInstance"), C4MeshDenumeratorFactory));
45  assert(attach->Child != nullptr);
46  attach->OwnChild = true; // Delete the newly allocated child instance when the parent instance is gone
47 
48  // TODO: Do we leak pGfx?
49  }
50  else
51  {
52  pComp->Value(mkNamingAdapt(Object, "ChildObject"));
53  attach->OwnChild = false; // Keep child instance when parent instance is gone since it belongs to a different object
54  }
55  }
56  else
57  {
58  int32_t def = 0;
59  if (Def)
60  {
61  ++def;
62  }
63  pComp->Value(mkNamingCountAdapt(def, "ChildInstance"));
64 
65  if (Def)
66  {
67  assert(attach->OwnChild);
68  C4DefGraphics* pGfx = &Def->Graphics;
69  assert(pGfx->Type == C4DefGraphics::TYPE_Mesh);
70  pComp->Value(mkNamingAdapt(C4DefGraphicsAdapt(pGfx), "ChildMesh"));
71  pComp->Value(mkParAdapt(mkNamingContextPtrAdapt(attach->Child, *pGfx->Mesh, "ChildInstance"), C4MeshDenumeratorFactory));
72  }
73  else
74  {
75  assert(!attach->OwnChild);
76  pComp->Value(mkNamingAdapt(Object, "ChildObject"));
77  }
78  }
79 }
80 
82 {
83  Object.DenumeratePointers();
84 
85  // Set child instance of attach after denumeration
86  if (Object)
87  {
88  assert(!attach->OwnChild);
89  assert(!attach->Child || attach->Child == Object->pMeshInstance);
90  if (!attach->Child)
91  {
92  attach->Child = Object->pMeshInstance;
93  }
94  }
95 }
96 
98 {
99  if (Object == pObj)
100  {
101  Object = nullptr;
102  // Return false causes the attached mesh to be deleted by StdMeshInstance
103  return false;
104  }
105 
106  return true;
107 }
const StdMeshInstance::AttachedMesh::DenumeratorFactoryFunc C4MeshDenumeratorFactory
StdContextPtrAdapt< T, ContextT > mkNamingContextPtrAdapt(T *&rpObj, const ContextT &ctx, const char *szNaming)
Definition: StdAdaptors.h:643
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:490
StdNamingCountAdapt< int_t > mkNamingCountAdapt(int_t &iCount, const char *szName)
Definition: StdAdaptors.h:1008
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
GraphicsType Type
Definition: C4DefGraphics.h:48
C4DefGraphics Graphics
Definition: C4Def.h:191
void DenumeratePointers(StdMeshInstance::AttachedMesh *attach) override
void CompileFunc(StdCompiler *pComp, StdMeshInstance::AttachedMesh *attach) override
bool ClearPointers(C4Object *pObj) override
StdMeshInstance * pMeshInstance
Definition: C4Object.h:154
void DenumeratePointers()
Definition: C4ObjectPtr.cpp:49
void excCorrupt(const char *szMessage,...)
Definition: StdCompiler.h:249
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
StdMeshInstance * Child
Definition: StdMesh.h:511
Denumerator *(* DenumeratorFactoryFunc)()
Definition: StdMesh.h:499