Skip to content

Commit 4a8356c

Browse files
authored
Fixed invalid default scene when created through the asset browser (#369)
1 parent 11da2ca commit 4a8356c

8 files changed

Lines changed: 66 additions & 38 deletions

File tree

Sources/Overload/OvCore/include/OvCore/SceneSystem/Scene.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ namespace OvCore::SceneSystem
4545
*/
4646
~Scene();
4747

48+
/**
49+
* Add a default camera to the scene
50+
*/
51+
void AddDefaultCamera();
52+
53+
/**
54+
* Add default lights to the scene
55+
*/
56+
void AddDefaultLights();
57+
4858
/**
4959
* Play the scene
5060
*/

Sources/Overload/OvCore/include/OvCore/SceneSystem/SceneManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ namespace OvCore::SceneSystem
4848
void LoadEmptyScene();
4949

5050
/**
51-
* Load an empty lighted scene in memory
51+
* Load a default scene in memory
5252
*/
53-
void LoadEmptyLightedScene();
53+
void LoadDefaultScene();
5454

5555
/**
5656
* Load specific scene in memory
@@ -108,7 +108,7 @@ namespace OvCore::SceneSystem
108108

109109
private:
110110
const std::string m_sceneRootFolder;
111-
Scene* m_currentScene = nullptr;
111+
std::unique_ptr<Scene> m_currentScene = nullptr;
112112

113113
bool m_currentSceneLoadedFromPath = false;
114114
std::string m_currentSceneSourcePath = "";

Sources/Overload/OvCore/src/OvCore/SceneSystem/Scene.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <algorithm>
88
#include <string>
99

10-
#include "OvCore/SceneSystem/Scene.h"
10+
#include <OvCore/SceneSystem/Scene.h>
11+
#include <OvCore/ECS/Components/CDirectionalLight.h>
12+
#include <OvCore/ECS/Components/CAmbientSphereLight.h>
1113

1214
OvCore::SceneSystem::Scene::Scene()
1315
{
@@ -24,6 +26,25 @@ OvCore::SceneSystem::Scene::~Scene()
2426
m_actors.clear();
2527
}
2628

29+
void OvCore::SceneSystem::Scene::AddDefaultCamera()
30+
{
31+
auto& camera = CreateActor("Main Camera");
32+
camera.AddComponent<ECS::Components::CCamera>();
33+
camera.transform.SetLocalPosition({ 0.0f, 3.0f, 8.0f });
34+
camera.transform.SetLocalRotation(OvMaths::FQuaternion({ 20.0f, 180.0f, 0.0f }));
35+
}
36+
37+
void OvCore::SceneSystem::Scene::AddDefaultLights()
38+
{
39+
auto& directionalLight = CreateActor("Directional Light");
40+
directionalLight.AddComponent<ECS::Components::CDirectionalLight>().SetIntensity(0.75f);
41+
directionalLight.transform.SetLocalPosition({ 0.0f, 10.0f, 0.0f });
42+
directionalLight.transform.SetLocalRotation(OvMaths::FQuaternion({ 120.0f, -40.0f, 0.0f }));
43+
44+
auto& ambientLight = CreateActor("Ambient Light");
45+
ambientLight.AddComponent<ECS::Components::CAmbientSphereLight>().SetRadius(10000.0f);
46+
}
47+
2748
void OvCore::SceneSystem::Scene::Play()
2849
{
2950
m_isPlaying = true;

Sources/Overload/OvCore/src/OvCore/SceneSystem/SceneManager.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,17 @@ void OvCore::SceneSystem::SceneManager::LoadAndPlayDelayed(const std::string& p_
4545
void OvCore::SceneSystem::SceneManager::LoadEmptyScene()
4646
{
4747
UnloadCurrentScene();
48-
49-
m_currentScene = new Scene();
50-
48+
m_currentScene.reset(new Scene());
5149
SceneLoadEvent.Invoke();
5250
}
5351

54-
void OvCore::SceneSystem::SceneManager::LoadEmptyLightedScene()
52+
void OvCore::SceneSystem::SceneManager::LoadDefaultScene()
5553
{
5654
UnloadCurrentScene();
57-
58-
m_currentScene = new Scene();
59-
55+
m_currentScene.reset(new Scene());
56+
m_currentScene->AddDefaultCamera();
57+
m_currentScene->AddDefaultLights();
6058
SceneLoadEvent.Invoke();
61-
62-
auto& directionalLight = m_currentScene->CreateActor("Directional Light");
63-
directionalLight.AddComponent<ECS::Components::CDirectionalLight>().SetIntensity(0.75f);
64-
directionalLight.transform.SetLocalPosition({ 0.0f, 10.0f, 0.0f });
65-
directionalLight.transform.SetLocalRotation(OvMaths::FQuaternion({ 120.0f, -40.0f, 0.0f }));
66-
67-
auto& ambientLight = m_currentScene->CreateActor("Ambient Light");
68-
ambientLight.AddComponent<ECS::Components::CAmbientSphereLight>().SetRadius(10000.0f);
69-
70-
auto& camera = m_currentScene->CreateActor("Main Camera");
71-
camera.AddComponent<ECS::Components::CCamera>();
72-
camera.transform.SetLocalPosition({ 0.0f, 3.0f, 8.0f });
73-
camera.transform.SetLocalRotation(OvMaths::FQuaternion({ 20.0f, 180.0f, 0.0f }));
7459
}
7560

7661
bool OvCore::SceneSystem::SceneManager::LoadScene(const std::string& p_path, bool p_absolute)
@@ -114,8 +99,7 @@ void OvCore::SceneSystem::SceneManager::UnloadCurrentScene()
11499
{
115100
if (m_currentScene)
116101
{
117-
delete m_currentScene;
118-
m_currentScene = nullptr;
102+
m_currentScene.release();
119103
SceneUnloadEvent.Invoke();
120104
}
121105

@@ -124,12 +108,12 @@ void OvCore::SceneSystem::SceneManager::UnloadCurrentScene()
124108

125109
bool OvCore::SceneSystem::SceneManager::HasCurrentScene() const
126110
{
127-
return m_currentScene;
111+
return m_currentScene != nullptr;
128112
}
129113

130114
OvCore::SceneSystem::Scene* OvCore::SceneSystem::SceneManager::GetCurrentScene() const
131115
{
132-
return m_currentScene;
116+
return m_currentScene.get();
133117
}
134118

135119
std::string OvCore::SceneSystem::SceneManager::GetCurrentSceneSourcePath() const

Sources/Overload/OvEditor/include/OvEditor/Core/EditorActions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,10 @@ namespace OvEditor::Core
303303

304304
/**
305305
* Save the current scene to the given path
306+
* @param p_sceneToSave
306307
* @param p_path
307308
*/
308-
void SaveCurrentSceneTo(const std::string& p_path);
309+
void SaveSceneToDisk(OvCore::SceneSystem::Scene& p_scene, const std::string& p_path);
309310

310311
/**
311312
* Load a scene from the disk

Sources/Overload/OvEditor/src/OvEditor/Core/Editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ OvEditor::Core::Editor::Editor(Context& p_context) :
3838
Settings::EditorSettings::Load();
3939
SetupUI();
4040

41-
m_context.sceneManager.LoadEmptyLightedScene();
41+
m_context.sceneManager.LoadDefaultScene();
4242
}
4343

4444
OvEditor::Core::Editor::~Editor()

Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,21 @@ void OvEditor::Core::EditorActions::LoadEmptyScene()
5151
if (GetCurrentEditorMode() != EEditorMode::EDIT)
5252
StopPlaying();
5353

54-
m_context.sceneManager.LoadEmptyLightedScene();
54+
m_context.sceneManager.LoadEmptyScene();
55+
auto scene = m_context.sceneManager.GetCurrentScene();
56+
scene->AddDefaultCamera();
57+
scene->AddDefaultLights();
58+
5559
OVLOG_INFO("New scene created");
5660
}
5761

58-
void OvEditor::Core::EditorActions::SaveCurrentSceneTo(const std::string& p_path)
62+
void OvEditor::Core::EditorActions::SaveSceneToDisk(OvCore::SceneSystem::Scene& p_scene, const std::string& p_path)
5963
{
6064
tinyxml2::XMLDocument doc;
6165
tinyxml2::XMLNode* node = doc.NewElement("root");
6266
doc.InsertFirstChild(node);
6367
m_context.sceneManager.StoreCurrentSceneSourcePath(p_path);
64-
m_context.sceneManager.GetCurrentScene()->OnSerialize(doc, node);
68+
p_scene.OnSerialize(doc, node);
6569
doc.SaveFile(p_path.c_str());
6670
}
6771

@@ -84,8 +88,12 @@ void OvEditor::Core::EditorActions::SaveSceneChanges()
8488
{
8589
if (IsCurrentSceneLoadedFromDisk())
8690
{
87-
SaveCurrentSceneTo(m_context.sceneManager.GetCurrentSceneSourcePath());
88-
OVLOG_INFO("Current scene saved to: " + m_context.sceneManager.GetCurrentSceneSourcePath());
91+
auto currentScene = m_context.sceneManager.GetCurrentScene();
92+
OVASSERT(currentScene, "Current scene is null");
93+
94+
const std::string currentScenePath = m_context.sceneManager.GetCurrentSceneSourcePath();
95+
SaveSceneToDisk(*currentScene, currentScenePath);
96+
OVLOG_INFO("Current scene saved to: " + currentScenePath);
8997
}
9098
else
9199
{
@@ -112,7 +120,8 @@ void OvEditor::Core::EditorActions::SaveAs()
112120
}
113121
}
114122

115-
SaveCurrentSceneTo(dialog.GetSelectedFilePath());
123+
auto currentScene = m_context.sceneManager.GetCurrentScene();
124+
SaveSceneToDisk(*currentScene, dialog.GetSelectedFilePath());
116125
OVLOG_INFO("Current scene saved to: " + dialog.GetSelectedFilePath());
117126
}
118127
}

Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,11 @@ class FolderContextualMenu : public BrowserItemContextualMenu
279279
++fails;
280280
} while (std::filesystem::exists(finalPath));
281281

282-
std::ofstream outfile(finalPath);
283-
outfile << "<root><scene><actors><actor><name>Directional Light</name><tag></tag><active>true</active><id>1</id><parent>0</parent><components><component><type>class OvCore::ECS::Components::CDirectionalLight</type><data><diffuse><x>1</x><y>1</y><z>1</z></diffuse><specular><x>1</x><y>1</y><z>1</z></specular><intensity>0.75</intensity></data></component><component><type>class OvCore::ECS::Components::CTransform</type><data><position><x>0</x><y>10</y><z>0</z></position><rotation><x>0.81379771</x><y>-0.17101006</y><z>0.29619816</z><w>0.46984628</w></rotation><scale><x>1</x><y>1</y><z>1</z></scale></data></component></components><behaviours/></actor><actor><name>Ambient Light</name><tag></tag><active>true</active><id>2</id><parent>0</parent><components><component><type>class OvCore::ECS::Components::CAmbientSphereLight</type><data><ambient><x>1</x><y>1</y><z>1</z></ambient><intensity>0.1</intensity><radius>10000</radius></data></component><component><type>class OvCore::ECS::Components::CTransform</type><data><position><x>0</x><y>0</y><z>0</z></position><rotation><x>0</x><y>0</y><z>0</z><w>1</w></rotation><scale><x>1</x><y>1</y><z>1</z></scale></data></component></components><behaviours/></actor><actor><name>Main Camera</name><tag></tag><active>true</active><id>3</id><parent>0</parent><components><component><type>class OvCore::ECS::Components::CCamera</type><data><fov>45</fov><near>0.1</near><far>1000</far><clear_color><x>0.1921569</x><y>0.3019608</y><z>0.47450981</z></clear_color></data></component><component><type>class OvCore::ECS::Components::CTransform</type><data><position><x>0</x><y>3</y><z>8</z></position><rotation><x>-7.5904039e-09</x><y>0.98480773</y><z>-0.17364819</z><w>-4.3047311e-08</w></rotation><scale><x>1</x><y>1</y><z>1</z></scale></data></component></components><behaviours/></actor></actors></scene></root>" << std::endl; // Empty scene content
282+
auto emptyScene = OvCore::SceneSystem::Scene{};
283+
emptyScene.AddDefaultCamera();
284+
emptyScene.AddDefaultLights();
285+
286+
EDITOR_EXEC(SaveSceneToDisk(emptyScene, finalPath));
284287

285288
ItemAddedEvent.Invoke(finalPath);
286289
Close();

0 commit comments

Comments
 (0)