diff --git a/src/inferno/component/cubemap-component.cpp b/src/inferno/component/cubemap-component.cpp index 433ef79..4ee1ed5 100644 --- a/src/inferno/component/cubemap-component.cpp +++ b/src/inferno/component/cubemap-component.cpp @@ -8,7 +8,7 @@ #include "inferno/asset/texture.h" #include "inferno/component/cubemap-component.h" -#include "inferno/component/spritecomponent.h" // TODO: Move glm::x toJson/fromJson to separate file +#include "inferno/component/serialize.h" // not detected as used by clang-tidy namespace Inferno { diff --git a/src/inferno/component/model-component.cpp b/src/inferno/component/model-component.cpp index bb6b44f..1390268 100644 --- a/src/inferno/component/model-component.cpp +++ b/src/inferno/component/model-component.cpp @@ -8,7 +8,7 @@ #include "inferno/asset/asset-manager.h" #include "inferno/asset/model.h" #include "inferno/asset/texture.h" -#include "inferno/component/spritecomponent.h" // TODO: Move glm::x toJson/fromJson to separate file +#include "inferno/component/serialize.h" // not detected as used by clang-tidy namespace Inferno { diff --git a/src/inferno/component/serialize.cpp b/src/inferno/component/serialize.cpp new file mode 100644 index 0000000..97c5e14 --- /dev/null +++ b/src/inferno/component/serialize.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include "glm/ext/matrix_float2x2.hpp" +#include "glm/ext/matrix_float3x3.hpp" +#include "glm/ext/matrix_float4x4.hpp" +#include "glm/ext/vector_float2.hpp" +#include "glm/ext/vector_float3.hpp" +#include "glm/ext/vector_float4.hpp" +#include "ruc/json/json.h" + +#include "inferno/component/serialize.h" + +namespace glm { + +void toJson(ruc::Json& json, const vec2& value) +{ + json = ruc::Json { + { value.x, value.y }, + }; +} + +void fromJson(const ruc::Json& json, vec2& value) +{ + VERIFY(json.type() == ruc::Json::Type::Array); + + auto& values = json.asArray(); + VERIFY(values.size() == 2, "glm::vec2 expected 2 values, got: {}", values.size()); + + value.x = values.at(0).get(); + value.y = values.at(1).get(); +} + +// ----------------------------------------- + +void toJson(ruc::Json& json, const vec3& value) +{ + json = ruc::Json { + { value.x, value.y, value.z }, + }; +} + +void fromJson(const ruc::Json& json, vec3& value) +{ + VERIFY(json.type() == ruc::Json::Type::Array); + + auto& values = json.asArray(); + VERIFY(values.size() == 3, "glm::vec3 expected 3 values, got: {}", values.size()); + + value.x = values.at(0).get(); + value.y = values.at(1).get(); + value.z = values.at(2).get(); +} + +// ----------------------------------------- + +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 expected 4 values, got: {}", 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 + +// ----------------------------------------- + +void ruc::format::Formatter::format(Builder& builder, glm::vec2 value) const +{ + return Formatter>::format(builder, { value.x, value.y }); +} + +void ruc::format::Formatter::format(Builder& builder, glm::vec3 value) const +{ + return Formatter>::format(builder, { value.x, value.y, value.z }); +} + +void ruc::format::Formatter::format(Builder& builder, glm::vec4 value) const +{ + return Formatter>::format(builder, { value.x, value.y, value.z, value.w }); +} + +void ruc::format::Formatter::format(Builder& builder, glm::mat2 value) const +{ + builder.putString("mat2 "); + Formatter::format(builder, value[0]); + builder.putString("\n "); + return Formatter::format(builder, value[1]); +} + +void ruc::format::Formatter::format(Builder& builder, glm::mat3 value) const +{ + builder.putString("mat3 "); + Formatter::format(builder, value[0]); + builder.putString("\n "); + Formatter::format(builder, value[1]); + builder.putString("\n "); + return Formatter::format(builder, value[2]); +} + +void ruc::format::Formatter::format(Builder& builder, glm::mat4 value) const +{ + builder.putString("mat4 "); + Formatter::format(builder, value[0]); + builder.putString("\n "); + Formatter::format(builder, value[1]); + builder.putString("\n "); + Formatter::format(builder, value[2]); + builder.putString("\n "); + return Formatter::format(builder, value[3]); +} diff --git a/src/inferno/component/serialize.h b/src/inferno/component/serialize.h new file mode 100644 index 0000000..a30aad7 --- /dev/null +++ b/src/inferno/component/serialize.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2024 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include "glm/ext/matrix_float2x2.hpp" +#include "glm/ext/matrix_float3x3.hpp" +#include "glm/ext/matrix_float4x4.hpp" +#include "glm/ext/vector_float2.hpp" +#include "glm/ext/vector_float3.hpp" +#include "glm/ext/vector_float4.hpp" +#include "ruc/json/json.h" + +namespace glm { + +void toJson(ruc::Json& json, const vec2& value); +void fromJson(const ruc::Json& json, vec2& value); + +void toJson(ruc::Json& json, const vec3& value); +void fromJson(const ruc::Json& json, vec3& value); + +void toJson(ruc::Json& json, const vec4& value); +void fromJson(const ruc::Json& json, vec4& value); + +} // namespace glm + +template<> +struct ruc::format::Formatter : Formatter> { + void format(Builder& builder, glm::vec2 value) const; +}; + +template<> +struct ruc::format::Formatter : Formatter> { + void format(Builder& builder, glm::vec3 value) const; +}; + +template<> +struct ruc::format::Formatter : Formatter> { + void format(Builder& builder, glm::vec4 value) const; +}; + +template<> +struct ruc::format::Formatter : Formatter { + void format(Builder& builder, glm::mat2 value) const; +}; + +template<> +struct ruc::format::Formatter : Formatter { + void format(Builder& builder, glm::mat3 value) const; +}; + +template<> +struct ruc::format::Formatter : Formatter { + void format(Builder& builder, glm::mat4 value) const; +}; diff --git a/src/inferno/component/spritecomponent.cpp b/src/inferno/component/spritecomponent.cpp index a97a02d..eba0ba1 100644 --- a/src/inferno/component/spritecomponent.cpp +++ b/src/inferno/component/spritecomponent.cpp @@ -4,9 +4,12 @@ * SPDX-License-Identifier: MIT */ -#include "inferno/component/spritecomponent.h" +#include "ruc/json/json.h" + #include "inferno/asset/asset-manager.h" #include "inferno/asset/texture.h" +#include "inferno/component/serialize.h" // not detected as used by clang-tidy +#include "inferno/component/spritecomponent.h" namespace Inferno { @@ -23,27 +26,3 @@ void fromJson(const ruc::Json& json, SpriteComponent& value) } } // 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 939f9e5..f025353 100644 --- a/src/inferno/component/spritecomponent.h +++ b/src/inferno/component/spritecomponent.h @@ -23,10 +23,3 @@ struct SpriteComponent { 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 diff --git a/src/inferno/component/transformcomponent.cpp b/src/inferno/component/transformcomponent.cpp index 1f12005..da64c08 100644 --- a/src/inferno/component/transformcomponent.cpp +++ b/src/inferno/component/transformcomponent.cpp @@ -7,6 +7,7 @@ #include "ruc/format/format.h" #include "ruc/json/json.h" +#include "inferno/component/serialize.h" #include "inferno/component/transformcomponent.h" namespace Inferno { @@ -28,56 +29,6 @@ void fromJson(const ruc::Json& json, TransformComponent& value) } // namespace Inferno -namespace glm { - -void toJson(ruc::Json& json, const vec3& value) -{ - json = ruc::Json { - { value.x, value.y, value.z }, - }; -} - -void fromJson(const ruc::Json& json, vec3& value) -{ - VERIFY(json.type() == ruc::Json::Type::Array); - - auto& values = json.asArray(); - VERIFY(values.size() == 3, "glm::vec3 expects 3 values, not {}", values.size()); - - value.x = values.at(0).get(); - value.y = values.at(1).get(); - value.z = values.at(2).get(); -} - -} // namespace glm - -void ruc::format::Formatter::format(Builder& builder, glm::vec2 value) const -{ - return Formatter>::format(builder, { value.x, value.y }); -} - -void ruc::format::Formatter::format(Builder& builder, glm::vec3 value) const -{ - return Formatter>::format(builder, { value.x, value.y, value.z }); -} - -void ruc::format::Formatter::format(Builder& builder, glm::vec4 value) const -{ - return Formatter>::format(builder, { value.x, value.y, value.z, value.w }); -} - -void ruc::format::Formatter::format(Builder& builder, glm::mat4 value) const -{ - builder.putString("mat4 "); - Formatter::format(builder, value[0]); - builder.putString("\n "); - Formatter::format(builder, value[1]); - builder.putString("\n "); - Formatter::format(builder, value[2]); - builder.putString("\n "); - return Formatter::format(builder, value[3]); -} - void ruc::format::Formatter::format(Builder& builder, Inferno::TransformComponent value) const { builder.putString("transform "); diff --git a/src/inferno/component/transformcomponent.h b/src/inferno/component/transformcomponent.h index c57d437..032cf1e 100644 --- a/src/inferno/component/transformcomponent.h +++ b/src/inferno/component/transformcomponent.h @@ -6,9 +6,6 @@ #pragma once -#include // uint32_t -#include - #include "entt/entity/entity.hpp" // entt::null #include "entt/entity/fwd.hpp" // entt::entity #include "glm/ext/matrix_float4x4.hpp" // glm::mat4 @@ -31,33 +28,6 @@ void fromJson(const ruc::Json& json, TransformComponent& value); } // namespace Inferno -namespace glm { - -void toJson(ruc::Json& json, const vec3& value); -void fromJson(const ruc::Json& json, vec3& value); - -} // namespace glm - -template<> -struct ruc::format::Formatter : Formatter> { - void format(Builder& builder, glm::vec2 value) const; -}; - -template<> -struct ruc::format::Formatter : Formatter> { - void format(Builder& builder, glm::vec3 value) const; -}; - -template<> -struct ruc::format::Formatter : Formatter> { - void format(Builder& builder, glm::vec4 value) const; -}; - -template<> -struct ruc::format::Formatter : Formatter { - void format(Builder& builder, glm::mat4 value) const; -}; - template<> struct ruc::format::Formatter : Formatter { void format(Builder& builder, Inferno::TransformComponent value) const;