Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ScenePool.cpp
Go to the documentation of this file.
1
2
3#include "Headers/ScenePool.h"
4
7
10
11namespace Divide {
12
13namespace SceneList
14{
15 [[nodiscard]] static SceneFactoryMap& sceneFactoryMap()
16 {
17 NO_DESTROY static SceneFactoryMap sceneFactory{};
18 return sceneFactory;
19 }
20
21 [[nodiscard]] static SceneNameMap& sceneNameMap()
22 {
24 return sceneNameMap;
25 }
26
27 void registerSceneFactory(const char* name, const ScenePtrFactory& factoryFunc)
28 {
29 sceneNameMap()[_ID(name)] = name;
30 sceneFactoryMap()[_ID(name)] = factoryFunc;
31 }
32}
33
35 : _parentProject(parentMgr)
36{
37 assert(!SceneList::sceneFactoryMap().empty());
38}
39
41{
43 {
45 tempScenes.insert(eastl::cend(tempScenes),
46 eastl::cbegin(_createdScenes),
47 eastl::cend(_createdScenes));
48 }
49
50 for (const std::shared_ptr<Scene>& scene : tempScenes)
51 {
53 deleteScene(scene->getGUID());
54 }
55
56 {
58 _createdScenes.clear();
59 }
60}
61
62bool ScenePool::defaultSceneActive() const noexcept
63{
65}
66
68{
69 return _activeScene;
70}
71
72void ScenePool::activeScene(Scene& scene) noexcept
73{
74 _activeScene = &scene;
75}
76
78{
79 return _defaultScene;
80}
81
82Scene* ScenePool::getOrCreateScene(PlatformContext& context, Project& parent, const SceneEntry& sceneEntry, bool& foundInCache)
83{
84 DIVIDE_ASSERT(!sceneEntry._name.empty());
85
86 foundInCache = false;
87 Scene_ptr ret = nullptr;
88
90 for (const Scene_ptr& scene : _createdScenes)
91 {
92 if (scene->resourceName().compare(sceneEntry._name) == 0)
93 {
94 ret = scene;
95 foundInCache = true;
96 break;
97 }
98 }
99
100 if (ret == nullptr)
101 {
102 const auto creationFunc = SceneList::sceneFactoryMap()[_ID(sceneEntry._name.c_str())];
103 if (creationFunc)
104 {
105 ret = creationFunc(context, parent, sceneEntry );
106 }
107 else
108 {
109 ret = std::make_shared<Scene>(context, parent, sceneEntry );
110 }
111
112 // Default scene is the first scene we load
113 if (!_defaultScene)
114 {
115 _defaultScene = ret.get();
116 }
117
118 if (ret != nullptr)
119 {
120 _createdScenes.push_back(ret);
121 }
122 }
123
124 return ret.get();
125}
126
127bool ScenePool::deleteScene(const I64 targetGUID)
128{
129 if (targetGUID != -1)
130 {
131 const I64 defaultGUID = _defaultScene ? _defaultScene->getGUID() : 0;
132 const I64 activeGUID = _activeScene ? _activeScene->getGUID() : 0;
133
134 if (targetGUID != defaultGUID)
135 {
136 if (targetGUID == activeGUID && defaultGUID != 0)
137 {
139 }
140 }
141 else
142 {
143 _defaultScene = nullptr;
144 }
145
147 erase_if(_createdScenes, [&targetGUID](const auto& s) noexcept { return s->getGUID() == targetGUID; });
148
149 return true;
150 }
151
152 return false;
153}
154
156{
157 vector<Str<256>> scenes;
158 for (const SceneList::SceneNameMap::value_type& it : SceneList::sceneNameMap())
159 {
160 scenes.push_back(it.second);
161 }
162
163 if (sorted)
164 {
165 eastl::sort(begin(scenes),
166 end(scenes),
167 [](const Str<256>& a, const Str<256>& b)
168 {
169 return a < b;
170 });
171 }
172
173 return scenes;
174}
175
176} //namespace Divide
#define DIVIDE_ASSERT(...)
#define NO_DESTROY
static bool unloadScene(Divide::Project &project, Scene *scene)
FORCE_INLINE I64 getGUID() const noexcept
Definition: GUIDWrapper.h:51
void setActiveScene(Scene *scene)
bool deleteScene(I64 targetGUID)
Definition: ScenePool.cpp:127
bool defaultSceneActive() const noexcept
Definition: ScenePool.cpp:62
Scene * defaultScene() const noexcept
Definition: ScenePool.cpp:77
vector< Str< 256 > > customCodeScenes(bool sorted) const
Definition: ScenePool.cpp:155
Scene * getOrCreateScene(PlatformContext &context, Project &parent, const SceneEntry &sceneEntry, bool &foundInCache)
Definition: ScenePool.cpp:82
Scene * activeScene() const noexcept
Definition: ScenePool.cpp:67
ScenePool(Project &parentProject)
Definition: ScenePool.cpp:34
Project & _parentProject
Definition: ScenePool.h:68
SharedMutex _sceneLock
Definition: ScenePool.h:66
Scene * _defaultScene
Definition: ScenePool.h:72
vector< Scene_ptr > _createdScenes
Definition: ScenePool.h:74
Scene * _activeScene
Definition: ScenePool.h:70
void registerSceneFactory(const char *name, const ScenePtrFactory &factoryFunc)
Definition: ScenePool.cpp:27
static SceneFactoryMap & sceneFactoryMap()
Definition: ScenePool.cpp:15
std::unordered_map< U64, ScenePtrFactory > SceneFactoryMap
Definition: Scene.h:534
std::unordered_map< U64, Str< 256 > > SceneNameMap
Definition: Scene.h:535
static SceneNameMap & sceneNameMap()
Definition: ScenePool.cpp:21
std::function< std::shared_ptr< Scene >(PlatformContext &context, Project &parent, const SceneEntry &name)> ScenePtrFactory
Definition: Scene.h:533
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
eastl::vector< Type > vector
Definition: Vector.h:42
std::shared_lock< mutex > SharedLock
Definition: SharedMutex.h:49
Project & parent
Definition: DefaultScene.h:41
constexpr U64 _ID(const char *const str, const U64 value=val_64_const) noexcept
int64_t I64
Definition: Scene.h:112
Str< 256 > _name
Definition: Scene.h:113