Skip to content

Commit c4cde9b

Browse files
committed
Add JSON serialization
1 parent f4dbe61 commit c4cde9b

12 files changed

Lines changed: 238 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
fail-fast: false
1313

1414
matrix:
15-
os: [macos-latest, ubuntu-latest, windows-latest]
16-
build_type: [Release, Debug]
17-
c_compiler: [clang]
18-
cpp_compiler: [clang++]
15+
os: [ macos-latest, ubuntu-latest, windows-latest ]
16+
build_type: [ Release, Debug ]
17+
c_compiler: [ clang ]
18+
cpp_compiler: [ clang++ ]
1919

2020
steps:
2121
- uses: actions/checkout@v4

Core/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ CPMAddPackage(
6464
"UUID_BUILD_TESTS OFF"
6565
)
6666

67+
CPMAddPackage(
68+
URI "gh:USCiLab/cereal#v1.3.2"
69+
OPTIONS
70+
"BUILD_DOC OFF"
71+
"BUILD_TESTS OFF"
72+
"BUILD_SANDBOX OFF"
73+
"SKIP_PERFORMANCE_COMPARISON ON"
74+
)
75+
6776
add_library(stbi STATIC vendor/stbi/stbi.h vendor/stbi/stbi.c)
6877

6978
CPMAddPackage(
@@ -179,6 +188,9 @@ set(PROJECT_SOURCES
179188
src/Uuid/Uuid.cpp
180189
src/Files/File.hpp
181190
src/Files/File.cpp
191+
src/Entities/Components/Graphics/Model.hpp
192+
src/Serialization/JsonArchive.hpp
193+
src/Serialization/Serialization.hpp
182194
)
183195

184196
add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCES})
@@ -195,6 +207,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
195207
assimp::assimp
196208
EnTT::EnTT
197209
stduuid
210+
cereal
198211
stbi
199212
imgui
200213
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef PIXF_MODEL_HPP
2+
#define PIXF_MODEL_HPP
3+
4+
#include "Serialization/Serialization.hpp"
5+
#include "Uuid/Uuid.hpp"
6+
7+
namespace Pixf::Core::Entities::Graphics {
8+
struct Model {
9+
Uuid::Uuid uuid;
10+
};
11+
12+
template<typename Archive>
13+
void Serialize(Archive &archive, Model &model) {
14+
Serialization::SerializeNvp(archive, "uuid", model.uuid);
15+
}
16+
17+
PIXF_SERIALIZE(Model)
18+
} // namespace Pixf::Core::Entities::Graphics
19+
20+
#endif // PIXF_MODEL_HPP

Core/src/Entities/Components/RigidTransform.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Math/Matrix.hpp"
55
#include "Math/Quaternion.hpp"
66
#include "Math/Vector.hpp"
7+
#include "Serialization/Serialization.hpp"
78

89
namespace Pixf::Core::Entities::Components {
910
struct RigidTransform {
@@ -24,6 +25,14 @@ namespace Pixf::Core::Entities::Components {
2425
return Math::Matrix4f::Rotate(rotation) * Math::Matrix4f::Translate(position);
2526
}
2627
};
28+
29+
template<class Archive>
30+
void Serialize(Archive &archive, RigidTransform &transform) {
31+
Serialization::SerializeNvp(archive, "position", transform.position);
32+
Serialization::SerializeNvp(archive, "rotation", transform.rotation);
33+
}
34+
35+
PIXF_SERIALIZE(RigidTransform)
2736
} // namespace Pixf::Core::Entities::Components
2837

2938
#endif // PIXF_RIGIDTRANSFORM_HPP

Core/src/Entities/Components/Transform.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Math/Matrix.hpp"
55
#include "Math/Quaternion.hpp"
66
#include "Math/Vector.hpp"
7+
#include "Serialization/Serialization.hpp"
78

89
namespace Pixf::Core::Entities::Components {
910
struct Transform {
@@ -28,6 +29,15 @@ namespace Pixf::Core::Entities::Components {
2829
Math::Matrix4f::Translate(position);
2930
}
3031
};
32+
33+
template<class Archive>
34+
void Serialize(Archive &archive, Transform &transform) {
35+
Serialization::SerializeNvp(archive, "position", transform.position);
36+
Serialization::SerializeNvp(archive, "rotation", transform.rotation);
37+
Serialization::SerializeNvp(archive, "scale", transform.scale);
38+
}
39+
40+
PIXF_SERIALIZE(Transform)
3141
} // namespace Pixf::Core::Entities::Components
3242

