Skip to content

Commit 73d478e

Browse files
committed
Fixed CircleCollider2DComponent and Texture not saving.
Added CircleCollider2DComponent to SerializeEntityComponent and DeserializeEntryComponent Added `GetPath()` to `Texture` and `OpenGLTexture` Added TextureExample.hazel
1 parent 12b6275 commit 73d478e

4 files changed

Lines changed: 104 additions & 16 deletions

File tree

Hazel/src/Hazel/Renderer/Texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Hazel {
1414
virtual uint32_t GetWidth() const = 0;
1515
virtual uint32_t GetHeight() const = 0;
1616
virtual uint32_t GetRendererID() const = 0;
17+
virtual const std::string & GetPath() const = 0;
1718

1819
virtual void SetData(void* data, uint32_t size) = 0;
1920

Hazel/src/Hazel/Scene/SceneSerializer.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,6 @@ namespace Hazel {
151151
// Serialize components (except IDComponent)
152152
SerializeAllEntityComponents(out,entity);
153153

154-
if (entity.HasComponent<CircleCollider2DComponent>())
155-
{
156-
out << YAML::Key << "CircleCollider2DComponent";
157-
out << YAML::BeginMap; // CircleCollider2DComponent
158-
159-
auto& cc2dComponent = entity.GetComponent<CircleCollider2DComponent>();
160-
out << YAML::Key << "Offset" << YAML::Value << cc2dComponent.Offset;
161-
out << YAML::Key << "Radius" << YAML::Value << cc2dComponent.Radius;
162-
out << YAML::Key << "Density" << YAML::Value << cc2dComponent.Density;
163-
out << YAML::Key << "Friction" << YAML::Value << cc2dComponent.Friction;
164-
out << YAML::Key << "Restitution" << YAML::Value << cc2dComponent.Restitution;
165-
out << YAML::Key << "RestitutionThreshold" << YAML::Value << cc2dComponent.RestitutionThreshold;
166-
167-
out << YAML::EndMap; // CircleCollider2DComponent
168-
}
169-
170154
out << YAML::EndMap; // Entity
171155
}
172156

@@ -362,6 +346,10 @@ namespace Hazel {
362346
out << YAML::BeginMap; // SpriteRendererComponent
363347

364348
auto& spriteRendererComponent = entity.GetComponent<SpriteRendererComponent>();
349+
if (spriteRendererComponent.Texture && std::filesystem::exists(spriteRendererComponent.Texture->GetPath()))
350+
{
351+
out << YAML::Key << "TexturePath" << YAML::Value << spriteRendererComponent.Texture->GetPath();
352+
}
365353
out << YAML::Key << "Color" << YAML::Value << spriteRendererComponent.Color;
366354
out << YAML::Key << "TilingFactor" << YAML::Value << spriteRendererComponent.TilingFactor;
367355

@@ -376,6 +364,8 @@ namespace Hazel {
376364
if (spriteRendererComponent)
377365
{
378366
auto& src = deserializedEntity.AddComponent<SpriteRendererComponent>();
367+
if(spriteRendererComponent["TexturePath"])
368+
src.Texture = Texture2D::Create(spriteRendererComponent["TexturePath"].as<std::string>());
379369
src.Color = spriteRendererComponent["Color"].as<glm::vec4>();
380370
if (spriteRendererComponent["TilingFactor"])
381371
src.TilingFactor = spriteRendererComponent["TilingFactor"].as<float>();
@@ -473,6 +463,42 @@ namespace Hazel {
473463
bc2d.RestitutionThreshold = boxCollider2DComponent["RestitutionThreshold"].as<float>();
474464
}
475465
}
466+
template<>
467+
static void SerializeEntityComponent<CircleCollider2DComponent>(YAML::Emitter& out, Entity entity)
468+
{
469+
470+
if (entity.HasComponent<CircleCollider2DComponent>())
471+
{
472+
out << YAML::Key << "CircleCollider2DComponent";
473+
out << YAML::BeginMap; // CircleCollider2DComponent
474+
475+
auto& cc2dComponent = entity.GetComponent<CircleCollider2DComponent>();
476+
out << YAML::Key << "Offset" << YAML::Value << cc2dComponent.Offset;
477+
out << YAML::Key << "Radius" << YAML::Value << cc2dComponent.Radius;
478+
out << YAML::Key << "Density" << YAML::Value << cc2dComponent.Density;
479+
out << YAML::Key << "Friction" << YAML::Value << cc2dComponent.Friction;
480+
out << YAML::Key << "Restitution" << YAML::Value << cc2dComponent.Restitution;
481+
out << YAML::Key << "RestitutionThreshold" << YAML::Value << cc2dComponent.RestitutionThreshold;
482+
483+
out << YAML::EndMap; // CircleCollider2DComponent
484+
}
485+
}
486+
487+
template<>
488+
static void DeserializeEntryComponent<CircleCollider2DComponent>(YAML::detail::iterator_value& entity, Entity& deserializedEntity)
489+
{
490+
auto circleCollider2DComponent = entity["CircleCollider2DComponent"];
491+
if (circleCollider2DComponent)
492+
{
493+
auto& bc2d = deserializedEntity.AddComponent<CircleCollider2DComponent>();
494+
bc2d.Offset = circleCollider2DComponent["Offset"].as<glm::vec2>();
495+
bc2d.Radius = circleCollider2DComponent["Radius"].as<float>();
496+
bc2d.Density = circleCollider2DComponent["Density"].as<float>();
497+
bc2d.Friction = circleCollider2DComponent["Friction"].as<float>();
498+
bc2d.Restitution = circleCollider2DComponent["Restitution"].as<float>();
499+
bc2d.RestitutionThreshold = circleCollider2DComponent["RestitutionThreshold"].as<float>();
500+
}
501+
}
476502

477503
template<>
478504
static void SerializeEntityComponent<NativeScriptComponent>(YAML::Emitter& out, Entity entity)

Hazel/src/Platform/OpenGL/OpenGLTexture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Hazel {
1616
virtual uint32_t GetWidth() const override { return m_Width; }
1717
virtual uint32_t GetHeight() const override { return m_Height; }
1818
virtual uint32_t GetRendererID() const override { return m_RendererID; }
19+
virtual const std::string& GetPath() const override { return m_Path; }
1920

2021
virtual void SetData(void* data, uint32_t size) override;
2122

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Scene: Untitled
2+
Entities:
3+
- Entity: 6963903425255029957
4+
TagComponent:
5+
Tag: Camera B
6+
TransformComponent:
7+
Translation: [0, 0, 0]
8+
Rotation: [0, 0, 0]
9+
Scale: [1, 1, 1]
10+
CameraComponent:
11+
Camera:
12+
ProjectionType: 1
13+
PerspectiveFOV: 0.785398185
14+
PerspectiveNear: 0.00999999978
15+
PerspectiveFar: 1000
16+
OrthographicSize: 10
17+
OrthographicNear: -1
18+
OrthographicFar: 1
19+
Primary: false
20+
FixedAspectRatio: false
21+
- Entity: 10881116728385524032
22+
TagComponent:
23+
Tag: Camera A
24+
TransformComponent:
25+
Translation: [0, 0, 0]
26+
Rotation: [0, 0, 0]
27+
Scale: [1, 1, 1]
28+
CameraComponent:
29+
Camera:
30+
ProjectionType: 1
31+
PerspectiveFOV: 0.785398185
32+
PerspectiveNear: 0.00999999978
33+
PerspectiveFar: 1000
34+
OrthographicSize: 10
35+
OrthographicNear: -1
36+
OrthographicFar: 1
37+
Primary: true
38+
FixedAspectRatio: false
39+
- Entity: 5955965714161965918
40+
TagComponent:
41+
Tag: Red Square
42+
TransformComponent:
43+
Translation: [0, 1.10000002, 0]
44+
Rotation: [0, 0, 0]
45+
Scale: [1, 1, 1]
46+
SpriteRendererComponent:
47+
TexturePath: assets\textures\ChernoLogo.png
48+
Color: [1, 0.999989986, 0.999989986, 1]
49+
TilingFactor: 1
50+
- Entity: 16837712394659778935
51+
TagComponent:
52+
Tag: Green Square
53+
TransformComponent:
54+
Translation: [2.4000001, 0, 0]
55+
Rotation: [0, 0, 0]
56+
Scale: [1, 1, 1]
57+
SpriteRendererComponent:
58+
TexturePath: assets\textures\Checkerboard.png
59+
Color: [1, 0.999989986, 0.999989986, 1]
60+
TilingFactor: 1

0 commit comments

Comments
 (0)