OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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  pComp->excCorrupt("ChildMesh points to non-mesh graphics");
41  assert(!attach->Child);
42  pComp->Value(mkParAdapt(mkNamingContextPtrAdapt(attach->Child, *pGfx->Mesh, "ChildInstance"), C4MeshDenumeratorFactory));
43  assert(attach->Child != nullptr);
44  attach->OwnChild = true; // Delete the newly allocated child instance when the parent instance is gone
45 
46  // TODO: Do we leak pGfx?
47  }
48  else
49  {
50  pComp->Value(mkNamingAdapt(Object, "ChildObject"));
51  attach->OwnChild = false; // Keep child instance when parent instance is gone since it belongs to a different object
52  }
53  }
54  else
55  {
56  int32_t def = 0;
57  if(Def) ++def;
58  pComp->Value(mkNamingCountAdapt(def, "ChildInstance"));
59 
60  if(Def)
61  {
62  assert(attach->OwnChild);
63  C4DefGraphics* pGfx = &Def->Graphics;
64  assert(pGfx->Type == C4DefGraphics::TYPE_Mesh);
65  pComp->Value(mkNamingAdapt(C4DefGraphicsAdapt(pGfx), "ChildMesh"));
66  pComp->Value(mkParAdapt(mkNamingContextPtrAdapt(attach->Child, *pGfx->Mesh, "ChildInstance"), C4MeshDenumeratorFactory));
67  }
68  else
69  {
70  assert(!attach->OwnChild);
71  pComp->Value(mkNamingAdapt(Object, "ChildObject"));
72  }
73  }
74 }
75 
77 {
78  Object.DenumeratePointers();
79 
80  // Set child instance of attach after denumeration
81  if(Object)
82  {
83  assert(!attach->OwnChild);
84  assert(!attach->Child || attach->Child == Object->pMeshInstance);
85  if(!attach->Child)
86  attach->Child = Object->pMeshInstance;
87  }
88 }
89 
91 {
92  if(Object == pObj)
93  {
94  Object = nullptr;
95  // Return false causes the attached mesh to be deleted by StdMeshInstance
96  return false;
97  }
98 
99  return true;
100 }
StdNamingCountAdapt< int_t > mkNamingCountAdapt(int_t &iCount, const char *szName)
Definition: StdAdaptors.h:976
void excCorrupt(const char *szMessage,...)
Definition: StdCompiler.h:249
const StdMeshInstance::AttachedMesh::DenumeratorFactoryFunc C4MeshDenumeratorFactory
StdContextPtrAdapt< T, ContextT > mkNamingContextPtrAdapt(T *&rpObj, const ContextT &ctx, const char *szNaming)
Definition: StdAdaptors.h:611
StdMeshInstance * Child
Definition: StdMesh.h:511
bool ClearPointers(C4Object *pObj) override
void DenumeratePointers()
Definition: C4ObjectPtr.cpp:47
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void DenumeratePointers(StdMeshInstance::AttachedMesh *attach) override
void CompileFunc(StdCompiler *pComp, StdMeshInstance::AttachedMesh *attach) override
Denumerator *(* DenumeratorFactoryFunc)()
Definition: StdMesh.h:499
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
GraphicsType Type
Definition: C4DefGraphics.h:48
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:458
C4DefGraphics Graphics
Definition: C4Def.h:192
StdMeshInstance * pMeshInstance
Definition: C4Object.h:155