galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Sprite.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
10#include "galaxy/core/ServiceLocator.hpp"
12
13#include "Sprite.hpp"
14
15namespace galaxy
16{
17 namespace components
18 {
20 : Serializable {}
21 , m_texture {nullptr}
22 //, m_mapped_vbo {nullptr}
23 {
24 }
25
26 Sprite::Sprite(const nlohmann::json& json)
27 : Serializable {}
28 {
29 deserialize(json);
30 }
31
33 : Serializable {}
34 {
35 /*if (this->m_mapped_vbo != nullptr)
36 {
37 glUnmapNamedBuffer(this->m_vao.vbo().id());
38 }*/
39
40 this->m_texture = s.m_texture;
41 this->m_vao = std::move(s.m_vao);
42 this->m_tint = std::move(s.m_tint);
43 this->m_name = std::move(s.m_name);
44 this->m_vertices = std::move(s.m_vertices);
45 // this->m_mapped_vbo = std::move(s.m_mapped_vbo);
46
47 s.m_texture = nullptr;
48 // s.m_mapped_vbo = nullptr;
49 }
50
52 {
53 if (this != &s)
54 {
55 /*if (this->m_mapped_vbo != nullptr)
56 {
57 glUnmapNamedBuffer(this->m_vao.vbo().id());
58 }*/
59
60 this->m_texture = s.m_texture;
61 this->m_vao = std::move(s.m_vao);
62 this->m_tint = std::move(s.m_tint);
63 this->m_name = std::move(s.m_name);
64 this->m_vertices = std::move(s.m_vertices);
65 // this->m_mapped_vbo = std::move(s.m_mapped_vbo);
66
67 s.m_texture = nullptr;
68 // s.m_mapped_vbo = nullptr;
69 }
70
71 return *this;
72 }
73
75 {
76 /*if (m_mapped_vbo != nullptr)
77 {
78 glUnmapNamedBuffer(m_vao.vbo().id());
79 }*/
80 }
81
82 void Sprite::set_texture(const std::string& texture)
83 {
84 auto& cache = core::ServiceLocator<resource::Textures>::ref();
85 auto tex = cache.get(texture);
86
87 if (tex)
88 {
89 /*if (m_mapped_vbo != nullptr)
90 {
91 glUnmapNamedBuffer(m_vao.vbo().id());
92 }*/
93
94 m_name = texture;
95 m_texture = tex;
96
97 m_vertices = graphics::gen_quad_vertices(m_texture->width(), m_texture->height());
98 auto indices = graphics::gen_default_indices();
99
100 m_vao.buffer(m_vertices, indices);
101 /*m_mapped_vbo = glMapNamedBufferRange(m_vao.vbo().id(),
102 0,
103 m_vertices.size() * sizeof(graphics::Vertex),
104 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_INVALIDATE_RANGE_BIT);*/
105 }
106 else
107 {
108 GALAXY_LOG(GALAXY_ERROR, "Failed to query texture storage for '{0}'.", texture);
109 }
110 }
111
112 void Sprite::set_texture(const std::string& texture, const math::fRect& rect)
113 {
114 auto& cache = core::ServiceLocator<resource::Textures>::ref();
115 auto tex = cache.get(texture);
116
117 if (tex)
118 {
119 /*if (m_mapped_vbo != nullptr)
120 {
121 glUnmapNamedBuffer(m_vao.vbo().id());
122 }*/
123
124 m_name = texture;
125 m_texture = tex;
126
127 m_vertices[0].m_pos.x = 0.0f;
128 m_vertices[0].m_pos.y = 0.0f;
129 m_vertices[0].m_texels.x = graphics::map_x_texel(rect.x, m_texture->width());
130 m_vertices[0].m_texels.y = graphics::map_y_texel(rect.y, m_texture->height());
131
132 m_vertices[1].m_pos.x = m_texture->width();
133 m_vertices[1].m_pos.y = 0.0f;
134 m_vertices[1].m_texels.x = graphics::map_x_texel(rect.x + rect.width, m_texture->width());
135 m_vertices[1].m_texels.y = graphics::map_y_texel(rect.y, m_texture->height());
136
137 m_vertices[2].m_pos.x = m_texture->width();
138 m_vertices[2].m_pos.y = m_texture->height();
139 m_vertices[2].m_texels.x = graphics::map_x_texel(rect.x + rect.width, m_texture->width());
140 m_vertices[2].m_texels.y = graphics::map_y_texel(rect.y + rect.height, m_texture->height());
141
142 m_vertices[3].m_pos.x = 0.0f;
143 m_vertices[3].m_pos.y = m_texture->height();
144 m_vertices[3].m_texels.x = graphics::map_x_texel(rect.x, m_texture->width());
145 m_vertices[3].m_texels.y = graphics::map_y_texel(rect.y + rect.height, m_texture->height());
146
147 auto indices = graphics::gen_default_indices();
148
149 m_vao.buffer(m_vertices, indices);
150 /*m_mapped_vbo = glMapNamedBufferRange(m_vao.vbo().id(),
151 0,
152 m_vertices.size() * sizeof(graphics::Vertex),
153 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_INVALIDATE_RANGE_BIT);*/
154 }
155 else
156 {
157 GALAXY_LOG(GALAXY_ERROR, "Failed to query texture atlas for '{0}'.", texture);
158 }
159 }
160
161 void Sprite::set_clip(const math::fRect& rect)
162 {
163 m_clip = rect;
164
165 // if (m_mapped_vbo)
166 //{
167 m_vertices[0].m_pos.x = 0.0f;
168 m_vertices[0].m_pos.y = 0.0f;
169 m_vertices[0].m_texels.x = graphics::map_x_texel(rect.x, m_texture->width());
170 m_vertices[0].m_texels.y = graphics::map_y_texel(rect.y, m_texture->height());
171
172 m_vertices[1].m_pos.x = m_texture->width();
173 m_vertices[1].m_pos.y = 0.0f;
174 m_vertices[1].m_texels.x = graphics::map_x_texel(rect.x + rect.width, m_texture->width());
175 m_vertices[1].m_texels.y = graphics::map_y_texel(rect.y, m_texture->height());
176
177 m_vertices[2].m_pos.x = m_texture->width();
178 m_vertices[2].m_pos.y = m_texture->height();
179 m_vertices[2].m_texels.x = graphics::map_x_texel(rect.x + rect.width, m_texture->width());
180 m_vertices[2].m_texels.y = graphics::map_y_texel(rect.y + rect.height, m_texture->height());
181
182 m_vertices[3].m_pos.x = 0.0f;
183 m_vertices[3].m_pos.y = m_texture->height();
184 m_vertices[3].m_texels.x = graphics::map_x_texel(rect.x, m_texture->width());
185 m_vertices[3].m_texels.y = graphics::map_y_texel(rect.y + rect.height, m_texture->height());
186
187 m_vao.sub_buffer(0, m_vertices);
188 // std::memcpy(m_mapped_vbo, m_vertices.data(), m_vertices.size() * sizeof(graphics::Vertex));
189 // glFlushMappedNamedBufferRange(m_vao.vbo().id(), 0, m_vertices.size() * sizeof(graphics::Vertex));
190 //}
191 }
192
193 const math::fRect& Sprite::get_clip() const
194 {
195 return m_clip;
196 }
197
198 graphics::Texture2D* Sprite::get_texture()
199 {
200 return m_texture;
201 }
202
203 const std::string& Sprite::get_texture_name() const
204 {
205 return m_name;
206 }
207
208 nlohmann::json Sprite::serialize()
209 {
210 nlohmann::json json = "{}"_json;
211 json["texture"] = m_name;
212 json["tint"]["r"] = m_tint.r<std::uint8_t>();
213 json["tint"]["g"] = m_tint.g<std::uint8_t>();
214 json["tint"]["b"] = m_tint.b<std::uint8_t>();
215 json["tint"]["a"] = m_tint.a<std::uint8_t>();
216
217 return json;
218 }
219
220 void Sprite::deserialize(const nlohmann::json& json)
221 {
222 const auto& tint = json.at("tint");
223 m_tint.set_r(tint.at("r").get<std::uint8_t>());
224 m_tint.set_g(tint.at("g").get<std::uint8_t>());
225 m_tint.set_b(tint.at("b").get<std::uint8_t>());
226 m_tint.set_a(tint.at("a").get<std::uint8_t>());
227
228 set_texture(json.at("texture"));
229 }
230 } // namespace components
231} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
A sprite is a texture with vertex data.
Definition Sprite.hpp:25
const math::fRect & get_clip() const
Get texture clip.
Definition Sprite.cpp:193
math::fRect m_clip
Texture clip.
Definition Sprite.hpp:156
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Sprite.cpp:220
graphics::Colour m_tint
Texture tint.
Definition Sprite.hpp:135
const std::string & get_texture_name() const
Texture name.
Definition Sprite.cpp:203
void set_clip(const math::fRect &rect)
Set texture clip.
Definition Sprite.cpp:161
graphics::Texture2D * get_texture()
Get texture.
Definition Sprite.cpp:198
graphics::VertexArray m_vao
Vertex Array Object.
Definition Sprite.hpp:130
Sprite & operator=(Sprite &&)
Move assignment operator.
Definition Sprite.cpp:51
nlohmann::json serialize() override
Serializes object.
Definition Sprite.cpp:208
std::array< graphics::Vertex, 4 > m_vertices
Vertex data.
Definition Sprite.hpp:151
std::string m_name
Texture debug name.
Definition Sprite.hpp:146
virtual ~Sprite()
Destructor.
Definition Sprite.cpp:74
void set_texture(const std::string &texture)
Sets the texture and vertex data.
Definition Sprite.cpp:82
graphics::Texture2D * m_texture
Texture.
Definition Sprite.hpp:141
Timer.hpp galaxy.
Definition Async.hpp:17