From c7a8f92970dcd44fe8ce3aaca771b008814c1834 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 25 Sep 2022 12:08:28 +0200 Subject: [PATCH] Component: Add ability to read SpriteComponent from JSON --- src/inferno/component/spritecomponent.cpp | 48 +++++++++++++++++++++++ src/inferno/component/spritecomponent.h | 10 +++++ 2 files changed, 58 insertions(+) create mode 100644 src/inferno/component/spritecomponent.cpp diff --git a/src/inferno/component/spritecomponent.cpp b/src/inferno/component/spritecomponent.cpp new file mode 100644 index 0000000..405962e --- /dev/null +++ b/src/inferno/component/spritecomponent.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include "inferno/component/spritecomponent.h" +#include "inferno/render/texture.h" + +namespace Inferno { + +void fromJson(const ruc::Json& json, SpriteComponent& value) +{ + VERIFY(json.type() == ruc::Json::Type::Object); + + if (json.exists("color")) { + json.at("color").getTo(value.color); + } + if (json.exists("texture") && json.at("texture").type() == ruc::Json::Type::String) { + value.texture = TextureManager::the().load(json.at("texture").asString()); + } +} + +} // namespace Inferno + +namespace glm { + +void toJson(ruc::Json& json, const vec4& value) +{ + json = ruc::Json { + { value.r, value.g, value.b, value.a }, + }; +} + +void fromJson(const ruc::Json& json, vec4& value) +{ + VERIFY(json.type() == ruc::Json::Type::Array); + + auto& values = json.asArray(); + VERIFY(values.size() == 4, "glm::vec4 expects 4 values, not {}", values.size()); + + value.r = values.at(0).get(); + value.g = values.at(1).get(); + value.b = values.at(2).get(); + value.a = values.at(3).get(); +} + +} // namespace glm diff --git a/src/inferno/component/spritecomponent.h b/src/inferno/component/spritecomponent.h index 608d8d2..49a333f 100644 --- a/src/inferno/component/spritecomponent.h +++ b/src/inferno/component/spritecomponent.h @@ -9,6 +9,7 @@ #include // std::shared_ptr #include "glm/ext/vector_float4.hpp" // glm::vec4 +#include "ruc/json/json.h" #include "inferno/render/texture.h" @@ -19,4 +20,13 @@ struct SpriteComponent { std::shared_ptr texture; }; +void fromJson(const ruc::Json& json, SpriteComponent& value); + } // namespace Inferno + +namespace glm { + +void toJson(ruc::Json& json, const vec4& value); +void fromJson(const ruc::Json& json, vec4& value); + +} // namespace glm