add SpriteRendererComponent's 'TexturePath' and 'TilingFactor' to serialize
This commit is contained in:
@ -220,6 +220,9 @@ namespace Hazel
|
|||||||
out << YAML::BeginMap; // SpriteRendererComponent
|
out << YAML::BeginMap; // SpriteRendererComponent
|
||||||
auto& spriteRendererComponent = entity.GetComponent<SpriteRendererComponent>();
|
auto& spriteRendererComponent = entity.GetComponent<SpriteRendererComponent>();
|
||||||
out << YAML::Key << "Color" << YAML::Value << spriteRendererComponent.Color;
|
out << YAML::Key << "Color" << YAML::Value << spriteRendererComponent.Color;
|
||||||
|
if (spriteRendererComponent.Texture)
|
||||||
|
out << YAML::Key << "TexturePath" << YAML::Value << spriteRendererComponent.Texture->GetPath();
|
||||||
|
out << YAML::Key << "TilingFactor" << YAML::Value << spriteRendererComponent.TilingFactor;
|
||||||
out << YAML::EndMap; // SpriteRendererComponent
|
out << YAML::EndMap; // SpriteRendererComponent
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,20 +298,47 @@ namespace Hazel
|
|||||||
bool SceneSerializer::Deserialize(const std::string& filepath)
|
bool SceneSerializer::Deserialize(const std::string& filepath)
|
||||||
{
|
{
|
||||||
const std::ifstream stream(filepath);
|
const std::ifstream stream(filepath);
|
||||||
|
|
||||||
|
if (!stream.is_open())
|
||||||
|
{
|
||||||
|
HZ_CORE_ERROR("Could not open file {0}", filepath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
std::stringstream strStream;
|
std::stringstream strStream;
|
||||||
strStream << stream.rdbuf();
|
strStream << stream.rdbuf();
|
||||||
|
|
||||||
YAML::Node data = YAML::Load(strStream.str());
|
if (strStream.str().empty())
|
||||||
|
{
|
||||||
|
HZ_CORE_ERROR("Empty scene file: {0}", filepath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
YAML::Node data;
|
||||||
|
try {
|
||||||
|
data = YAML::Load(strStream.str());
|
||||||
|
}
|
||||||
|
catch (const YAML::Exception& e) {
|
||||||
|
HZ_CORE_ERROR("YAML parsing failed: {0}\nFile: {1}", e.what(), filepath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.IsNull() || !data.IsDefined() || !data.IsMap())
|
||||||
|
{
|
||||||
|
HZ_CORE_ERROR("YAML parsing failed: {0}", filepath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!data["Scene"])
|
if (!data["Scene"])
|
||||||
{
|
{
|
||||||
HZ_CORE_WARN("cannot find key: Scene");
|
HZ_CORE_ERROR("cannot find key: Scene");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
auto sceneName = data["Scene"].as<std::string>();
|
auto sceneName = data["Scene"].as<std::string>();
|
||||||
|
|
||||||
HZ_CORE_TRACE("Deserializing Scene {}", strStream.str());
|
HZ_CORE_DEBUG("Deserializing Scene {}", strStream.str());
|
||||||
|
|
||||||
auto entities = data["Entities"];
|
auto entities = data["Entities"];
|
||||||
if (entities)
|
if (entities)
|
||||||
@ -360,6 +390,9 @@ namespace Hazel
|
|||||||
{
|
{
|
||||||
auto& sprite = deserializedEntity.AddComponent<SpriteRendererComponent>();
|
auto& sprite = deserializedEntity.AddComponent<SpriteRendererComponent>();
|
||||||
sprite.Color = spriteRendererComponent["Color"].as<glm::vec4>();
|
sprite.Color = spriteRendererComponent["Color"].as<glm::vec4>();
|
||||||
|
sprite.TilingFactor = spriteRendererComponent["TilingFactor"].as<float>();
|
||||||
|
if (spriteRendererComponent["TexturePath"])
|
||||||
|
sprite.Texture = Texture2D::Create(spriteRendererComponent["TexturePath"].as<std::string>());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto circleRendererComponent = entity["CircleRendererComponent"];
|
auto circleRendererComponent = entity["CircleRendererComponent"];
|
||||||
|
|||||||
Reference in New Issue
Block a user