All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SceneElement.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "ISceneElement.h"
6 #include "Streaming.h"
7 #include "Types.h"
8 #include "ISceneElementRemover.h"
9 #include "ISceneElementInGraphObserver.h"
10 #include "MortonKey.h"
11 #include "PackedRenderable.h" // Has to be included to support the m_pRenderable delete below. Remove as soon as possible
12 #include "TextRenderable.h" // Has to be included to support the m_pRenderable delete below. Remove as soon as possible
13 
14 namespace Eegeo
15 {
16  namespace Rendering
17  {
18  namespace Scene
19  {
25  template <class TRenderableType>
26  class SceneElement : public Resources::IBuiltResource
27  {
28  public:
29  SceneElement(TRenderableType* pRenderable, const Streaming::MortonKey& key, std::string name)
30  : m_pRemover(NULL)
31  , m_pObserver(NULL)
32  , m_pRenderable(pRenderable)
33  , m_isInScene(false)
34  , m_key(key)
35  , m_name(std::move(name))
36  {
37  }
38 
39  ~SceneElement()
40  {
41  Eegeo_ASSERT(m_pRemover != NULL);
42  m_pRemover->Remove(this);
43 
44  #if !defined(EEGEO_UNITY)
45  Eegeo_ASSERT(m_pRenderable != NULL);
46  #endif
47 
48  Eegeo_DELETE(m_pRenderable); // Calling ~TRenderableType() without knowing its implementation? Smells a bit.
49  }
50 
51  // Do nothing - legacy, should never be called for this type.
52  // IBuiltResource needs this removing.
53  void Unload()
54  {
55  Eegeo_DELETE(this); //lol.
56  }
57 
58  bool IsInSceneGraph() const
59  {
60  return m_isInScene;
61  }
62 
63  void RemovedFromSceneGraph()
64  {
65  m_isInScene = false;
66  if(m_pObserver != NULL)
67  {
68  m_pObserver->OnRemovedFromSceneGraph(this);
69  }
70  }
71 
72  void AddedToSceneGraph()
73  {
74  m_isInScene = true;
75  if(m_pObserver != NULL)
76  {
77  m_pObserver->OnAddedToSceneGraph(this);
78  }
79  }
80 
81  const Eegeo::Streaming::MortonKey& GetKey() const
82  {
83  return m_key;
84  }
85 
86  const std::string& GetName() const
87  {
88  return m_name;
89  }
90 
91  void SetKey(const Eegeo::Streaming::MortonKey& key)
92  {
93  m_key = key;
94  }
95 
96  void SetRemover(Rendering::Scene::ISceneElementRemover<TRenderableType>* remover)
97  {
98  Eegeo_ASSERT(m_pRemover == NULL);
99  m_pRemover = remover;
100  }
101 
102  void SetObserver(Scene::ISceneElementInGraphObserver<TRenderableType>* pObserver)
103  {
104  m_pObserver = pObserver;
105  }
106 
107  TRenderableType& GetResource() const { return *m_pRenderable; }
108 
109  inline static void Prefetch(const SceneElement<TRenderableType>* pSceneElement)
110  {
111 #ifndef EEGEO_WIN
112  const int prefetchForRead = 0;
113  const u8* p = reinterpret_cast<const u8*>(pSceneElement);
114  __builtin_prefetch(p + 0, prefetchForRead, 0);
115 #endif
116  }
117  private:
118  Scene::ISceneElementRemover<TRenderableType>* m_pRemover;
119  Scene::ISceneElementInGraphObserver<TRenderableType>* m_pObserver;
120  TRenderableType* m_pRenderable;
121  bool m_isInScene;
123  std::string m_name;
124 
125  };
126  }
127  }
128 }