PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
sprite_renderer.h
1#pragma once
2
3#include "pixelbullet/filesystem/virtual_path.h"
4#include "pixelbullet/filesystem/virtual_path_serialization.h"
5#include "pixelbullet/serialization/node.h"
6
7#include <glm/glm.hpp>
8
9namespace pixelbullet
10{
12{
13 VirtualPath texture;
14 glm::vec4 tint = glm::vec4(1.0f);
15 glm::vec2 uv_scale = glm::vec2(1.0f);
16 glm::vec2 uv_offset = glm::vec2(0.0f);
17 bool visible = true;
18};
19
20inline Node& operator<<(Node& node, const SpriteRenderer& sprite)
21{
22 node["texture"] << sprite.texture;
23
24 Node tintNode;
25 tintNode.SetType(NodeType::Array);
26 auto& tintProperties = tintNode.GetProperties();
27 tintProperties.clear();
28 for (int index = 0; index < 4; ++index)
29 {
30 Node componentNode;
31 componentNode << sprite.tint[index];
32 tintProperties.emplace_back("", std::move(componentNode));
33 }
34 node["tint"] = std::move(tintNode);
35
36 Node uvScaleNode;
37 uvScaleNode.SetType(NodeType::Array);
38 auto& uvScaleProperties = uvScaleNode.GetProperties();
39 uvScaleProperties.clear();
40 for (int index = 0; index < 2; ++index)
41 {
42 Node componentNode;
43 componentNode << sprite.uv_scale[index];
44 uvScaleProperties.emplace_back("", std::move(componentNode));
45 }
46 node["uvScale"] = std::move(uvScaleNode);
47
48 Node uvOffsetNode;
49 uvOffsetNode.SetType(NodeType::Array);
50 auto& uvOffsetProperties = uvOffsetNode.GetProperties();
51 uvOffsetProperties.clear();
52 for (int index = 0; index < 2; ++index)
53 {
54 Node componentNode;
55 componentNode << sprite.uv_offset[index];
56 uvOffsetProperties.emplace_back("", std::move(componentNode));
57 }
58 node["uvOffset"] = std::move(uvOffsetNode);
59
60 node["visible"] << sprite.visible;
61 return node;
62}
63
64inline const Node& operator>>(const Node& node, SpriteRenderer& sprite)
65{
66 node["texture"] >> sprite.texture;
67
68 const Node& tintNode = node["tint"];
69 ASSERT(tintNode.GetType() == NodeType::Array, "SpriteRenderer tint must be an array");
70 const auto& tintProperties = tintNode.GetProperties();
71 ASSERT(tintProperties.size() >= 4, "SpriteRenderer tint requires four components");
72 for (int index = 0; index < 4; ++index)
73 {
74 tintProperties[index].second >> sprite.tint[index];
75 }
76
77 const Node& uvScaleNode = node["uvScale"];
78 ASSERT(uvScaleNode.GetType() == NodeType::Array, "SpriteRenderer uvScale must be an array");
79 const auto& uvScaleProperties = uvScaleNode.GetProperties();
80 ASSERT(uvScaleProperties.size() >= 2, "SpriteRenderer uvScale requires two components");
81 for (int index = 0; index < 2; ++index)
82 {
83 uvScaleProperties[index].second >> sprite.uv_scale[index];
84 }
85
86 const Node& uvOffsetNode = node["uvOffset"];
87 ASSERT(uvOffsetNode.GetType() == NodeType::Array, "SpriteRenderer uvOffset must be an array");
88 const auto& uvOffsetProperties = uvOffsetNode.GetProperties();
89 ASSERT(uvOffsetProperties.size() >= 2, "SpriteRenderer uvOffset requires two components");
90 for (int index = 0; index < 2; ++index)
91 {
92 uvOffsetProperties[index].second >> sprite.uv_offset[index];
93 }
94
95 node["visible"] >> sprite.visible;
96 return node;
97}
98} // namespace pixelbullet
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:163
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition virtual_path.h:10
Definition sprite_renderer.h:12