3343
#endif // PIXF_TRANSFORM_HPP

Core/src/Math/Math.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ namespace Pixf::Core::Math {
55

66
double RadiansToDegrees(const double angleRadians) { return angleRadians * 180.0 / s_Pi; }
77
double DegreesToRadians(const double angleDegrees) { return angleDegrees * s_Pi / 180.0; }
8+
double GetPi() {
9+
return s_Pi;
10+
}
811
} // namespace Pixf::Core::Math

Core/src/Math/Math.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Pixf::Core::Math {
3030

3131
PIXF_API double RadiansToDegrees(double angleRadians);
3232
PIXF_API double DegreesToRadians(double angleDegrees);
33+
PIXF_API double GetPi();
3334
} // namespace Pixf::Core::Math
3435

3536
#endif // PIXF_MATH_HPP

Core/src/Math/Quaternion.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "Common.hpp"
77
#include "Vector.hpp"
8+
#include "Math.hpp"
89

910
namespace Pixf::Core::Math {
1011
template<typename T>
@@ -114,6 +115,30 @@ namespace Pixf::Core::Math {
114115
q1.w * w1 + qz.w * w2);
115116
}
116117

118+
Vector3f EulerAngles() const {
119+
Vector3f angles;
120+
const float sp = 2.0F * (w * y - z * x);
121+
122+
if (constexpr float epsilon = 0.9999F; std::abs(sp) >= epsilon) {
123+
angles.y = (sp > 0) ? (GetPi() / 2.0F) : (-GetPi() / 2.0F);
124+
125+
angles.x = -2.0F * std::atan2(x, w); // or 2.0F * atan2(x, w)
126+
angles.z = 0.0F;
127+
128+
} else {
129+
angles.y = std::asin(sp);
130+
131+
const float srcp = 2.0F * (w * z + x * y);
132+
const float crcp = 1.0F - 2.0F * (y * y + z * z);
133+
angles.z = std::atan2(srcp, crcp);
134+
const float sycp = 2.0F * (w * x + y * z);
135+
const float cycp = 1.0F - 2.0F * (x * x + y * y);
136+
angles.x = std::atan2(sycp, cycp);
137+
}
138+
139+
return angles;
140+
}
141+
117142
Matrix4f ToMatrix() const;
118143
};
119144
} // namespace Pixf::Core::Math
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef PIXF_JSONARCHIVE_HPP
2+
#define PIXF_JSONARCHIVE_HPP
3+
4+
#include <cereal/archives/json.hpp>
5+
6+
namespace Pixf::Core::Serialization {
7+
class JsonOutputArchive final : cereal::JSONOutputArchive {
8+
public:
9+
using JSONOutputArchive::JSONOutputArchive;
10+
11+
template<typename T>
12+
void Save(const std::string &name, T &value) {
13+
(*this)(cereal::make_nvp(name.c_str(), value));
14+
}
15+
};
16+
17+
class JsonInputArchive final : cereal::JSONInputArchive {
18+
public:
19+
using JSONInputArchive::JSONInputArchive;
20+
21+
template<typename T>
22+
void Load(const std::string &name, T &value) {
23+
(*this)(cereal::make_nvp(name.c_str(), value));
24+
}
25+
};
26+
} // namespace Pixf::Core::Serialization
27+
28+
#endif // PIXF_JSONARCHIVE_HPP
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#ifndef PIXF_SERIALIZATION_HPP
2+
#define PIXF_SERIALIZATION_HPP
3+
4+
#include <cereal/cereal.hpp>
5+
6+
#define PIXF_SERIALIZE(Type) \
7+
template<typename Archive> \
8+
void serialize(Archive &archive, Type &obj) { \
9+
Serialize(archive, obj); \
10+
}
11+
12+
namespace Pixf::Core::Serialization {
13+
template<typename Archive, typename T>
14+
void SerializeNvp(Archive &archive, const std::string &name, T &obj) {
15+
archive(cereal::make_nvp(name, obj));
16+
}
17+
18+
template<typename Archive, typename T>
19+
void Serialize(Archive &archive, T &obj) {
20+
archive(obj);
21+
}
22+
} // namespace Pixf::Core::Serialization
23+
24+
namespace Pixf::Core::Math {
25+
template<typename Archive, typename T>
26+
void Serialize(Archive &archive, Vector2<T> &vector) {
27+
Serialization::SerializeNvp(archive, "x", vector.x);
28+
Serialization::SerializeNvp(archive, "y", vector.y);
29+
}
30+
31+
template<typename Archive, typename T>
32+
void Serialize(Archive &archive, Vector3<T> &vector) {
33+
Serialization::SerializeNvp(archive, "x", vector.x);
34+
Serialization::SerializeNvp(archive, "y", vector.y);
35+
Serialization::SerializeNvp(archive, "z", vector.z);
36+
}
37+
38+
template<typename Archive, typename T>
39+
void Serialize(Archive &archive, Vector4<T> &vector) {
40+
Serialization::SerializeNvp(archive, "x", vector.x);
41+
Serialization::SerializeNvp(archive, "y", vector.y);
42+
Serialization::SerializeNvp(archive, "z", vector.z);
43+
Serialization::SerializeNvp(archive, "w", vector.w);
44+
}
45+
46+
template<typename Archive, typename T>
47+
void Serialize(Archive &archive, Color3<T> &color) {
48+
Serialization::SerializeNvp(archive, "r", color.r);
49+
Serialization::SerializeNvp(archive, "g", color.g);
50+
Serialization::SerializeNvp(archive, "b", color.b);
51+
}
52+
53+
template<typename Archive, typename T>
54+
void Serialize(Archive &archive, Color4<T> &color) {
55+
Serialization::SerializeNvp(archive, "r", color.r);
56+
Serialization::SerializeNvp(archive, "g", color.g);
57+
Serialization::SerializeNvp(archive, "b", color.b);
58+
Serialization::SerializeNvp(archive, "a", color.a);
59+
}
60+
61+
template<typename Archive>
62+
void Serialize(Archive &archive, Quaternion &quat) {
63+
Serialization::SerializeNvp(archive, "x", quat.x);
64+
Serialization::SerializeNvp(archive, "y", quat.y);
65+
Serialization::SerializeNvp(archive, "z", quat.z);
66+
Serialization::SerializeNvp(archive, "w", quat.w);
67+
}
68+
69+
PIXF_SERIALIZE(Vector2f)
70+
PIXF_SERIALIZE(Vector3f)
71+
PIXF_SERIALIZE(Vector4f)
72+
73+
PIXF_SERIALIZE(Vector2d)
74+
PIXF_SERIALIZE(Vector3d)
75+
PIXF_SERIALIZE(Vector4d)
76+
77+
PIXF_SERIALIZE(Vector2i)
78+
PIXF_SERIALIZE(Vector3i)
79+
PIXF_SERIALIZE(Vector4i)
80+
81+
PIXF_SERIALIZE(Vector2u)
82+
PIXF_SERIALIZE(Vector3u)
83+
PIXF_SERIALIZE(Vector4u)
84+
85+
PIXF_SERIALIZE(Color3u8)
86+
PIXF_SERIALIZE(Color4u8)
87+
88+
PIXF_SERIALIZE(Color3u16)
89+
PIXF_SERIALIZE(Color4u16)
90+
91+
PIXF_SERIALIZE(Quaternion)
92+
} // namespace Pixf::Core::Math
93+
94+
namespace Pixf::Core::Uuid {
95+
template<typename Archive>
96+
void Serialize(Archive &archive, Uuid &uuid) {
97+
std::string str = uuid.ToString();
98+
Serialization::SerializeNvp(archive, "value", str);
99+
uuid = Uuid::FromString(str.c_str()).value();
100+
}
101+
102+
PIXF_SERIALIZE(Uuid)
103+
} // namespace Pixf::Core::Uuid
104+
#endif // PIXF_SERIALIZATION_HPP

0 commit comments

Comments
 (0)