Made a Simple scene manager with loading and saving

This commit is contained in:
OusmBlueNinja 2024-12-26 22:08:50 -06:00
parent e8b43ae843
commit d204e68cf3
4 changed files with 86 additions and 14 deletions

View File

@ -1,6 +1,6 @@
[Window][DockSpace]
Pos=0,0
Size=1920,1177
Size=1280,720
Collapsed=0
[Window][Debug##Default]
@ -9,26 +9,26 @@ Size=400,400
Collapsed=0
[Window][Inspector]
Pos=1588,27
Size=324,587
Pos=948,27
Size=324,352
Collapsed=0
DockId=0x00000005,0
[Window][OpenGL Output]
Pos=375,27
Size=1211,770
Pos=374,27
Size=572,313
Collapsed=0
DockId=0x00000003,0
[Window][Performance]
Pos=1588,616
Size=324,553
Pos=948,381
Size=324,331
Collapsed=0
DockId=0x00000006,0
[Window][Logger]
Pos=375,799
Size=1211,370
Pos=374,342
Size=572,370
Collapsed=0
DockId=0x00000004,0
@ -40,14 +40,14 @@ DockId=0x00000007,0
[Window][Scene Window]
Pos=8,27
Size=365,1142
Size=364,685
Collapsed=0
DockId=0x00000009,0
[Docking][Data]
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,27 Size=1904,1142 Split=X Selected=0xF7365A5A
DockNode ID=0x00000009 Parent=0x14621557 SizeRef=365,1142 HiddenTabBar=1 Selected=0x3DC5AC3F
DockNode ID=0x0000000A Parent=0x14621557 SizeRef=1537,1142 Split=X
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,27 Size=1264,685 Split=X Selected=0xF7365A5A
DockNode ID=0x00000009 Parent=0x14621557 SizeRef=364,1142 HiddenTabBar=1 Selected=0x3DC5AC3F
DockNode ID=0x0000000A Parent=0x14621557 SizeRef=1538,1142 Split=X
DockNode ID=0x00000007 Parent=0x0000000A SizeRef=357,1142 Selected=0x7737E8B2
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=1545,1142 Split=X
DockNode ID=0x00000001 Parent=0x00000008 SizeRef=1211,1142 Split=Y Selected=0xF7365A5A

View File

@ -11,6 +11,8 @@
#include <GLFW/glfw3.h>
#include <string>
// Dear ImGui
#include "imgui.h"
#include "imgui_impl_glfw.h"
@ -23,6 +25,8 @@
#include "Windows/SceneWindow.h"
#include "Engine/ThemeManager.h"
#include "Engine/SceneManager.h"
// #define YAML_CPP_STATIC_DEFINE
#include <yaml-cpp/yaml.h>
@ -37,7 +41,6 @@ std::vector<std::shared_ptr<GameObject>> g_GameObjects;
int g_GPU_Triangles_drawn_to_screen = 0;
GameObject *g_SelectedObject; // Pointer to the currently selected object
bool MyEngine::Init(int width, int height, const std::string &title)
@ -313,7 +316,19 @@ void MyEngine::ShowDockSpace()
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Exit"))
{
m_Running = false; // Stop the engine
}
if (ImGui::MenuItem("Save"))
{
m_LoggerWindow->AddLog("Saveing Scene");
}
if (ImGui::MenuItem("Load"))
{
m_LoggerWindow->AddLog("Loading Scene");
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();

View File

@ -0,0 +1,44 @@
#include "SceneManager.h"
#include "Component.h"
#include "Transform.h"
#include "Mesh.h"
#include "GameObject.h"
#include <yaml-cpp/yaml.h>
#include <fstream>
void SceneManager::SaveScene(const std::vector<std::shared_ptr<GameObject>> &gameobjects, const std::string &filename)
{
YAML::Node sceneNode;
for (const auto &gameobject : gameobjects)
{
sceneNode["Entities"].push_back(gameobject->Serialize());
}
std::ofstream fout(filename);
fout << sceneNode;
}
void SceneManager::LoadScene(std::vector<std::shared_ptr<GameObject>> &gameobjects, const std::string &filename)
{
YAML::Node sceneNode = YAML::LoadFile(filename);
gameobjects.clear();
if (sceneNode["Entities"])
{
for (const auto &gameobjectNode : sceneNode["Entities"])
{
int id = gameobjectNode["ID"].as<int>();
std::string name = gameobjectNode["Name"].as<std::string>();
auto gameobject = std::make_shared<GameObject>(id, name);
gameobject->Deserialize(gameobjectNode);
gameobjects.push_back(gameobject);
}
}
}

13
src/Engine/SceneManager.h Normal file
View File

@ -0,0 +1,13 @@
#include "GameObject.h"
class SceneManager
{
public:
void SaveScene(const std::vector<std::shared_ptr<GameObject>> &gameobjects, const std::string &filename);
void LoadScene(std::vector<std::shared_ptr<GameObject>> &gameobjects, const std::string &filename);
private:
};