Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
SpriteComponent.cpp
Go to the documentation of this file.
2#include "graphics/Texture.h"
3#include "render/RenderQueue.h"
4#include "scene/GameObject.h"
5#include "Engine.h"
6
7#include <string>
8
9namespace eng
10{
11 void SpriteComponent::LoadProperties(const nlohmann::json& json)
12 {
13 // Texture
14 const std::string texturePath = json.value("texture", "");
15 if (auto texture = Texture::Load(texturePath))
16 {
17 SetTexture(texture);
18 }
19
20 // Color
21 if (json.contains("color"))
22 {
23 auto& colorObj = json["color"];
24 glm::vec4 color(
25 colorObj.value("r", 1.0f),
26 colorObj.value("g", 1.0f),
27 colorObj.value("b", 1.0f),
28 colorObj.value("a", 1.0f)
29 );
30 SetColor(color);
31 }
32
33 // Size
34 if (json.contains("size"))
35 {
36 auto& sizeObj = json["size"];
37 glm::vec2 size(
38 sizeObj.value("x", 100.0f),
39 sizeObj.value("y", 100.0f)
40 );
41 SetSize(size);
42 }
43
44 // Lower Left UV
45 if (json.contains("lowerLeftUV"))
46 {
47 auto& uvObj = json["lowerLeftUV"];
48 glm::vec2 uv(
49 uvObj.value("u", 0.0f),
50 uvObj.value("v", 0.0f)
51 );
53 }
54
55 // Upper Right UV
56 if (json.contains("upperRightUV"))
57 {
58 auto& uvObj = json["upperRightUV"];
59 glm::vec2 uv(
60 uvObj.value("u", 1.0f),
61 uvObj.value("v", 1.0f)
62 );
64 }
65
66 // Pivot
67 if (json.contains("pivot"))
68 {
69 auto& pivotObj = json["pivot"];
70 glm::vec2 pivot(
71 pivotObj.value("x", 0.5f),
72 pivotObj.value("y", 0.5f)
73 );
74 SetPivot(pivot);
75 }
76 }
77
78 void SpriteComponent::Update(float deltaTime)
79 {
80 if (!m_texture || !m_visible)
81 {
82 return;
83 }
84
85 RenderCommand2D command;
87 command.texture = m_texture.get();
88 command.color = m_color;
89 command.size = m_size;
90 command.lowerLeftUV = m_lowerLeftUV;
91 command.upperRightUV = m_upperRightUV;
92 command.pivot = m_pivot;
93
94 auto& renderQueue = Engine::GetInstance().GetRenderQueue();
95 renderQueue.Submit(command);
96 }
97
98 void SpriteComponent::SetTexture(const std::shared_ptr<Texture>& texture)
99 {
100 m_texture = texture;
101 }
102
103 const std::shared_ptr<Texture>& SpriteComponent::GetTexture() const
104 {
105 return m_texture;
106 }
107
108 void SpriteComponent::SetColor(const glm::vec4& color)
109 {
110 m_color = color;
111 }
112
113 const glm::vec4& SpriteComponent::GetColor() const
114 {
115 return m_color;
116 }
117
118 void SpriteComponent::SetSize(const glm::vec2& size)
119 {
120 m_size = size;
121 }
122
123 const glm::vec2& SpriteComponent::GetSize() const
124 {
125 return m_size;
126 }
127
128 void SpriteComponent::SetLowerLeftUV(const glm::vec2& uv)
129 {
130 m_lowerLeftUV = uv;
131 }
132
133 const glm::vec2& SpriteComponent::GetLowerLeftUV() const
134 {
135 return m_lowerLeftUV;
136 }
137
138 void SpriteComponent::SetUpperRightUV(const glm::vec2& uv)
139 {
140 m_upperRightUV = uv;
141 }
142
143 const glm::vec2& SpriteComponent::GetUpperRightUV() const
144 {
145 return m_upperRightUV;
146 }
147
148 void SpriteComponent::SetUV(const glm::vec2& lowerLeftUV, const glm::vec2& upperRightUV)
149 {
150 m_lowerLeftUV = lowerLeftUV;
151 m_upperRightUV = upperRightUV;
152 }
153
154 void SpriteComponent::SetPivot(const glm::vec2& pivot)
155 {
156 m_pivot = pivot;
157 }
158
159 const glm::vec2& SpriteComponent::GetPivot() const
160 {
161 return m_pivot;
162 }
163
165 {
166 m_visible = visible;
167 }
168
170 {
171 return m_visible;
172 }
173}
GameObject * GetOwner()
Definition Component.cpp:15
static Engine & GetInstance()
Definition Engine.cpp:50
RenderQueue & GetRenderQueue()
Definition Engine.cpp:200
glm::mat4 GetWorldTransform2D() const
void Submit(const RenderCommand &command)
void SetPivot(const glm::vec2 &pivot)
const std::shared_ptr< Texture > & GetTexture() const
void SetLowerLeftUV(const glm::vec2 &uv)
void SetUV(const glm::vec2 &lowerLeftUV, const glm::vec2 &upperRightUV)
void SetTexture(const std::shared_ptr< Texture > &texture)
void Update(float deltaTime) override
void LoadProperties(const nlohmann::json &json) override
const glm::vec2 & GetPivot() const
void SetUpperRightUV(const glm::vec2 &uv)
const glm::vec4 & GetColor() const
const glm::vec2 & GetLowerLeftUV() const
const glm::vec2 & GetSize() const
void SetVisibile(bool visible)
void SetColor(const glm::vec4 &color)
const glm::vec2 & GetUpperRightUV() const
void SetSize(const glm::vec2 &size)
static std::shared_ptr< Texture > Load(const std::string &path)
Definition Texture.cpp:53