From c57177a1a933b83238ad35e17642939dc0bb3e6c Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Wed, 9 Nov 2022 15:27:06 +0100 Subject: [PATCH] Rudimentary Scene loading --- Editor/src/SceneSerializer.h | 162 +++++++++++++++++++++-------------- 1 file changed, 96 insertions(+), 66 deletions(-) diff --git a/Editor/src/SceneSerializer.h b/Editor/src/SceneSerializer.h index c900462..55816b9 100644 --- a/Editor/src/SceneSerializer.h +++ b/Editor/src/SceneSerializer.h @@ -1,9 +1,13 @@ #pragma once #include +#include + #include #include #include +#include "../../YoggieEngine/src/Scene/Entity.h" + void WriteFile(std::string& emitter, std::filesystem::path path) { std::cout << "Writing Scene file to: " << path.u8string() << std::endl; @@ -16,74 +20,72 @@ void WriteFile(std::string& emitter, std::filesystem::path path) } YAML::Emitter& operator<< (YAML::Emitter& emitter, glm::vec3& vector) { - emitter << YAML::Flow << YAML::BeginSeq << vector.x << vector.y << vector.x << YAML::EndSeq; + emitter << YAML::Flow << YAML::BeginSeq << vector.x << vector.y << vector.x << YAML::EndSeq; return emitter; } -void Serialize(YAML::Emitter& emitter, TransformComponent& transform) -{ - emitter << YAML::BeginMap; - emitter << YAML::Key << "Transform" << YAML::Value << YAML::Flow << YAML::BeginSeq; - - emitter << YAML::Key << "Position"; - emitter << YAML::Value << transform.Position; - - emitter << YAML::Key << "Rotation"; - emitter << YAML::Value << transform.Rotation; - - emitter << YAML::Key << "Scale"; - emitter << YAML::Value << transform.Scale; - - emitter << YAML::EndSeq << YAML::EndMap; -} - -void Serialize(YAML::Emitter& emitter, IdentifierComponent& identifier) -{ - emitter << YAML::BeginMap; - emitter << YAML::Key << "Ident"; - emitter << YAML::Value << identifier.name; - emitter << YAML::EndMap; -} - -void Serialize(YAML::Emitter& emitter, LightComponent& light) -{ - emitter << YAML::BeginMap << "Light"; - emitter << YAML::Key << "strength"; - emitter << YAML::Value << light.Strength; - - emitter << YAML::Key << "Color"; - emitter << YAML::Value << light.Color; - emitter << YAML::EndMap; -} - std::string Serialize( Scene& scene) { - YAML::Emitter sceneYAML; + YAML::Emitter emitter; - sceneYAML << "YOGGIE_SCENE_FILE" ; + emitter << YAML::BeginMap; + emitter << YAML::Key << "Scene" << YAML::Value << "test-Scene"; + emitter << YAML::Key << "Entities" << YAML::Value << YAML::BeginSeq; - - scene.getReg().each([&scene, &sceneYAML](auto enttNumber) { + scene.getReg().each([&scene, &emitter](auto enttNumber) { Entity entity = Entity(enttNumber, &scene); - - Serialize(sceneYAML, entity.GetComponent()); - Serialize(sceneYAML, entity.GetComponent()); - if (entity.HasComponent()) { - Serialize(sceneYAML, entity.GetComponent()); + + emitter << YAML::BeginMap; + emitter << YAML::Key << "Entity" << YAML::Value << entity.GetComponent().name; + + if (entity.HasComponent()) { + emitter << YAML::Key << "Ident"; + emitter << YAML::BeginMap; + emitter << YAML::Value << entity.GetComponent().name; + emitter << YAML::EndMap; } + if (entity.HasComponent()) { + emitter << YAML::Key << "Transform" << YAML::Value ; + + emitter << YAML::BeginMap; + + emitter << YAML::Key << "Position"; + emitter << YAML::Value << entity.GetComponent().Position; + + emitter << YAML::Key << "Rotation"; + emitter << YAML::Value << entity.GetComponent().Rotation; + + emitter << YAML::Key << "Scale"; + emitter << YAML::Value << entity.GetComponent().Scale; + + emitter << YAML::EndMap; + } + + + if (entity.HasComponent()) { + emitter << YAML::Key << "Light"; + emitter << YAML::Value; + emitter << YAML::BeginMap; + emitter << YAML::Key << "strength"; + emitter << YAML::Value << entity.GetComponent().Strength; + + emitter << YAML::Key << "Color"; + emitter << YAML::Value << entity.GetComponent().Color; + emitter << YAML::EndMap; + + } + + + emitter << YAML::EndMap; + }); - return std::string(sceneYAML.c_str()); + + emitter << YAML::EndSeq; + emitter << YAML::EndMap; + + return std::string(emitter.c_str()); } - - void Parse(std::string& YAML) { - std::vector nodes = YAML::LoadAll(YAML); - - for(YAML::Node node : nodes) - { - std::cout << node.Type() << std::endl; - } - - } + void SaveScene(std::filesystem::path path, Scene& scene) { std::string YAMLString = Serialize(scene); @@ -93,17 +95,45 @@ void Serialize(YAML::Emitter& emitter, LightComponent& light) void LoadScene(std::filesystem::path path, Scene& scene) { - std::ifstream sceneFile; - std::string YAMLScene; - sceneFile.open(path.u8string()); + auto sceneYAML = YAML::LoadFile(path.u8string()); - std::stringstream sstream; - sstream << sceneFile.rdbuf(); - YAMLScene = sstream.str(); - - sceneFile.close(); + if (!sceneYAML["Scene"]) { + spdlog::error("Not a scene file!"); + return; + } + + scene.getReg().clear(); - Parse(YAMLScene); + + std::string SceneName = sceneYAML["Scene"].as(); + + + auto entities = sceneYAML["Entities"]; + + for (const auto& entity : entities) { + + std::string entityID = entity["Ident"].as(); + YoggieEngine::Entity SE = scene.AddEntity(entityID); + if (entity["Transform"]) + { + TransformComponent tc = SE.GetComponent(); + auto positionNode = entity["Transform"]["Position"]; + tc.Position = glm::vec3(positionNode[0].as(), positionNode[1].as(), positionNode[2].as()); + + auto rotationNode = entity["Transform"]["Rotation"]; + tc.Rotation = glm::vec3(rotationNode[0].as(), rotationNode[1].as(), rotationNode[2].as()); + + auto scaleNode = entity["Transform"]["Scale"]; + tc.Scale = glm::vec3(scaleNode[0].as(), scaleNode[1].as(), scaleNode[2].as()); + } + if (entity["Light"]) { + LightComponent lc = SE.AddComponent(); + lc.Strength = entity["Light"]["strength"].as(); + lc.Color = glm::vec3(entity["Light"]["Color"][0].as(), entity["Light"]["Color"][1].as(), entity["Light"]["Color"][2].as()); + } + + } + } \ No newline at end of file