-
Notifications
You must be signed in to change notification settings - Fork 35
WIP: Refactoring scene tree to make unordered set of unique pointers #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -249,28 +249,27 @@ bool Scene::snatchChild(Scene* child) | |
| } | ||
|
|
||
| Unordered_set<Ptr<Scene>>& children = child->getParent()->getChildren(); | ||
| for (auto&& child_scene : children) | ||
| for (auto& child_scene : children) | ||
| { | ||
| if (child_scene.get() == child) | ||
| { | ||
| auto nh = children.extract(child_scene); | ||
| m_ChildrenScenes.insert(std::move(nh)); | ||
| child->m_ParentScene = this; | ||
| return true; | ||
| m_ChildrenScenes.insert(std::move(child_scene)); | ||
| children.erase(child_scene); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| for (int i = 0; i < children.size(); i++) | ||
| { | ||
| if (children.at(i).get() == child) | ||
| { | ||
| m_ChildrenScenes.insert(std::move(children[i])); | ||
| children.erase(children.begin() + i); | ||
| } | ||
| if (children.at(i).get() == child) | ||
| { | ||
| m_ChildrenScenes.insert(std::move(children[i])); | ||
| children.erase(children.begin() + i); | ||
| } | ||
| } | ||
| */ | ||
| return false; | ||
| child->m_ParentScene = this; | ||
| return true; | ||
| } | ||
|
|
||
| bool Scene::checkCycle(Scene* child) | ||
|
|
@@ -307,9 +306,8 @@ bool Scene::addChild(Ptr<Scene>& child) | |
| } | ||
| m_ChildrenScenes.insert(std::move(child)); | ||
|
|
||
| auto it = m_ChildrenScenes.end(); | ||
| --it; | ||
| ScriptSystem::GetSingleton()->addEnterScriptEntity(&(*it)->getEntity()); | ||
| //aarya | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| //ScriptSystem::GetSingleton()->addEnterScriptEntity(&m_ChildrenScenes.back()->getEntity()); | ||
| } | ||
| else | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,98 +1,98 @@ | ||
| #pragma once | ||
| #include "common/common.h" | ||
| #include "entity.h" | ||
| #include "core/input/input_manager.h" | ||
| class Entity; | ||
| #define ROOT_SCENE_ID 1 | ||
| struct SceneSettings | ||
| { | ||
| ResourceCollection preloads; | ||
| SceneID camera = ROOT_SCENE_ID; | ||
| SceneID listener = ROOT_SCENE_ID; | ||
| HashMap<String, InputScheme> inputSchemes; | ||
| String startScheme = {}; | ||
| void drawCameraSceneSelectables(Scene* scene, SceneID& toSet); | ||
| void drawListenerSceneSelectables(Scene* scene, SceneID& toSet); | ||
| void drawInputScheme(InputDescription& floatInput); | ||
| void draw(); | ||
| }; | ||
| void to_json(JSON::json& j, const SceneSettings& s); | ||
| void from_json(const JSON::json& j, SceneSettings& s); | ||
| class Scene | ||
| { | ||
| public: | ||
| enum class ImportStyle | ||
| { | ||
| /// If scene is not imported but created raw inside this scene | ||
| Local, | ||
| /// If scene is linked to another scene file | ||
| External | ||
| }; | ||
| private: | ||
| bool m_IsScenePaused; | ||
| static Vector<Scene*> s_Scenes; | ||
| SceneID m_ID; | ||
| String m_Name; | ||
| String m_FullName; | ||
| ImportStyle m_ImportStyle; | ||
| /// Contains the current file name if local, else contains the linked scene file | ||
| String m_SceneFile; | ||
| SceneSettings m_Settings; | ||
| Entity m_Entity; | ||
| Scene* m_ParentScene = nullptr; | ||
| Unordered_set<Ptr<Scene>> m_ChildrenScenes; | ||
| bool checkCycle(Scene* child); | ||
| public: | ||
| static void ResetNextID(); | ||
| static Ptr<Scene> Create(const JSON::json& sceneData, const bool assignNewIDs); | ||
| static Ptr<Scene> CreateFromFile(const String& sceneFile); | ||
| static Ptr<Scene> CreateEmpty(); | ||
| static Ptr<Scene> CreateEmptyAtPath(const String& sceneFile); | ||
| static Ptr<Scene> CreateRootScene(); | ||
| static bool isReservedName(const String& sceneName); | ||
| static Vector<Scene*> FindScenesByName(const String& name); | ||
| static Scene* FindSceneByID(const SceneID& id); | ||
| static const Vector<Scene*>& FindAllScenes(); | ||
| Scene(SceneID id, const String& name, const SceneSettings& settings, ImportStyle importStyle, const String& sceneFile); | ||
| ~Scene(); | ||
| Scene* findScene(SceneID scene); | ||
| void reimport(); | ||
| void onLoad(); | ||
| bool snatchChild(Scene* child); | ||
| bool addChild(Ptr<Scene>& child); | ||
| bool removeChild(Scene* toRemove); | ||
| void setName(const String& name); | ||
| JSON::json getJSON() const; | ||
| bool& getIsScenePaused() { return m_IsScenePaused; } | ||
| void setIsScenePaused(bool pause) { m_IsScenePaused = pause; } | ||
| Unordered_set<Ptr<Scene>>& getChildren() { return m_ChildrenScenes; } | ||
| SceneID getID() const { return m_ID; } | ||
| ImportStyle getImportStyle() const { return m_ImportStyle; } | ||
| String getScenePath() const { return m_SceneFile; } | ||
| Scene* getParent() const { return m_ParentScene; } | ||
| Entity& getEntity() { return m_Entity; } | ||
| const String& getName() const { return m_Name; } | ||
| const String& getFullName() const { return m_FullName; } | ||
| void setFullName(String& name) { m_FullName = name; } | ||
| SceneSettings& getSettings() { return m_Settings; } | ||
| }; | ||
| #pragma once | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix this, your diff shouldn't be the entire file. |
||
|
|
||
| #include "common/common.h" | ||
| #include "entity.h" | ||
| #include "core/input/input_manager.h" | ||
|
|
||
| class Entity; | ||
|
|
||
| #define ROOT_SCENE_ID 1 | ||
|
|
||
| struct SceneSettings | ||
| { | ||
| ResourceCollection preloads; | ||
| SceneID camera = ROOT_SCENE_ID; | ||
| SceneID listener = ROOT_SCENE_ID; | ||
| HashMap<String, InputScheme> inputSchemes; | ||
| String startScheme = {}; | ||
|
|
||
| void drawCameraSceneSelectables(Scene* scene, SceneID& toSet); | ||
| void drawListenerSceneSelectables(Scene* scene, SceneID& toSet); | ||
| void drawInputScheme(InputDescription& floatInput); | ||
| void draw(); | ||
| }; | ||
|
|
||
| void to_json(JSON::json& j, const SceneSettings& s); | ||
| void from_json(const JSON::json& j, SceneSettings& s); | ||
|
|
||
| class Scene | ||
| { | ||
| public: | ||
| enum class ImportStyle | ||
| { | ||
| /// If scene is not imported but created raw inside this scene | ||
| Local, | ||
| /// If scene is linked to another scene file | ||
| External | ||
| }; | ||
|
|
||
| private: | ||
| bool m_IsScenePaused; | ||
|
|
||
| static Vector<Scene*> s_Scenes; | ||
|
|
||
| SceneID m_ID; | ||
| String m_Name; | ||
| String m_FullName; | ||
| ImportStyle m_ImportStyle; | ||
| /// Contains the current file name if local, else contains the linked scene file | ||
| String m_SceneFile; | ||
| SceneSettings m_Settings; | ||
| Entity m_Entity; | ||
|
|
||
| Scene* m_ParentScene = nullptr; | ||
| Unordered_set<Ptr<Scene>> m_ChildrenScenes; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use an unordered set? What are the advantages of using one here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, we needed O(1) addressing along with O(1) deletion. Based on this, the most ideal data structure seemed to be an unodered_set. O(1) deletion was needed because currently in many functions we loop over the complete vector which isn't necessary.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Bashar-Ahmed Is there any performance testing done to see if using a cache inefficient data structure but with a theoretically better complexity is performing better than a vector? Also, do you want to save the order in which the children are added in a parent?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, as of now no performance testing has been done. Will have to look into it and get back to you. Also caching due to vector would've been minimal, if any, due to the fact that it was a vector of ptr, instead of the actual scenes. So when fetching the address,caching would be useful but when getting the object the caching wouldn't be effective. (Had a discussion regarding this with @sin3point14 ). Apologies if this approach is flawed, and do correct me. As far as the order is concerned, we looked at this aspect and weren't able to find any viable usecase where saving the order of scenes would be necessary
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One way the order is important is when you need to iterate over the children to display them on the editor. Seeing the order of the children change as you make any changes in the scene hierarchy can get confusing. |
||
|
|
||
| bool checkCycle(Scene* child); | ||
|
|
||
| public: | ||
| static void ResetNextID(); | ||
|
|
||
| static Ptr<Scene> Create(const JSON::json& sceneData, const bool assignNewIDs); | ||
| static Ptr<Scene> CreateFromFile(const String& sceneFile); | ||
| static Ptr<Scene> CreateEmpty(); | ||
| static Ptr<Scene> CreateEmptyAtPath(const String& sceneFile); | ||
| static Ptr<Scene> CreateRootScene(); | ||
| static bool isReservedName(const String& sceneName); | ||
|
|
||
| static Vector<Scene*> FindScenesByName(const String& name); | ||
| static Scene* FindSceneByID(const SceneID& id); | ||
| static const Vector<Scene*>& FindAllScenes(); | ||
|
|
||
| Scene(SceneID id, const String& name, const SceneSettings& settings, ImportStyle importStyle, const String& sceneFile); | ||
| ~Scene(); | ||
|
|
||
| Scene* findScene(SceneID scene); | ||
| void reimport(); | ||
|
|
||
| void onLoad(); | ||
| bool snatchChild(Scene* child); | ||
| bool addChild(Ptr<Scene>& child); | ||
| bool removeChild(Scene* toRemove); | ||
|
|
||
| void setName(const String& name); | ||
|
|
||
| JSON::json getJSON() const; | ||
| bool& getIsScenePaused() { return m_IsScenePaused; } | ||
| void setIsScenePaused(bool pause) { m_IsScenePaused = pause; } | ||
| Unordered_set<Ptr<Scene>>& getChildren() { return m_ChildrenScenes; } | ||
| SceneID getID() const { return m_ID; } | ||
| ImportStyle getImportStyle() const { return m_ImportStyle; } | ||
| String getScenePath() const { return m_SceneFile; } | ||
| Scene* getParent() const { return m_ParentScene; } | ||
| Entity& getEntity() { return m_Entity; } | ||
| const String& getName() const { return m_Name; } | ||
| const String& getFullName() const { return m_FullName; } | ||
| void setFullName(String& name) { m_FullName = name; } | ||
| SceneSettings& getSettings() { return m_Settings; } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason to use an lvalue reference instead of rvalue?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the universal binder, not an rvalue reference. However, this line diff doesn't seem to change anything.